lkm1638-leds

Turns on the LEDs on the LKM1638 board.
Author: Jan Vantomme
License: BSD
Github: jv/lkm1638/lkm1638 leds.axo

Inlets

bool32 LED 1

bool32 LED 2

bool32 LED 3

bool32 LED 4

bool32 LED 5

bool32 LED 6

bool32 LED 7

bool32 LED 8

Outlets

None

Declaration
uint8_t *txbuf;
uint8_t *rxbuf;

bool led1;
bool led2;
bool led3;
bool led4;
bool led5;
bool led6;
bool led7;
bool led8;

// 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() {

  if (led1 == true) {
    tm1638_set_led(0, 2);
  } else {
    tm1638_set_led(0, 0);
  }

  if (led2 == true) {
    tm1638_set_led(1, 2);
  } else {
    tm1638_set_led(1, 0);
  }

  if (led3 == true) {
    tm1638_set_led(2, 2);
  } else {
    tm1638_set_led(2, 0);
  }

  if (led4 == true) {
    tm1638_set_led(3, 2);
  } else {
    tm1638_set_led(3, 0);
  }

  if (led5 == true) {
    tm1638_set_led(4, 2);
  } else {
    tm1638_set_led(4, 0);
  }

  if (led6 == true) {
    tm1638_set_led(5, 2);
  } else {
    tm1638_set_led(5, 0);
  }

  if (led7 == true) {
    tm1638_set_led(6, 2);
  } else {
    tm1638_set_led(6, 0);
  }

  if (led8 == 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;
Init
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
                        (void *)this);

led1 = false;
led2 = false;
led3 = false;
led4 = false;
led5 = false;
led6 = false;
led7 = false;
led8 = false;
Control Rate
led1 = inlet_led1;
led2 = inlet_led2;
led3 = inlet_led3;
led4 = inlet_led4;
led5 = inlet_led5;
led6 = inlet_led6;
led7 = inlet_led7;
led8 = inlet_led8;
Dispose
chThdTerminate(Thd);
chThdWait(Thd);

Privacy

© 2024 Zrna Research