None
bool32 Button 1 Status
bool32 Button 2 Status
bool32 Button 3 Status
bool32 Button 4 Status
bool32 Button 5 Status
bool32 Button 6 Status
bool32 Button 7 Status
bool32 Button 8 Status
uint8_t *txbuf;
uint8_t *rxbuf;
bool btn1;
bool btn2;
bool btn3;
bool btn4;
bool btn5;
bool btn6;
bool btn7;
bool btn8;
// Send a command to the TM1638 Chip
// ------------------------------------
void tm1638_send_command(uint8_t x) {
SPID1.spi->CR1 |= SPI_CR1_BIDIOE;
spiSelect(&SPID1);
txbuf[0] = x;
spiSend(&SPID1, 1, &txbuf[0]);
spiUnselect(&SPID1);
}
// Send data to the TM1638 Chip
// ------------------------------------
void tm1638_send_data(uint8_t addr, uint8_t data) {
SPID1.spi->CR1 |= SPI_CR1_BIDIOE;
tm1638_send_command(0x44);
txbuf[0] = addr | 0xc0;
txbuf[1] = data;
spiSelect(&SPID1);
spiSend(&SPID1, 2, txbuf);
spiUnselect(&SPID1);
}
// Set the color for a single LED
// ------------------------------------
// 0 = OFF
// 1 = GREEN
// 2 = RED
void tm1638_set_led(uint8_t led, uint8_t cols) {
tm1638_send_data((led << 1) + 1, cols);
}
// SETUP
// ------------------
void setup() {
static uint8_t _txbuf[8] __attribute__((section(".sram2")));
static uint8_t _rxbuf[8] __attribute__((section(".sram2")));
txbuf = _txbuf;
rxbuf = _rxbuf;
}
// LOOP
// ------------------
void loop() {
chThdSleepMilliseconds(1);
txbuf[0] = 0x42;
txbuf[1] = 0;
txbuf[2] = 0;
txbuf[3] = 0;
txbuf[4] = 0;
txbuf[5] = 0;
txbuf[6] = 0;
txbuf[7] = 0;
spiSelect(&SPID1);
spiSend(&SPID1, 1, &txbuf[0]);
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT); // MOSI -> tristate
spiReceive(&SPID1, 4, &rxbuf[0]);
spiUnselect(&SPID1);
palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5)); // MOSI -> output
btn1 = rxbuf[0] & 0x01 ? true : false;
btn2 = rxbuf[1] & 0x01 ? true : false;
btn3 = rxbuf[2] & 0x01 ? true : false;
btn4 = rxbuf[3] & 0x01 ? true : false;
btn5 = rxbuf[0] & 0x10 ? true : false;
btn6 = rxbuf[1] & 0x10 ? true : false;
btn7 = rxbuf[2] & 0x10 ? true : false;
btn8 = rxbuf[3] & 0x10 ? true : false;
if (btn1 == true) {
tm1638_set_led(0, 2);
} else {
tm1638_set_led(0, 0);
}
if (btn2 == true) {
tm1638_set_led(1, 2);
} else {
tm1638_set_led(1, 0);
}
if (btn3 == true) {
tm1638_set_led(2, 2);
} else {
tm1638_set_led(2, 0);
}
if (btn4 == true) {
tm1638_set_led(3, 2);
} else {
tm1638_set_led(3, 0);
}
if (btn5 == true) {
tm1638_set_led(4, 2);
} else {
tm1638_set_led(4, 0);
}
if (btn6 == true) {
tm1638_set_led(5, 2);
} else {
tm1638_set_led(5, 0);
}
if (btn7 == true) {
tm1638_set_led(6, 2);
} else {
tm1638_set_led(6, 0);
}
if (btn8 == true) {
tm1638_set_led(7, 2);
} else {
tm1638_set_led(7, 0);
}
}
// ------------------------------------------------------------------------------------------------
// THREADS
// ------------------------------------------------------------------------------------------------
msg_t ThreadX2() {
setup();
while (!chThdShouldTerminate()) {
loop();
chThdSleepMilliseconds(1);
}
chThdExit((msg_t)0);
}
static msg_t ThreadX(void *arg) { ((attr_parent *)arg)->ThreadX2(); }
WORKING_AREA(waThreadX, 1024);
Thread *Thd;
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
(void *)this);
btn1 = false;
btn2 = false;
btn3 = false;
btn4 = false;
btn5 = false;
btn6 = false;
btn7 = false;
btn8 = false;
outlet_button1 = btn1;
outlet_button2 = btn2;
outlet_button3 = btn3;
outlet_button4 = btn4;
outlet_button5 = btn5;
outlet_button6 = btn6;
outlet_button7 = btn7;
outlet_button8 = btn8;
chThdTerminate(Thd);
chThdWait(Thd);