![](/media/cache/profiles/ef6fe50336005eee1e8e9093bfe576ae.jpg.50x50_q85.jpg)
stackable sakura.io board for patlite
Diff: main.cpp
- Revision:
- 0:a277e27599ee
- Child:
- 1:36220c7411e9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Aug 09 06:21:04 2017 +0000 @@ -0,0 +1,154 @@ +#include "mbed.h" +#include "SakuraIO.h" + +#define SEND_INTERVAL 5 + +DigitalOut status_led(PA_15); + +DigitalOut patlite_1(PA_0); // Clear +DigitalOut patlite_2(PA_1); // Blue +DigitalOut patlite_3(PA_2); // Green +DigitalOut patlite_4(PA_3); // Yellow +DigitalOut patlite_5(PA_4); // Red + +DigitalIn patlite_state_1(PA_12); +DigitalIn patlite_state_2(PA_8); +DigitalIn patlite_state_3(PA_7); +DigitalIn patlite_state_4(PA_6); +DigitalIn patlite_state_5(PA_5); + +DigitalOut patlite_out[5] = {patlite_1, patlite_2, patlite_4, patlite_5, patlite_3}; +// DigitalOut patlite_out[5] = {patlite_1, patlite_3, patlite_4, patlite_2, patlite_5}; +DigitalIn patlite_state[5] = {patlite_state_1, patlite_state_2, patlite_state_4, patlite_state_5, patlite_state_3}; + +I2C i2c(PB_7, PB_6); // sda, scl +uint64_t counter = 0; +uint32_t iine_count = 0; +uint8_t blink_flag = 0; +uint8_t changed = 0; + +uint8_t last_state[5] = {0}; + +//SakuraIO_SPI sakuraio(spi, cs); +SakuraIO_I2C sakuraio(i2c); + +Ticker tick; + +void timer() +{ + uint8_t state[5]; + __disable_irq(); + for (int i = 0; i < 5; i++) + { + state[i] = !patlite_state[i]; + } + + for (int i = 0; i < 5; i++) + { + if (last_state[i] != state[i]) + { + changed = 1; + //sakuraio.enqueueTx(i, state[i]); + status_led = !status_led; + last_state[i] = state[i]; + } + } + __enable_irq(); +} + +int main() +{ + for (int i = 0; i < 5; i++) + { + patlite_state[i].mode(PullUp); + } + + // Waiting to come online + int a = 0; + for (;;) + { + status_led = 1; + patlite_out[a] = 1; + + if ((sakuraio.getConnectionStatus() & 0x80) == 0x80) + break; + wait_ms(500); + status_led = 0; + patlite_out[a] = 0; + a++; + if (a == 5) + a = 0; + wait_ms(500); + } + status_led = 1; + for (int i = 0; i < 5; i++) + patlite_out[i] = 1; + + wait(1); + status_led = 0; + for (int i = 0; i < 5; i++) + patlite_out[i] = 0; + + // tick.attach_us(&timer, 100 * 1000); // 100ms + + while (1) + { + timer(); + + uint8_t avail = 0; + uint8_t queued = 0; + + __disable_irq(); + sakuraio.getRxQueueLength(&avail, &queued); + for (uint8_t i = 0; i < queued; i++) + { + uint8_t channel; + uint8_t type; + uint8_t values[8]; + int64_t offset; + + // RXキューから点灯要求を取り出す + uint8_t ret = sakuraio.dequeueRx(&channel, &type, values, &offset); + if (ret == 0x01) + { + if (channel >= 0 && channel < 5) + { + if (*((int32_t *)values) == 0) + patlite_out[channel] = 0; + else + patlite_out[channel] = 1; + } + changed = 1; + } + } + + avail = 0; + queued = 0; + sakuraio.getTxQueueLength(&avail, &queued); + if (changed == 1) + { + uint8_t state[5]; + for (int i = 0; i < 5; i++) + { + state[i] = !patlite_state[i]; + } + + for (int i = 0; i < 5; i++) + { + sakuraio.enqueueTx(i, state[i]); + } + sakuraio.send(); + changed = 0; + } + __enable_irq(); + + /* + sakuraio.clearTx(); + sakuraio.enqueueTx(0, counter); + sakuraio.enqueueTx(1, iine_count); + sakuraio.send(); + */ + +// wait_ms(10); + } +}