From: Clinton Ebadi Date: Wed, 26 Nov 2014 00:53:49 +0000 (-0500) Subject: led controller: port to teensy 3.1 X-Git-Url: http://git.hcoop.net/clinton/scratch.git/commitdiff_plain/e732331a7722e334a0f6adc36275206216dff56c led controller: port to teensy 3.1 - For now, only target the teensy 3.1. - Target full 16-bit pwm (not sure if clock set correctly for full use though) - Eliminate color correction code - Likely wrong anyway - Revert to simple text based protocol - Teensy I/O is buffered, this is easier - Blink random colors when no device has the serial port open --- diff --git a/arduino_rgb_led/pwm_rgb_led_corrected.ino b/arduino_rgb_led/arduino_rgb_led.ino similarity index 63% rename from arduino_rgb_led/pwm_rgb_led_corrected.ino rename to arduino_rgb_led/arduino_rgb_led.ino index e9b3c5a..44e6209 100644 --- a/arduino_rgb_led/pwm_rgb_led_corrected.ino +++ b/arduino_rgb_led/arduino_rgb_led.ino @@ -14,9 +14,8 @@ /* You should have received a copy of the GNU General Public License */ /* along with this program. If not, see . */ -#include -#include "gamma.h" - +#define DITHER_BITS 1 +const int led_color_max = pow (2, 16) - 1; const int dither_steps = pow (2, DITHER_BITS); typedef struct rgb_pins @@ -28,10 +27,10 @@ typedef struct rgb_pins const int rgb_banks = 2; -const rgb_pins pins[rgb_banks] = { {10, 9, 11}, - {5, 6, 3} }; - -static int led_colors[rgb_banks * 3]; +//const rgb_pins pins[rgb_banks] = { {10, 9, 11}, +// {5, 6, 3} }; +const rgb_pins pins[rgb_banks] = { {9, 6, 10}, + {4, 5, 3} }; void set_color_x (int r, int g, int b, int dither_step = 0) { @@ -39,16 +38,18 @@ void set_color_x (int r, int g, int b, int dither_step = 0) for (int bank = 0; bank < rgb_banks; bank++) { - analogWrite (pins[bank].red, pgm_read_byte_near (gammaTable + (r >= 0 ? offset : 0) + r)); - analogWrite (pins[bank].green, pgm_read_byte_near (gammaTable + (g >= 0 ? offset : 0) + g)); - analogWrite (pins[bank].blue, pgm_read_byte_near (gammaTable + (b >= 0 ? offset : 0) + b)); + analogWrite (pins[bank].red, r); + analogWrite (pins[bank].green, g); + analogWrite (pins[bank].blue, b); } } void setup () { Serial.begin (115200); - Serial.setTimeout (100000); + Serial.setTimeout (20000); + analogWriteResolution (16); + randomSeed (analogRead (14)); for (int bank = 0; bank < rgb_banks; bank++) { @@ -58,31 +59,31 @@ void setup () } // Test sequence to confirm wiring - set_color_x (255, 255, 255); + set_color_x (led_color_max, led_color_max, led_color_max); delay(500); - set_color_x (255, 0, 0); + set_color_x (led_color_max, 0, 0); delay (500); - set_color_x (0, 255, 0); + set_color_x (0, led_color_max, 0); delay (500); - set_color_x (0, 0, 255); + set_color_x (0, 0, led_color_max); delay (500); } void loop () { - static int r = 192; - static int g = 192; - static int b = 192; + static int r = led_color_max / 2; + static int g = led_color_max / 2; + static int b = led_color_max / 2; static int step = -1; //step = (step + 1) % dither_steps; step = 0; // dithering is bogus/i'm-not-doing-it-right - if (Serial.available () >= 3) + if (Serial.available ()) { - r = Serial.read (); - g = Serial.read (); - b = Serial.read (); + r = Serial.parseInt (); + g = Serial.parseInt (); + b = Serial.parseInt (); Serial.print ("set color "); Serial.print (r); @@ -95,6 +96,13 @@ void loop () set_color_x (r, g, b); } + // display random colors when the serial port is closed + if (!Serial.dtr ()) + { + set_color_x (random (led_color_max), random (led_color_max), random (led_color_max)); + delay (1500); + } + set_color_x (r, g, b, step); } diff --git a/led-controller.scm b/led-controller.scm index db55ac9..a417fe2 100644 --- a/led-controller.scm +++ b/led-controller.scm @@ -41,6 +41,7 @@ (define color-lock (make-mutex 'recursive)) (define current-color (vector 0.0 1.0 1.0)) +(define pwm-resolution (1- (expt 2 16))) (define serial-lock (make-mutex))