Convert arduino autonomous.pde to new TLC59116, single-TLC configuration

This commit is contained in:
Petr Baudis 2012-06-28 00:36:19 +02:00
parent 2763d82908
commit a78e231296
4 changed files with 79 additions and 29 deletions

View file

@ -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 autonomous.pde: This is autonomous LED controller that implements several
simple color cycling algorithms directly on Ardiuno - no host controller simple color cycling algorithms directly on Ardiuno - no host controller
is required. All LEDs have the same color. Useful for testing. 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.

View file

@ -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

View file

@ -1,8 +1,8 @@
#include "Tlc5940.h" #include "../ledbar.h"
#define CH 3 #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; unsigned int xr1 = 19543;
@ -13,27 +13,23 @@ int cpin[][CH] = {
{TLCCH(0, 12), TLCCH(0, 11),TLCCH(0, 10)}, {TLCCH(0, 12), TLCCH(0, 11),TLCCH(0, 10)},
{TLCCH(0, 15),TLCCH(0, 14),TLCCH(0, 13)}, {TLCCH(0, 15),TLCCH(0, 14),TLCCH(0, 13)},
#if 0
{TLCCH(1, 2), TLCCH(1, 1), TLCCH(1, 0)}, {TLCCH(1, 2), TLCCH(1, 1), TLCCH(1, 0)},
{TLCCH(1, 5), TLCCH(1, 4), TLCCH(1, 3)}, {TLCCH(1, 5), TLCCH(1, 4), TLCCH(1, 3)},
{TLCCH(1, 8), TLCCH(1, 7), TLCCH(1, 6)}, {TLCCH(1, 8), TLCCH(1, 7), TLCCH(1, 6)},
{TLCCH(1, 12), TLCCH(1, 11), TLCCH(1, 10)}, {TLCCH(1, 12), TLCCH(1, 11), TLCCH(1, 10)},
{TLCCH(1, 15), TLCCH(1, 14), TLCCH(1, 13)}, {TLCCH(1, 15), TLCCH(1, 14), TLCCH(1, 13)},
#endif
}; };
#define cpinsets (sizeof(cpin)/sizeof(cpin[0])) #define cpinsets (sizeof(cpin)/sizeof(cpin[0]))
/* cca 2.7ohm resistor per channel */ /* cca 2.7ohm resistor per channel */
int cmax[cpinsets][CH] = { int cmax[cpinsets][CH] = {
{ 1600, 4000, 2200 }, { 100, 250, 138 },
{ 1600, 4000, 2200 }, { 100, 250, 138 },
{ 1600, 4000, 2200 }, { 100, 250, 138 },
{ 1600, 3900, 2000 }, { 100, 240, 230 },
{ 1600, 3700, 3000 }, { 100, 230, 188 },
{ 1600, 4000, 2200 },
{ 1600, 4000, 2200 },
{ 1600, 3400, 3000 },
{ 1600, 4000, 2200 },
{ 1600, 3400, 3000 },
}; };
int c[cpinsets][CH]; int c[cpinsets][CH];
@ -42,13 +38,14 @@ int wait = 10;
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);
/* Call Tlc.init() to setup the tlc. Ledbar.begin(B1100000);
You can optionally pass an initial PWM value (0 - 4095) for all channels.*/
Tlc.init();
int i = 0, led = 0; int i = 0, led = 0;
for (led = 0; led < cpinsets; led++) for (led = 0; led < cpinsets; led++) {
for (i = 0; i < CH; i++) for (i = 0; i < CH; i++) {
c[led][i] = cmax[led][i] / 2; c[led][i] = cmax[led][i] / 2;
Ledbar.setPinMode(cpin[led][i], LPM_PWM);
}
}
xr1 += analogRead(0); xr1 += analogRead(0);
} }
@ -164,8 +161,6 @@ void grey(int led)
void loop() void loop()
{ {
Tlc.clear();
int led; int led;
for (led = 0; led < cpinsets; led++) { for (led = 0; led < cpinsets; led++) {
//random_walk(led); //random_walk(led);
@ -179,15 +174,11 @@ void loop()
for (i = 0; i < CH; i++) { for (i = 0; i < CH; i++) {
for (led = 0; led < cpinsets; led++) { 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(" "); //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.print(NUM_TLCS, DEC);
//Serial.println(); //Serial.println();
/* Tlc.update() sends the data to the TLCs. This is when the LEDs will
actually change. */
Tlc.update();
delay(wait); delay(wait);
} }

50
arduino/ledbar.h Normal file
View file

@ -0,0 +1,50 @@
/* Generic ledbar library taking care of I2C Arduino communication
* with TLC59116. */
#include <Wire.h>
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();
}