![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Werluschnig: Aufgabe 7, 2 Threads, Temp Werte, Consumer, Producer
main.cpp@0:f5de9cd30ded, 2019-04-29 (annotated)
- Committer:
- martwerl
- Date:
- Mon Apr 29 14:58:34 2019 +0000
- Revision:
- 0:f5de9cd30ded
Werluschnig: Aufgabe 7, zwei Threads, Tempwerte, Consumer, Producer;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martwerl | 0:f5de9cd30ded | 1 | #include "mbed.h" |
martwerl | 0:f5de9cd30ded | 2 | #include "C12832.h" //by Kevin Anderson |
martwerl | 0:f5de9cd30ded | 3 | #include "LM75B.h" //by Chris Styles |
martwerl | 0:f5de9cd30ded | 4 | |
martwerl | 0:f5de9cd30ded | 5 | |
martwerl | 0:f5de9cd30ded | 6 | #define MAX_BUF 3 |
martwerl | 0:f5de9cd30ded | 7 | |
martwerl | 0:f5de9cd30ded | 8 | C12832 lcd(p5,p7,p6,p8,p11); |
martwerl | 0:f5de9cd30ded | 9 | InterruptIn btn(p14);//Joystick Center |
martwerl | 0:f5de9cd30ded | 10 | LM75B tmp(p28,p27); |
martwerl | 0:f5de9cd30ded | 11 | |
martwerl | 0:f5de9cd30ded | 12 | EventQueue con_queue, pro_queue; |
martwerl | 0:f5de9cd30ded | 13 | Thread consumer; |
martwerl | 0:f5de9cd30ded | 14 | Thread producer; |
martwerl | 0:f5de9cd30ded | 15 | |
martwerl | 0:f5de9cd30ded | 16 | |
martwerl | 0:f5de9cd30ded | 17 | class CircularBuffer1 |
martwerl | 0:f5de9cd30ded | 18 | { |
martwerl | 0:f5de9cd30ded | 19 | |
martwerl | 0:f5de9cd30ded | 20 | private: |
martwerl | 0:f5de9cd30ded | 21 | Semaphore s_con, s_pro; |
martwerl | 0:f5de9cd30ded | 22 | Mutex mutex_con, mutex_pro; |
martwerl | 0:f5de9cd30ded | 23 | float buffer[MAX_BUF]; |
martwerl | 0:f5de9cd30ded | 24 | int occupiedBufferCount; |
martwerl | 0:f5de9cd30ded | 25 | int readLocation; |
martwerl | 0:f5de9cd30ded | 26 | int writeLocation; |
martwerl | 0:f5de9cd30ded | 27 | |
martwerl | 0:f5de9cd30ded | 28 | public: |
martwerl | 0:f5de9cd30ded | 29 | CircularBuffer1():s_con(0), s_pro(0) |
martwerl | 0:f5de9cd30ded | 30 | { |
martwerl | 0:f5de9cd30ded | 31 | occupiedBufferCount=0; |
martwerl | 0:f5de9cd30ded | 32 | readLocation=0; |
martwerl | 0:f5de9cd30ded | 33 | writeLocation=0; |
martwerl | 0:f5de9cd30ded | 34 | } |
martwerl | 0:f5de9cd30ded | 35 | float getBuffer() |
martwerl | 0:f5de9cd30ded | 36 | { |
martwerl | 0:f5de9cd30ded | 37 | mutex_con.lock(); |
martwerl | 0:f5de9cd30ded | 38 | if (occupiedBufferCount == 0) |
martwerl | 0:f5de9cd30ded | 39 | { |
martwerl | 0:f5de9cd30ded | 40 | printf("Buffer empty\r\n");//Ausgabe HTerm |
martwerl | 0:f5de9cd30ded | 41 | s_pro.release();//Semaphore |
martwerl | 0:f5de9cd30ded | 42 | s_con.wait(); |
martwerl | 0:f5de9cd30ded | 43 | } |
martwerl | 0:f5de9cd30ded | 44 | |
martwerl | 0:f5de9cd30ded | 45 | float readValue = buffer[readLocation]; |
martwerl | 0:f5de9cd30ded | 46 | occupiedBufferCount--; |
martwerl | 0:f5de9cd30ded | 47 | readLocation = (readLocation + 1) % MAX_BUF; |
martwerl | 0:f5de9cd30ded | 48 | mutex_con.unlock(); |
martwerl | 0:f5de9cd30ded | 49 | return readValue; |
martwerl | 0:f5de9cd30ded | 50 | } |
martwerl | 0:f5de9cd30ded | 51 | void setBuffer(float value) |
martwerl | 0:f5de9cd30ded | 52 | { |
martwerl | 0:f5de9cd30ded | 53 | mutex_pro.lock(); |
martwerl | 0:f5de9cd30ded | 54 | if (occupiedBufferCount == MAX_BUF) |
martwerl | 0:f5de9cd30ded | 55 | { |
martwerl | 0:f5de9cd30ded | 56 | printf("Buffer full\r\n");//Ausgabe HTerm |
martwerl | 0:f5de9cd30ded | 57 | s_con.release();//Semaphore |
martwerl | 0:f5de9cd30ded | 58 | s_pro.wait(); |
martwerl | 0:f5de9cd30ded | 59 | } |
martwerl | 0:f5de9cd30ded | 60 | |
martwerl | 0:f5de9cd30ded | 61 | buffer[writeLocation] = value; |
martwerl | 0:f5de9cd30ded | 62 | printf("occupiedBufferCount: %i \n", occupiedBufferCount);//Ausgabe HTerm |
martwerl | 0:f5de9cd30ded | 63 | occupiedBufferCount++; |
martwerl | 0:f5de9cd30ded | 64 | writeLocation = (writeLocation + 1) % MAX_BUF; |
martwerl | 0:f5de9cd30ded | 65 | mutex_pro.unlock(); |
martwerl | 0:f5de9cd30ded | 66 | |
martwerl | 0:f5de9cd30ded | 67 | } |
martwerl | 0:f5de9cd30ded | 68 | }; |
martwerl | 0:f5de9cd30ded | 69 | |
martwerl | 0:f5de9cd30ded | 70 | |
martwerl | 0:f5de9cd30ded | 71 | CircularBuffer1 cb; |
martwerl | 0:f5de9cd30ded | 72 | |
martwerl | 0:f5de9cd30ded | 73 | void consumer_worker() |
martwerl | 0:f5de9cd30ded | 74 | { |
martwerl | 0:f5de9cd30ded | 75 | float temp = cb.getBuffer(); |
martwerl | 0:f5de9cd30ded | 76 | printf("Temperatur: %.2f\n",temp);//Ausgabe HTerm |
martwerl | 0:f5de9cd30ded | 77 | lcd.locate(10,10); |
martwerl | 0:f5de9cd30ded | 78 | lcd.printf("Temperatur: %.2f", temp);//Ausgabe LCD |
martwerl | 0:f5de9cd30ded | 79 | |
martwerl | 0:f5de9cd30ded | 80 | } |
martwerl | 0:f5de9cd30ded | 81 | |
martwerl | 0:f5de9cd30ded | 82 | void producer_worker() |
martwerl | 0:f5de9cd30ded | 83 | { |
martwerl | 0:f5de9cd30ded | 84 | cb.setBuffer(tmp.read());//Class CircularBuffer1 |
martwerl | 0:f5de9cd30ded | 85 | } |
martwerl | 0:f5de9cd30ded | 86 | |
martwerl | 0:f5de9cd30ded | 87 | void btn_pressed() |
martwerl | 0:f5de9cd30ded | 88 | { |
martwerl | 0:f5de9cd30ded | 89 | con_queue.call(consumer_worker);//Event Queue Consumer |
martwerl | 0:f5de9cd30ded | 90 | } |
martwerl | 0:f5de9cd30ded | 91 | |
martwerl | 0:f5de9cd30ded | 92 | int main() |
martwerl | 0:f5de9cd30ded | 93 | { |
martwerl | 0:f5de9cd30ded | 94 | btn.fall(&btn_pressed);//Joystick Center drücken |
martwerl | 0:f5de9cd30ded | 95 | pro_queue.call_every(1000, producer_worker);//jede Sekunde |
martwerl | 0:f5de9cd30ded | 96 | |
martwerl | 0:f5de9cd30ded | 97 | consumer.start(callback(&con_queue, &EventQueue::dispatch_forever));//Queue dispatch event ohne timeout |
martwerl | 0:f5de9cd30ded | 98 | producer.start(callback(&pro_queue, &EventQueue::dispatch_forever)); |
martwerl | 0:f5de9cd30ded | 99 | |
martwerl | 0:f5de9cd30ded | 100 | } |