From a78e231296b6a75d68f821eee6036a7994e9958e Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 28 Jun 2012 00:36:19 +0200 Subject: [PATCH] Convert arduino autonomous.pde to new TLC59116, single-TLC configuration --- arduino/README | 7 ++- arduino/autonomous/Makefile | 10 ++++ .../autonomous.ino} | 41 ++++++--------- arduino/ledbar.h | 50 +++++++++++++++++++ 4 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 arduino/autonomous/Makefile rename arduino/{autonomous.pde => autonomous/autonomous.ino} (86%) create mode 100644 arduino/ledbar.h diff --git a/arduino/README b/arduino/README index 9347987..e2caded 100644 --- a/arduino/README +++ b/arduino/README @@ -1,7 +1,6 @@ -These sketches require the Tlc5940 library, with NUM_TLCS set to 23. -You may also need to reduce HardwareSerial RX_BUFFER_SIZE to something -like 16 in order to free up more RAM. - autonomous.pde: This is autonomous LED controller that implements several simple color cycling algorithms directly on Ardiuno - no host controller is required. All LEDs have the same color. Useful for testing. + +controlled.pde: Listens to a byte stream on serial. The byte stream should +contain one R,G,B tribyte per LED, sent repeatedly. diff --git a/arduino/autonomous/Makefile b/arduino/autonomous/Makefile new file mode 100644 index 0000000..c45e5fe --- /dev/null +++ b/arduino/autonomous/Makefile @@ -0,0 +1,10 @@ +ARDUINO_DIR = /usr/share/arduino + +BOARD_TAG = pro5v +ARDUINO_PORT = /dev/ttyUSB0 + +TARGET = autonomous + +ARDUINO_LIBS = Wire Wire/utility + +include /usr/share/arduino/Arduino.mk diff --git a/arduino/autonomous.pde b/arduino/autonomous/autonomous.ino similarity index 86% rename from arduino/autonomous.pde rename to arduino/autonomous/autonomous.ino index 787bac2..415b3ff 100644 --- a/arduino/autonomous.pde +++ b/arduino/autonomous/autonomous.ino @@ -1,8 +1,8 @@ -#include "Tlc5940.h" +#include "../ledbar.h" #define CH 3 -#define TLCCH(tlc_num, ch_num) ((tlc_num)*16 + (ch_num)) +#define TLCCH(tlc_num, ch_num) (ch_num) /* FIXME */ unsigned int xr1 = 19543; @@ -13,27 +13,23 @@ int cpin[][CH] = { {TLCCH(0, 12), TLCCH(0, 11),TLCCH(0, 10)}, {TLCCH(0, 15),TLCCH(0, 14),TLCCH(0, 13)}, +#if 0 {TLCCH(1, 2), TLCCH(1, 1), TLCCH(1, 0)}, {TLCCH(1, 5), TLCCH(1, 4), TLCCH(1, 3)}, {TLCCH(1, 8), TLCCH(1, 7), TLCCH(1, 6)}, {TLCCH(1, 12), TLCCH(1, 11), TLCCH(1, 10)}, {TLCCH(1, 15), TLCCH(1, 14), TLCCH(1, 13)}, +#endif }; #define cpinsets (sizeof(cpin)/sizeof(cpin[0])) /* cca 2.7ohm resistor per channel */ int cmax[cpinsets][CH] = { - { 1600, 4000, 2200 }, - { 1600, 4000, 2200 }, - { 1600, 4000, 2200 }, - { 1600, 3900, 2000 }, - { 1600, 3700, 3000 }, - - { 1600, 4000, 2200 }, - { 1600, 4000, 2200 }, - { 1600, 3400, 3000 }, - { 1600, 4000, 2200 }, - { 1600, 3400, 3000 }, + { 100, 250, 138 }, + { 100, 250, 138 }, + { 100, 250, 138 }, + { 100, 240, 230 }, + { 100, 230, 188 }, }; int c[cpinsets][CH]; @@ -42,13 +38,14 @@ int wait = 10; void setup() { Serial.begin(9600); - /* Call Tlc.init() to setup the tlc. - You can optionally pass an initial PWM value (0 - 4095) for all channels.*/ - Tlc.init(); + Ledbar.begin(B1100000); int i = 0, led = 0; - for (led = 0; led < cpinsets; led++) - for (i = 0; i < CH; i++) + for (led = 0; led < cpinsets; led++) { + for (i = 0; i < CH; i++) { c[led][i] = cmax[led][i] / 2; + Ledbar.setPinMode(cpin[led][i], LPM_PWM); + } + } xr1 += analogRead(0); } @@ -164,8 +161,6 @@ void grey(int led) void loop() { - Tlc.clear(); - int led; for (led = 0; led < cpinsets; led++) { //random_walk(led); @@ -179,15 +174,11 @@ void loop() for (i = 0; i < CH; i++) { for (led = 0; led < cpinsets; led++) { //Serial.print(cpin[led][i], DEC); Serial.print("="); Serial.print(c[led][i], DEC); Serial.print("/"); Serial.print(cmax[led][i], DEC); Serial.print(" "); - Tlc.set(cpin[led][i], c[led][i]); + Ledbar.setPinPWM(cpin[led][i], c[led][i]); } } //Serial.print(NUM_TLCS, DEC); //Serial.println(); - /* Tlc.update() sends the data to the TLCs. This is when the LEDs will - actually change. */ - Tlc.update(); - delay(wait); } diff --git a/arduino/ledbar.h b/arduino/ledbar.h new file mode 100644 index 0000000..2420ba0 --- /dev/null +++ b/arduino/ledbar.h @@ -0,0 +1,50 @@ +/* Generic ledbar library taking care of I2C Arduino communication + * with TLC59116. */ + +#include + +enum LedbarPinMode { + LPM_OFF, + LPM_ON, + LPM_PWM, + LPM_GRPPWM, +}; + +class Ledbar { + public: + void begin(unsigned char address); /* 7-bit TLC address */ + /* pin is 0 .. 15 */ + void setPinMode(int pin, enum LedbarPinMode pinMode); + void setPinPWM(int pin, unsigned char dutyCycle); + private: + unsigned char address; + unsigned char rawPinModes[4]; +} Ledbar; + +void Ledbar::begin(unsigned char address_) +{ + address = address_; + memset(&rawPinModes, 0, sizeof(rawPinModes)); + + Wire.begin(); + Wire.beginTransmission(address); + Wire.write(0x00); // reg 0 + Wire.write(0x01); // broadcast on, [5bit]=0 turns on oscillator + Wire.endTransmission(); +} + +void Ledbar::setPinMode(int pin, enum LedbarPinMode pinMode) +{ + Wire.beginTransmission(address); + rawPinModes[pin / 4] &= ~(0x3 << (pin % 4 * 2)); + rawPinModes[pin / 4] |= (pinMode << (pin % 4 * 2)); + Wire.write(0x14 + pin / 4); Wire.write(rawPinModes[pin / 4]); + Wire.endTransmission(); +} + +void Ledbar::setPinPWM(int pin, unsigned char dutyCycle) +{ + Wire.beginTransmission(address); + Wire.write(0x2 + pin); Wire.write(dutyCycle); + Wire.endTransmission(); +}