stackable sakura.io board for patlite

Dependencies:   SakuraIO mbed

Committer:
misodengaku
Date:
Wed Aug 09 06:21:04 2017 +0000
Revision:
0:a277e27599ee
Child:
1:36220c7411e9
release 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
misodengaku 0:a277e27599ee 1 #include "mbed.h"
misodengaku 0:a277e27599ee 2 #include "SakuraIO.h"
misodengaku 0:a277e27599ee 3
misodengaku 0:a277e27599ee 4 #define SEND_INTERVAL 5
misodengaku 0:a277e27599ee 5
misodengaku 0:a277e27599ee 6 DigitalOut status_led(PA_15);
misodengaku 0:a277e27599ee 7
misodengaku 0:a277e27599ee 8 DigitalOut patlite_1(PA_0); // Clear
misodengaku 0:a277e27599ee 9 DigitalOut patlite_2(PA_1); // Blue
misodengaku 0:a277e27599ee 10 DigitalOut patlite_3(PA_2); // Green
misodengaku 0:a277e27599ee 11 DigitalOut patlite_4(PA_3); // Yellow
misodengaku 0:a277e27599ee 12 DigitalOut patlite_5(PA_4); // Red
misodengaku 0:a277e27599ee 13
misodengaku 0:a277e27599ee 14 DigitalIn patlite_state_1(PA_12);
misodengaku 0:a277e27599ee 15 DigitalIn patlite_state_2(PA_8);
misodengaku 0:a277e27599ee 16 DigitalIn patlite_state_3(PA_7);
misodengaku 0:a277e27599ee 17 DigitalIn patlite_state_4(PA_6);
misodengaku 0:a277e27599ee 18 DigitalIn patlite_state_5(PA_5);
misodengaku 0:a277e27599ee 19
misodengaku 0:a277e27599ee 20 DigitalOut patlite_out[5] = {patlite_1, patlite_2, patlite_4, patlite_5, patlite_3};
misodengaku 0:a277e27599ee 21 // DigitalOut patlite_out[5] = {patlite_1, patlite_3, patlite_4, patlite_2, patlite_5};
misodengaku 0:a277e27599ee 22 DigitalIn patlite_state[5] = {patlite_state_1, patlite_state_2, patlite_state_4, patlite_state_5, patlite_state_3};
misodengaku 0:a277e27599ee 23
misodengaku 0:a277e27599ee 24 I2C i2c(PB_7, PB_6); // sda, scl
misodengaku 0:a277e27599ee 25 uint64_t counter = 0;
misodengaku 0:a277e27599ee 26 uint32_t iine_count = 0;
misodengaku 0:a277e27599ee 27 uint8_t blink_flag = 0;
misodengaku 0:a277e27599ee 28 uint8_t changed = 0;
misodengaku 0:a277e27599ee 29
misodengaku 0:a277e27599ee 30 uint8_t last_state[5] = {0};
misodengaku 0:a277e27599ee 31
misodengaku 0:a277e27599ee 32 //SakuraIO_SPI sakuraio(spi, cs);
misodengaku 0:a277e27599ee 33 SakuraIO_I2C sakuraio(i2c);
misodengaku 0:a277e27599ee 34
misodengaku 0:a277e27599ee 35 Ticker tick;
misodengaku 0:a277e27599ee 36
misodengaku 0:a277e27599ee 37 void timer()
misodengaku 0:a277e27599ee 38 {
misodengaku 0:a277e27599ee 39 uint8_t state[5];
misodengaku 0:a277e27599ee 40 __disable_irq();
misodengaku 0:a277e27599ee 41 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 42 {
misodengaku 0:a277e27599ee 43 state[i] = !patlite_state[i];
misodengaku 0:a277e27599ee 44 }
misodengaku 0:a277e27599ee 45
misodengaku 0:a277e27599ee 46 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 47 {
misodengaku 0:a277e27599ee 48 if (last_state[i] != state[i])
misodengaku 0:a277e27599ee 49 {
misodengaku 0:a277e27599ee 50 changed = 1;
misodengaku 0:a277e27599ee 51 //sakuraio.enqueueTx(i, state[i]);
misodengaku 0:a277e27599ee 52 status_led = !status_led;
misodengaku 0:a277e27599ee 53 last_state[i] = state[i];
misodengaku 0:a277e27599ee 54 }
misodengaku 0:a277e27599ee 55 }
misodengaku 0:a277e27599ee 56 __enable_irq();
misodengaku 0:a277e27599ee 57 }
misodengaku 0:a277e27599ee 58
misodengaku 0:a277e27599ee 59 int main()
misodengaku 0:a277e27599ee 60 {
misodengaku 0:a277e27599ee 61 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 62 {
misodengaku 0:a277e27599ee 63 patlite_state[i].mode(PullUp);
misodengaku 0:a277e27599ee 64 }
misodengaku 0:a277e27599ee 65
misodengaku 0:a277e27599ee 66 // Waiting to come online
misodengaku 0:a277e27599ee 67 int a = 0;
misodengaku 0:a277e27599ee 68 for (;;)
misodengaku 0:a277e27599ee 69 {
misodengaku 0:a277e27599ee 70 status_led = 1;
misodengaku 0:a277e27599ee 71 patlite_out[a] = 1;
misodengaku 0:a277e27599ee 72
misodengaku 0:a277e27599ee 73 if ((sakuraio.getConnectionStatus() & 0x80) == 0x80)
misodengaku 0:a277e27599ee 74 break;
misodengaku 0:a277e27599ee 75 wait_ms(500);
misodengaku 0:a277e27599ee 76 status_led = 0;
misodengaku 0:a277e27599ee 77 patlite_out[a] = 0;
misodengaku 0:a277e27599ee 78 a++;
misodengaku 0:a277e27599ee 79 if (a == 5)
misodengaku 0:a277e27599ee 80 a = 0;
misodengaku 0:a277e27599ee 81 wait_ms(500);
misodengaku 0:a277e27599ee 82 }
misodengaku 0:a277e27599ee 83 status_led = 1;
misodengaku 0:a277e27599ee 84 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 85 patlite_out[i] = 1;
misodengaku 0:a277e27599ee 86
misodengaku 0:a277e27599ee 87 wait(1);
misodengaku 0:a277e27599ee 88 status_led = 0;
misodengaku 0:a277e27599ee 89 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 90 patlite_out[i] = 0;
misodengaku 0:a277e27599ee 91
misodengaku 0:a277e27599ee 92 // tick.attach_us(&timer, 100 * 1000); // 100ms
misodengaku 0:a277e27599ee 93
misodengaku 0:a277e27599ee 94 while (1)
misodengaku 0:a277e27599ee 95 {
misodengaku 0:a277e27599ee 96 timer();
misodengaku 0:a277e27599ee 97
misodengaku 0:a277e27599ee 98 uint8_t avail = 0;
misodengaku 0:a277e27599ee 99 uint8_t queued = 0;
misodengaku 0:a277e27599ee 100
misodengaku 0:a277e27599ee 101 __disable_irq();
misodengaku 0:a277e27599ee 102 sakuraio.getRxQueueLength(&avail, &queued);
misodengaku 0:a277e27599ee 103 for (uint8_t i = 0; i < queued; i++)
misodengaku 0:a277e27599ee 104 {
misodengaku 0:a277e27599ee 105 uint8_t channel;
misodengaku 0:a277e27599ee 106 uint8_t type;
misodengaku 0:a277e27599ee 107 uint8_t values[8];
misodengaku 0:a277e27599ee 108 int64_t offset;
misodengaku 0:a277e27599ee 109
misodengaku 0:a277e27599ee 110 // RXキューから点灯要求を取り出す
misodengaku 0:a277e27599ee 111 uint8_t ret = sakuraio.dequeueRx(&channel, &type, values, &offset);
misodengaku 0:a277e27599ee 112 if (ret == 0x01)
misodengaku 0:a277e27599ee 113 {
misodengaku 0:a277e27599ee 114 if (channel >= 0 && channel < 5)
misodengaku 0:a277e27599ee 115 {
misodengaku 0:a277e27599ee 116 if (*((int32_t *)values) == 0)
misodengaku 0:a277e27599ee 117 patlite_out[channel] = 0;
misodengaku 0:a277e27599ee 118 else
misodengaku 0:a277e27599ee 119 patlite_out[channel] = 1;
misodengaku 0:a277e27599ee 120 }
misodengaku 0:a277e27599ee 121 changed = 1;
misodengaku 0:a277e27599ee 122 }
misodengaku 0:a277e27599ee 123 }
misodengaku 0:a277e27599ee 124
misodengaku 0:a277e27599ee 125 avail = 0;
misodengaku 0:a277e27599ee 126 queued = 0;
misodengaku 0:a277e27599ee 127 sakuraio.getTxQueueLength(&avail, &queued);
misodengaku 0:a277e27599ee 128 if (changed == 1)
misodengaku 0:a277e27599ee 129 {
misodengaku 0:a277e27599ee 130 uint8_t state[5];
misodengaku 0:a277e27599ee 131 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 132 {
misodengaku 0:a277e27599ee 133 state[i] = !patlite_state[i];
misodengaku 0:a277e27599ee 134 }
misodengaku 0:a277e27599ee 135
misodengaku 0:a277e27599ee 136 for (int i = 0; i < 5; i++)
misodengaku 0:a277e27599ee 137 {
misodengaku 0:a277e27599ee 138 sakuraio.enqueueTx(i, state[i]);
misodengaku 0:a277e27599ee 139 }
misodengaku 0:a277e27599ee 140 sakuraio.send();
misodengaku 0:a277e27599ee 141 changed = 0;
misodengaku 0:a277e27599ee 142 }
misodengaku 0:a277e27599ee 143 __enable_irq();
misodengaku 0:a277e27599ee 144
misodengaku 0:a277e27599ee 145 /*
misodengaku 0:a277e27599ee 146 sakuraio.clearTx();
misodengaku 0:a277e27599ee 147 sakuraio.enqueueTx(0, counter);
misodengaku 0:a277e27599ee 148 sakuraio.enqueueTx(1, iine_count);
misodengaku 0:a277e27599ee 149 sakuraio.send();
misodengaku 0:a277e27599ee 150 */
misodengaku 0:a277e27599ee 151
misodengaku 0:a277e27599ee 152 // wait_ms(10);
misodengaku 0:a277e27599ee 153 }
misodengaku 0:a277e27599ee 154 }