Skip to content

Commit f95b705

Browse files
committed
src/lightgrid: use contiki process
This finishes the TODO to convert the light grid animation to use a contiki process.
1 parent d33c050 commit f95b705

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

lib/pbio/include/pbio/lightgrid.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,15 @@ pbio_error_t pbio_lightgrid_set_image(pbio_lightgrid_t *lightgrid, const uint8_t
8181
* @param [in] images Buffer of buffer of brightness values (0--100)
8282
* @param [in] frames Number of images
8383
* @param [in] interval Time between subsequent images
84-
* @return ::PBIO_SUCCESS on success or
85-
* ::PBIO_ERROR_NOT_SUPPORTED if the PWM driver is disabled.
8684
*/
87-
pbio_error_t pbio_lightgrid_start_pattern(pbio_lightgrid_t *lightgrid, const uint8_t *images, uint8_t frames, uint32_t interval);
85+
void pbio_lightgrid_start_pattern(pbio_lightgrid_t *lightgrid, const uint8_t *images, uint8_t frames, uint32_t interval);
8886

8987
/**
9088
* Stops the pattern from updating further
9189
* @param [in] lightgrid The lightgrid object
9290
*/
9391
void pbio_lightgrid_stop_pattern(pbio_lightgrid_t *lightgrid);
9492

95-
96-
// TODO: Convert to contiki process
97-
void _pbio_lightgrid_poll(uint32_t now);
98-
99-
#else
100-
static inline void _pbio_lightgrid_poll(uint32_t now) {
101-
}
10293
#endif // PBIO_CONFIG_LIGHTGRID
10394

10495
#endif // _PBIO_LIGHTGRID_H_

lib/pbio/src/lightgrid.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
struct _pbio_lightgrid_t {
1818
pbdrv_pwm_dev_t *pwm;
1919
const pbdrv_lightgrid_platform_data_t *data;
20-
uint32_t last_poll;
2120
uint8_t number_of_frames;
2221
uint8_t frame_index;
2322
uint8_t interval;
2423
const uint8_t *frame_data;
2524
};
2625

26+
PROCESS(pbio_lightgrid_process, "light grid");
2727
static pbio_lightgrid_t _lightgrid;
2828

2929
pbio_error_t pbio_lightgrid_get_dev(pbio_lightgrid_t **lightgrid) {
@@ -106,18 +106,16 @@ pbio_error_t pbio_lightgrid_set_image(pbio_lightgrid_t *lightgrid, const uint8_t
106106
}
107107

108108
void pbio_lightgrid_stop_pattern(pbio_lightgrid_t *lightgrid) {
109-
lightgrid->number_of_frames = 0;
109+
process_exit(&pbio_lightgrid_process);
110110
}
111111

112-
pbio_error_t pbio_lightgrid_start_pattern(pbio_lightgrid_t *lightgrid, const uint8_t *images, uint8_t frames, uint32_t interval) {
112+
void pbio_lightgrid_start_pattern(pbio_lightgrid_t *lightgrid, const uint8_t *images, uint8_t frames, uint32_t interval) {
113113
lightgrid->number_of_frames = frames;
114114
lightgrid->frame_index = 0;
115115
lightgrid->interval = interval;
116116
lightgrid->frame_data = images;
117-
lightgrid->last_poll = clock_time();
118117

119-
// Start with the first frame
120-
return pbio_lightgrid_set_image(lightgrid, images);
118+
process_start(&pbio_lightgrid_process, NULL);
121119
}
122120

123121
// FIXME: compress / implement differently
@@ -164,29 +162,30 @@ const uint8_t pbio_lightgrid_sys_pattern[1000] = {
164162
0, 0, 0, 0, 0, 15, 68, 100, 72, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165163
};
166164

167-
// TODO: Convert to contiki process
168-
void _pbio_lightgrid_poll(uint32_t now) {
165+
PROCESS_THREAD(pbio_lightgrid_process, ev, data) {
166+
static pbio_lightgrid_t *lightgrid = &_lightgrid;
167+
static struct etimer timer;
169168

170-
pbio_lightgrid_t *lightgrid = &_lightgrid;
169+
PROCESS_BEGIN();
171170

172-
// Poll only if there are frames to do
173-
if (lightgrid->number_of_frames > 1) {
171+
etimer_set(&timer, clock_from_msec(lightgrid->interval));
174172

175-
// Check if we are past the next sample yet
176-
if (now - lightgrid->last_poll >= lightgrid->interval) {
173+
for (;;) {
174+
// Current frame data
175+
uint8_t size = lightgrid->data->size;
176+
const uint8_t *frame = lightgrid->frame_data + size * size * lightgrid->frame_index;
177177

178-
// Bump the poll time and frame index
179-
lightgrid->last_poll += lightgrid->interval;
180-
lightgrid->frame_index = (lightgrid->frame_index + 1) % lightgrid->number_of_frames;
178+
// Display the frame
179+
pbio_lightgrid_set_image(lightgrid, frame);
181180

182-
// Current frame
183-
const uint8_t *frame = lightgrid->frame_data + lightgrid->data->size * lightgrid->data->size * lightgrid->frame_index;
181+
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
182+
etimer_reset(&timer);
184183

185-
// Display the frame
186-
pbio_lightgrid_set_image(lightgrid, frame);
187-
}
184+
// Move to next frame
185+
lightgrid->frame_index = (lightgrid->frame_index + 1) % lightgrid->number_of_frames;
188186
}
189187

188+
PROCESS_END();
190189
}
191190

192191
#endif // PBIO_CONFIG_LIGHTGRID

lib/pbio/src/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ int pbio_do_one_event(void) {
8888
}
8989
if (now - prev_slow_poll_time >= clock_from_msec(32)) {
9090
_pbio_light_poll(now);
91-
_pbio_lightgrid_poll(now);
9291
prev_slow_poll_time = now;
9392
}
9493
return process_run();

pybricks/common/pb_type_lightgrid.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ STATIC mp_obj_t common_LightGrid_pattern(size_t n_args, const mp_obj_t *pos_args
242242
common_LightGrid_image__extract(image_objs[i], size, self->data + size * size * i);
243243
}
244244

245+
// Stop any ongoing pattern
246+
pbio_lightgrid_stop_pattern(self->lightgrid);
247+
245248
// Activate the pattern
246249
pbio_lightgrid_start_pattern(self->lightgrid, self->data, self->frames, dt);
247250
return mp_const_none;

0 commit comments

Comments
 (0)