Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: fll mbed-rtos mbed
fll.cpp@32:fdf9f6fca8a2, 2015-02-17 (annotated)
- Committer:
- sierra
- Date:
- Tue Feb 17 03:19:06 2015 +0000
- Revision:
- 32:fdf9f6fca8a2
- Parent:
- 31:2dbed2eea0f2
- Child:
- 33:cc84b10e6c67
Move main process to FLL class
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| amutake | 0:c80e972b4c59 | 1 | // Frame Level Language |
| amutake | 0:c80e972b4c59 | 2 | // implementation |
| amutake | 0:c80e972b4c59 | 3 | |
| amutake | 0:c80e972b4c59 | 4 | #include "mbed.h" |
| amutake | 0:c80e972b4c59 | 5 | #include "rtos.h" |
| sierra | 31:2dbed2eea0f2 | 6 | #include "fll.h" |
| sierra | 31:2dbed2eea0f2 | 7 | |
| amutake | 0:c80e972b4c59 | 8 | #include <stdint.h> |
| amutake | 0:c80e972b4c59 | 9 | |
| sierra | 32:fdf9f6fca8a2 | 10 | void invoke_sinkrun(const void *p); |
| sierra | 32:fdf9f6fca8a2 | 11 | |
| amutake | 7:61b4825304e2 | 12 | // if you want to debug by `printf`, uncomment next line and put `pc.printf("...", ...);` into certain position. |
| amutake | 7:61b4825304e2 | 13 | // Serial pc(USBTX, USBRX); |
| sierra | 1:1abcd83947bf | 14 | |
| amutake | 0:c80e972b4c59 | 15 | // ---- |
| amutake | 0:c80e972b4c59 | 16 | // Sink |
| amutake | 0:c80e972b4c59 | 17 | // ---- |
| sierra | 6:d0348b7a2f05 | 18 | Sink::Sink(Producer* src, rtos::Mail<button_t, MAIL_BOX_SIZE>* box, Mutex* mut) |
| amutake | 0:c80e972b4c59 | 19 | { |
| amutake | 0:c80e972b4c59 | 20 | source = src; |
| amutake | 2:165723d41023 | 21 | mail_box = box; |
| amutake | 2:165723d41023 | 22 | mutex = mut; |
| amutake | 0:c80e972b4c59 | 23 | } |
| amutake | 0:c80e972b4c59 | 24 | |
| amutake | 0:c80e972b4c59 | 25 | void Sink::run() |
| amutake | 0:c80e972b4c59 | 26 | { |
| sierra | 3:edbf31a8589f | 27 | button_t *btn; |
| sierra | 4:9ee673ca05ad | 28 | while(1) { |
| amutake | 2:165723d41023 | 29 | mutex->lock(); |
| sierra | 3:edbf31a8589f | 30 | btn = mail_box->alloc(); |
| sierra | 4:9ee673ca05ad | 31 | if(!btn) { |
| sierra | 4:9ee673ca05ad | 32 | mutex->unlock(); |
| sierra | 4:9ee673ca05ad | 33 | continue; |
| sierra | 4:9ee673ca05ad | 34 | } |
| sierra | 1:1abcd83947bf | 35 | *btn = source->await(); |
| amutake | 2:165723d41023 | 36 | mail_box->put(btn); |
| amutake | 2:165723d41023 | 37 | mutex->unlock(); |
| amutake | 0:c80e972b4c59 | 38 | } |
| amutake | 0:c80e972b4c59 | 39 | } |
| amutake | 0:c80e972b4c59 | 40 | |
| sierra | 6:d0348b7a2f05 | 41 | void Sink::reset(Producer* src) |
| amutake | 0:c80e972b4c59 | 42 | { |
| amutake | 2:165723d41023 | 43 | mutex->lock(); |
| amutake | 7:61b4825304e2 | 44 | // consume current (and actually old) buffer |
| amutake | 0:c80e972b4c59 | 45 | osEvent e; |
| amutake | 0:c80e972b4c59 | 46 | do { |
| amutake | 2:165723d41023 | 47 | e = mail_box->get(0); |
| sierra | 1:1abcd83947bf | 48 | if(e.status == osEventMail) { |
| sierra | 3:edbf31a8589f | 49 | mail_box->free((button_t*)e.value.p); |
| sierra | 1:1abcd83947bf | 50 | } |
| sierra | 1:1abcd83947bf | 51 | } while (e.status == osEventMail); |
| amutake | 7:61b4825304e2 | 52 | // update source |
| amutake | 0:c80e972b4c59 | 53 | source = src; |
| amutake | 2:165723d41023 | 54 | mutex->unlock(); |
| amutake | 0:c80e972b4c59 | 55 | } |
| amutake | 0:c80e972b4c59 | 56 | |
| amutake | 0:c80e972b4c59 | 57 | // ------ |
| amutake | 0:c80e972b4c59 | 58 | // Output |
| amutake | 0:c80e972b4c59 | 59 | // ------ |
| sierra | 32:fdf9f6fca8a2 | 60 | Output::Output(rtos::Mail<button_t, MAIL_BOX_SIZE>* box, FLL *f) |
| amutake | 0:c80e972b4c59 | 61 | { |
| amutake | 2:165723d41023 | 62 | mail_box = box; |
| sierra | 32:fdf9f6fca8a2 | 63 | fll = f; |
| amutake | 0:c80e972b4c59 | 64 | } |
| amutake | 0:c80e972b4c59 | 65 | |
| amutake | 0:c80e972b4c59 | 66 | void Output::run() |
| amutake | 0:c80e972b4c59 | 67 | { |
| amutake | 2:165723d41023 | 68 | osEvent e = mail_box->get(0); |
| amutake | 17:69536d02cb3c | 69 | if (e.status == osEventMail) { // getting is success |
| sierra | 3:edbf31a8589f | 70 | button_t b = *(button_t*)(e.value.p); |
| sierra | 3:edbf31a8589f | 71 | mail_box->free((button_t*)e.value.p); |
| sierra | 32:fdf9f6fca8a2 | 72 | fll->press(b); |
| amutake | 0:c80e972b4c59 | 73 | } else { |
| sierra | 32:fdf9f6fca8a2 | 74 | fll->press(0); // if mail box is empty |
| amutake | 0:c80e972b4c59 | 75 | } |
| amutake | 0:c80e972b4c59 | 76 | } |
| amutake | 0:c80e972b4c59 | 77 | |
| sierra | 32:fdf9f6fca8a2 | 78 | FLL::FLL(Producer* p) |
| sierra | 32:fdf9f6fca8a2 | 79 | { |
| sierra | 32:fdf9f6fca8a2 | 80 | producer = p; |
| sierra | 32:fdf9f6fca8a2 | 81 | |
| sierra | 32:fdf9f6fca8a2 | 82 | // button_no -> pin (R1 to L1) |
| sierra | 32:fdf9f6fca8a2 | 83 | PinName ps[] = { |
| sierra | 32:fdf9f6fca8a2 | 84 | p10, p5, p6, p7, p8, p9, |
| sierra | 32:fdf9f6fca8a2 | 85 | p11, p12, p13, p18, p16, p17, p15, |
| sierra | 32:fdf9f6fca8a2 | 86 | p19, p14, |
| sierra | 32:fdf9f6fca8a2 | 87 | }; |
| sierra | 32:fdf9f6fca8a2 | 88 | |
| sierra | 32:fdf9f6fca8a2 | 89 | for(int i = 0; i < BUTTON_NUM; i++) { |
| sierra | 32:fdf9f6fca8a2 | 90 | off[i] = new int(1); |
| sierra | 32:fdf9f6fca8a2 | 91 | pin[i] = new DigitalOut(ps[i], *off[i]); |
| sierra | 32:fdf9f6fca8a2 | 92 | } |
| sierra | 32:fdf9f6fca8a2 | 93 | } |
| amutake | 0:c80e972b4c59 | 94 | |
| sierra | 32:fdf9f6fca8a2 | 95 | FLL::~FLL() |
| sierra | 32:fdf9f6fca8a2 | 96 | { |
| sierra | 32:fdf9f6fca8a2 | 97 | for(int i = 0; i < BUTTON_NUM; i++) { |
| sierra | 32:fdf9f6fca8a2 | 98 | delete off[i]; |
| sierra | 32:fdf9f6fca8a2 | 99 | delete pin[i]; |
| sierra | 32:fdf9f6fca8a2 | 100 | } |
| sierra | 32:fdf9f6fca8a2 | 101 | } |
| amutake | 0:c80e972b4c59 | 102 | |
| amutake | 7:61b4825304e2 | 103 | // button-pin mapping table |
| sierra | 32:fdf9f6fca8a2 | 104 | void FLL::press(button_t btn) |
| amutake | 0:c80e972b4c59 | 105 | { |
| sierra | 32:fdf9f6fca8a2 | 106 | for(int i = 0; i < BUTTON_NUM; i++) { |
| sierra | 32:fdf9f6fca8a2 | 107 | if ((btn >> i) & 0x1) { |
| sierra | 32:fdf9f6fca8a2 | 108 | pin[i]->write(!(*off[i])); |
| amutake | 0:c80e972b4c59 | 109 | } else { |
| sierra | 32:fdf9f6fca8a2 | 110 | pin[i]->write(*off[i]); |
| amutake | 0:c80e972b4c59 | 111 | } |
| amutake | 0:c80e972b4c59 | 112 | } |
| amutake | 0:c80e972b4c59 | 113 | } |
| amutake | 8:d16a0fcc2735 | 114 | |
| sierra | 32:fdf9f6fca8a2 | 115 | void FLL::run() |
| amutake | 17:69536d02cb3c | 116 | { |
| amutake | 8:d16a0fcc2735 | 117 | Mail<button_t, MAIL_BOX_SIZE>* mail_box = new Mail<button_t, MAIL_BOX_SIZE>(); |
| amutake | 8:d16a0fcc2735 | 118 | Mutex* mutex = new Mutex(); |
| amutake | 8:d16a0fcc2735 | 119 | |
| amutake | 8:d16a0fcc2735 | 120 | Sink* sink = new Sink(producer, mail_box, mutex); |
| amutake | 8:d16a0fcc2735 | 121 | |
| amutake | 8:d16a0fcc2735 | 122 | Ticker ticker; |
| sierra | 32:fdf9f6fca8a2 | 123 | Output* output = new Output(mail_box, this); |
| amutake | 8:d16a0fcc2735 | 124 | |
| amutake | 8:d16a0fcc2735 | 125 | Thread th(invoke_sinkrun, (void *)sink); |
| amutake | 16:253d933a7633 | 126 | DigitalIn pin20(p20); |
| amutake | 16:253d933a7633 | 127 | wait(1); |
| amutake | 16:253d933a7633 | 128 | while (1) { |
| amutake | 16:253d933a7633 | 129 | if (pin20.read() == 1) { |
| amutake | 16:253d933a7633 | 130 | break; |
| amutake | 16:253d933a7633 | 131 | } |
| amutake | 16:253d933a7633 | 132 | } |
| amutake | 8:d16a0fcc2735 | 133 | ticker.attach(output, &Output::run, FRAME); |
| amutake | 8:d16a0fcc2735 | 134 | |
| amutake | 8:d16a0fcc2735 | 135 | Thread::wait(osWaitForever); |
| sierra | 32:fdf9f6fca8a2 | 136 | } |
| sierra | 32:fdf9f6fca8a2 | 137 | |
| sierra | 32:fdf9f6fca8a2 | 138 | static void invoke_sinkrun(const void *p) |
| sierra | 32:fdf9f6fca8a2 | 139 | { |
| sierra | 32:fdf9f6fca8a2 | 140 | ((Sink*)p)->run(); |
| amutake | 8:d16a0fcc2735 | 141 | } |
