Skip to content

Instantly share code, notes, and snippets.

@michalmonday
Last active November 25, 2022 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalmonday/7ec808ec6048ac2ecbedcab2c72ff4a4 to your computer and use it in GitHub Desktop.
Save michalmonday/7ec808ec6048ac2ecbedcab2c72ff4a4 to your computer and use it in GitHub Desktop.
Extension board LEDs wiring fix
#include "mbed.h"
// mosi, miso (unused really), sclk
SPI sw(p5, p6, p7);
// lat (latch) pin of TLC59281 as shown in extension board schematic
DigitalOut lat(p8);
/* The extension board wiring from TLC59281 to 3 LEDs pairs are not consistent with
remaining 5 LEDs pairs (green is wired as red, red is wired as green).
This function fixes that problem. This way, all LEDs will turn red
when we send the following to TLC59281:
0b0101010101010101
(where each "01" is a 2-bit value controlling the state of green/red LED pair)
It switches position of bits in a number as shown in: https://stackoverflow.com/a/47990/4620679 */
unsigned short fix_extension_board_leds(unsigned short leds_state_bits) {
for (int i = 10; i < 16; i += 2) {
char bit1 = (leds_state_bits >> i) & 1;
char bit2 = (leds_state_bits >> i+1) & 1;
leds_state_bits ^= (-bit2 ^ leds_state_bits) & (1 << i);
leds_state_bits ^= (-bit1 ^ leds_state_bits) & (1 << i+1);
}
return leds_state_bits;
}
int main() {
lat = 0;
sw.format(16,0);
sw.frequency(1000000);
while(1) {
sw.write( fix_extension_board_leds(0b0101010101010101) ); // 0x5555
lat = 1;
lat = 0;
wait(1);
sw.write( fix_extension_board_leds(0b0000000000000000) ); // 0x0000
lat = 1;
lat = 0;
wait(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment