
a
Revision 1:cc715a7c24a5, committed 2018-11-30
- Comitter:
- lucaspennati
- Date:
- Fri Nov 30 11:50:00 2018 +0000
- Parent:
- 0:6ab5b8697bfb
- Commit message:
- a
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 6ab5b8697bfb -r cc715a7c24a5 main.cpp --- a/main.cpp Mon Nov 12 12:10:30 2018 +0000 +++ b/main.cpp Fri Nov 30 11:50:00 2018 +0000 @@ -1,34 +1,81 @@ #include "mbed.h" -DigitalOut led1(LED1); -InterruptIn button(USER_BUTTON); -EventQueue queue(32 * EVENTS_EVENT_SIZE); -Thread t; +#define QUEUE_MAX_SIZE 32 + +typedef struct { + int count; +} message_t; + +Queue<message_t, QUEUE_MAX_SIZE> queue; +Mutex mutex; +ConditionVariable cond(mutex); + +int queue_size = 0; + -void rise_handler_thread_context(void) { - printf("rise_handler_thread_context in context %p\r\n", Thread::gettid()); +void consumer_thread() { + while(true) { + int wait_time = rand() % 5; + printf("Consumer - Waiting %d seconds\n", wait_time); + wait(wait_time); + if (queue_size > 0) { + printf("Consumer - Queue is not empty, getting message\n"); + // Acquire the lock + mutex.lock(); + // Decrease the size of the queue + queue_size--; + // Get the message + osEvent evt = queue.get(); + // Extract the actual payload + if (evt.status == osEventMessage) { + message_t *message = (message_t *) evt.value.p; + printf("Consumer - Got message with payload %d\n", message->count); + } + // Notify + cond.notify_all(); + + // Unlock the mutex + mutex.unlock(); + } else { + printf("Consumer - Queue is empty! waiting for producer\n"); + } + } } -void rise_handler_iterrupt_context(void) { - // Execute the time critical part first - led1 = !led1; - // The rest can execute later in user context (and can contain code that's not interrupt safe) - // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue - queue.call(rise_handler_thread_context); -} - -void fall_handler(void) { - printf("fall_handler in context %p\r\n", Thread::gettid()); - // Toggle LED - led1 = !led1; -} int main() { - // Start the event queue - t.start(callback(&queue, &EventQueue::dispatch_forever)); - printf("Starting in context %p\r\n", Thread::gettid()); - // The 'rise' handler will execute in IRQ context - button.rise(rise_handler_iterrupt_context); - // The 'fall' handler will execute in the context of thread 't' - button.fall(queue.event(fall_handler)); + Thread consumer; + consumer.start(consumer_thread); + + + while(1) { + // Random wait time + int wait_time = rand() % 5; + printf("Producer - Waiting %d seconds\n", wait_time); + wait(wait_time); + if (queue_size < QUEUE_MAX_SIZE) { + // Acquire the lock + mutex.lock(); + + // Increase the count + queue_size++; + + // create a new message + message_t *message; + message->count = queue_size; + + // Add it to the queue + queue.put(message); + + printf("Producer - Added message to queue with payload %d\n", queue_size); + + // Notify all + cond.notify_all(); + + // Unlock the mutex + mutex.unlock(); + } else { + printf("Producer - Queue is full! waiting for consumer\n"); + } + } }