![](/media/cache/profiles/ef6fe50336005eee1e8e9093bfe576ae.jpg.50x50_q85.jpg)
stackable sakura.io board for patlite
main.cpp@1:36220c7411e9, 2017-12-22 (annotated)
- Committer:
- misodengaku
- Date:
- Fri Dec 22 05:52:01 2017 +0000
- Revision:
- 1:36220c7411e9
- Parent:
- 0:a277e27599ee
SREF-PAT-01
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
misodengaku | 0:a277e27599ee | 1 | #include "mbed.h" |
misodengaku | 0:a277e27599ee | 2 | #include "SakuraIO.h" |
misodengaku | 0:a277e27599ee | 3 | |
misodengaku | 1:36220c7411e9 | 4 | // プロトタイプ宣言 |
misodengaku | 1:36220c7411e9 | 5 | uint8_t monitor(); |
misodengaku | 1:36220c7411e9 | 6 | uint8_t dequeue(); |
misodengaku | 0:a277e27599ee | 7 | |
misodengaku | 0:a277e27599ee | 8 | DigitalOut status_led(PA_15); |
misodengaku | 1:36220c7411e9 | 9 | DigitalOut patlite_1(PA_0); // R |
misodengaku | 1:36220c7411e9 | 10 | DigitalOut patlite_2(PA_1); // Y |
misodengaku | 1:36220c7411e9 | 11 | DigitalOut patlite_3(PA_2); // G |
misodengaku | 1:36220c7411e9 | 12 | DigitalOut patlite_4(PA_3); // B |
misodengaku | 1:36220c7411e9 | 13 | DigitalOut patlite_5(PA_4); // C |
misodengaku | 1:36220c7411e9 | 14 | DigitalIn patlite_state_1(PA_12); // R |
misodengaku | 1:36220c7411e9 | 15 | DigitalIn patlite_state_2(PA_8); // Y |
misodengaku | 1:36220c7411e9 | 16 | DigitalIn patlite_state_3(PA_7); // G |
misodengaku | 1:36220c7411e9 | 17 | DigitalIn patlite_state_4(PA_6); // B |
misodengaku | 1:36220c7411e9 | 18 | DigitalIn patlite_state_5(PA_5); // C |
misodengaku | 0:a277e27599ee | 19 | |
misodengaku | 1:36220c7411e9 | 20 | DigitalOut patlite_out[5] = {patlite_1, patlite_2, patlite_3, patlite_4, patlite_5}; |
misodengaku | 1:36220c7411e9 | 21 | DigitalIn patlite_state[5] = {patlite_state_1, patlite_state_2, patlite_state_3, patlite_state_4, patlite_state_5}; |
misodengaku | 0:a277e27599ee | 22 | |
misodengaku | 0:a277e27599ee | 23 | I2C i2c(PB_7, PB_6); // sda, scl |
misodengaku | 1:36220c7411e9 | 24 | SakuraIO_I2C sakuraio(i2c); |
misodengaku | 0:a277e27599ee | 25 | |
misodengaku | 0:a277e27599ee | 26 | uint8_t last_state[5] = {0}; |
misodengaku | 0:a277e27599ee | 27 | |
misodengaku | 1:36220c7411e9 | 28 | int main() |
misodengaku | 1:36220c7411e9 | 29 | { |
misodengaku | 1:36220c7411e9 | 30 | // パトライトを1段ずつ順に点滅させながら接続待ち |
misodengaku | 1:36220c7411e9 | 31 | for (int patlite_index = 0;; patlite_index = (patlite_index + 1) % 5) |
misodengaku | 1:36220c7411e9 | 32 | { |
misodengaku | 1:36220c7411e9 | 33 | if ((sakuraio.getConnectionStatus() & 0x80) == 0x80) |
misodengaku | 1:36220c7411e9 | 34 | break; |
misodengaku | 0:a277e27599ee | 35 | |
misodengaku | 1:36220c7411e9 | 36 | status_led = 1; |
misodengaku | 1:36220c7411e9 | 37 | patlite_out[patlite_index] = 1; |
misodengaku | 1:36220c7411e9 | 38 | wait_ms(500); |
misodengaku | 1:36220c7411e9 | 39 | status_led = 0; |
misodengaku | 1:36220c7411e9 | 40 | patlite_out[patlite_index] = 0; |
misodengaku | 1:36220c7411e9 | 41 | wait_ms(500); |
misodengaku | 1:36220c7411e9 | 42 | } |
misodengaku | 1:36220c7411e9 | 43 | |
misodengaku | 1:36220c7411e9 | 44 | // 接続完了したら全段1度点滅 |
misodengaku | 1:36220c7411e9 | 45 | status_led = 1; |
misodengaku | 1:36220c7411e9 | 46 | for (int i = 0; i < 5; i++) |
misodengaku | 1:36220c7411e9 | 47 | { |
misodengaku | 1:36220c7411e9 | 48 | patlite_out[i] = 1; |
misodengaku | 1:36220c7411e9 | 49 | } |
misodengaku | 1:36220c7411e9 | 50 | wait(1); |
misodengaku | 1:36220c7411e9 | 51 | status_led = 0; |
misodengaku | 0:a277e27599ee | 52 | for (int i = 0; i < 5; i++) |
misodengaku | 0:a277e27599ee | 53 | { |
misodengaku | 1:36220c7411e9 | 54 | patlite_out[i] = 0; |
misodengaku | 1:36220c7411e9 | 55 | } |
misodengaku | 1:36220c7411e9 | 56 | |
misodengaku | 1:36220c7411e9 | 57 | while (1) |
misodengaku | 1:36220c7411e9 | 58 | { |
misodengaku | 1:36220c7411e9 | 59 | // ピン状態と新着メッセージを監視してどちらか変化があったら1 |
misodengaku | 1:36220c7411e9 | 60 | uint8_t changed = monitor() | dequeue(); |
misodengaku | 1:36220c7411e9 | 61 | if (changed == 1) |
misodengaku | 1:36220c7411e9 | 62 | { |
misodengaku | 1:36220c7411e9 | 63 | uint8_t state[5]; |
misodengaku | 1:36220c7411e9 | 64 | for (int i = 0; i < 5; i++) |
misodengaku | 1:36220c7411e9 | 65 | { |
misodengaku | 1:36220c7411e9 | 66 | state[i] = patlite_state[i]; |
misodengaku | 1:36220c7411e9 | 67 | } |
misodengaku | 1:36220c7411e9 | 68 | for (int i = 0; i < 5; i++) |
misodengaku | 1:36220c7411e9 | 69 | { |
misodengaku | 1:36220c7411e9 | 70 | sakuraio.enqueueTx(i, (int32_t)state[i]); |
misodengaku | 1:36220c7411e9 | 71 | } |
misodengaku | 1:36220c7411e9 | 72 | sakuraio.send(); |
misodengaku | 1:36220c7411e9 | 73 | } |
misodengaku | 1:36220c7411e9 | 74 | wait_ms(10); |
misodengaku | 1:36220c7411e9 | 75 | } |
misodengaku | 1:36220c7411e9 | 76 | } |
misodengaku | 1:36220c7411e9 | 77 | |
misodengaku | 1:36220c7411e9 | 78 | // 外部からパトライトの点灯状態が変えられたことを検知する |
misodengaku | 1:36220c7411e9 | 79 | uint8_t monitor() |
misodengaku | 1:36220c7411e9 | 80 | { |
misodengaku | 1:36220c7411e9 | 81 | uint8_t changed = 0; |
misodengaku | 1:36220c7411e9 | 82 | uint8_t state[5]; |
misodengaku | 1:36220c7411e9 | 83 | for (int i = 0; i < 5; i++) |
misodengaku | 1:36220c7411e9 | 84 | { |
misodengaku | 1:36220c7411e9 | 85 | // 負論理なのでNOTかけてから点灯状態として取り込み |
misodengaku | 0:a277e27599ee | 86 | state[i] = !patlite_state[i]; |
misodengaku | 0:a277e27599ee | 87 | } |
misodengaku | 0:a277e27599ee | 88 | |
misodengaku | 0:a277e27599ee | 89 | for (int i = 0; i < 5; i++) |
misodengaku | 0:a277e27599ee | 90 | { |
misodengaku | 0:a277e27599ee | 91 | if (last_state[i] != state[i]) |
misodengaku | 0:a277e27599ee | 92 | { |
misodengaku | 0:a277e27599ee | 93 | changed = 1; |
misodengaku | 0:a277e27599ee | 94 | status_led = !status_led; |
misodengaku | 0:a277e27599ee | 95 | last_state[i] = state[i]; |
misodengaku | 0:a277e27599ee | 96 | } |
misodengaku | 0:a277e27599ee | 97 | } |
misodengaku | 1:36220c7411e9 | 98 | |
misodengaku | 1:36220c7411e9 | 99 | return changed; |
misodengaku | 0:a277e27599ee | 100 | } |
misodengaku | 0:a277e27599ee | 101 | |
misodengaku | 1:36220c7411e9 | 102 | // sakura.ioのRxキューの状態を見て操作命令が来ていないか確認する |
misodengaku | 1:36220c7411e9 | 103 | uint8_t dequeue() |
misodengaku | 0:a277e27599ee | 104 | { |
misodengaku | 1:36220c7411e9 | 105 | uint8_t avail = 0, queued = 0, changed = 0; |
misodengaku | 1:36220c7411e9 | 106 | sakuraio.getRxQueueLength(&avail, &queued); |
misodengaku | 1:36220c7411e9 | 107 | for (uint8_t i = 0; i < queued; i++) |
misodengaku | 0:a277e27599ee | 108 | { |
misodengaku | 1:36220c7411e9 | 109 | uint8_t channel, type, values[8]; |
misodengaku | 1:36220c7411e9 | 110 | int64_t offset; |
misodengaku | 0:a277e27599ee | 111 | |
misodengaku | 1:36220c7411e9 | 112 | // RXキューから点灯要求を取り出す |
misodengaku | 1:36220c7411e9 | 113 | uint8_t ret = sakuraio.dequeueRx(&channel, &type, values, &offset); |
misodengaku | 1:36220c7411e9 | 114 | if (ret == 0x01) |
misodengaku | 0:a277e27599ee | 115 | { |
misodengaku | 1:36220c7411e9 | 116 | if (channel < 5) |
misodengaku | 0:a277e27599ee | 117 | { |
misodengaku | 1:36220c7411e9 | 118 | if (*((int32_t *)values) == 0) |
misodengaku | 1:36220c7411e9 | 119 | patlite_out[channel] = 0; |
misodengaku | 1:36220c7411e9 | 120 | else |
misodengaku | 1:36220c7411e9 | 121 | patlite_out[channel] = 1; |
misodengaku | 0:a277e27599ee | 122 | } |
misodengaku | 1:36220c7411e9 | 123 | changed = 1; |
misodengaku | 0:a277e27599ee | 124 | } |
misodengaku | 0:a277e27599ee | 125 | } |
misodengaku | 1:36220c7411e9 | 126 | return changed; |
misodengaku | 0:a277e27599ee | 127 | } |