Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 3afd408

Browse files
authored
Merge pull request #243 from ARMmbed/fix_blinky_thread
Using thread signals instead of restarting a thread
2 parents 8a85350 + 2c8ff1b commit 3afd408

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

main.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@
3535
#define RED_LED (LED3)
3636
#define GREEN_LED (LED1)
3737
#define BLUE_LED (LED2)
38-
#define LED_ON (1)
38+
#define LED_ON (1)
3939
#else // !TARGET_STM
4040
#define RED_LED (LED1)
4141
#define GREEN_LED (LED2)
42-
#define BLUE_LED (LED3)
43-
#define LED_ON (0)
42+
#define BLUE_LED (LED3)
43+
#define LED_ON (0)
4444
#endif // !TARGET_STM
4545
#define LED_OFF (!LED_ON)
4646

47+
#define BLINK_SIGNAL 0x1
48+
4749
// Status indication
4850
DigitalOut red_led(RED_LED);
4951
DigitalOut green_led(GREEN_LED);
@@ -103,6 +105,7 @@ class LedResource {
103105
public:
104106
LedResource() {
105107
// create ObjectID with metadata tag of '3201', which is 'digital output'
108+
blinky_thread.start(callback(this, &LedResource::do_blink));
106109
led_object = M2MInterfaceFactory::create_object("3201");
107110
M2MObjectInstance* led_inst = led_object->create_object_instance();
108111

@@ -176,30 +179,33 @@ class LedResource {
176179
printf("Payload: %.*s\n", payload_length, payload);
177180
}
178181
// do_blink is called with the vector, and starting at -1
179-
blinky_thread.start(callback(this, &LedResource::do_blink));
182+
blinky_thread.signal_set(BLINK_SIGNAL);
180183
}
181184

182185
private:
183186
M2MObject* led_object;
184187
Thread blinky_thread;
185188
BlinkArgs *blink_args;
186189
void do_blink() {
187-
for (;;) {
188-
// blink the LED
189-
red_led = !red_led;
190-
// up the position, if we reached the end of the vector
191-
if (blink_args->position >= blink_args->blink_pattern.size()) {
192-
// send delayed response after blink is done
193-
M2MObjectInstance* inst = led_object->object_instance();
194-
M2MResource* led_res = inst->resource("5850");
195-
led_res->send_delayed_post_response();
196-
red_led = LED_OFF;
197-
status_ticker.attach_us(blinky, 250000);
198-
return;
199-
}
200-
// Wait requested time, then continue processing the blink pattern from next position.
201-
Thread::wait(blink_args->blink_pattern.at(blink_args->position));
202-
blink_args->position++;
190+
for(;;) {
191+
blinky_thread.signal_wait(BLINK_SIGNAL);
192+
for (;;) {
193+
// blink the LED
194+
red_led = !red_led;
195+
// up the position, if we reached the end of the vector
196+
if (blink_args->position >= blink_args->blink_pattern.size()) {
197+
// send delayed response after blink is done
198+
M2MObjectInstance* inst = led_object->object_instance();
199+
M2MResource* led_res = inst->resource("5850");
200+
led_res->send_delayed_post_response();
201+
red_led = LED_OFF;
202+
status_ticker.attach_us(blinky, 250000);
203+
break;
204+
}
205+
// Wait requested time, then continue prosessing the blink pattern from next position.
206+
Thread::wait(blink_args->blink_pattern.at(blink_args->position));
207+
blink_args->position++;
208+
}
203209
}
204210
}
205211
};

0 commit comments

Comments
 (0)