arduino rgb led strip driver
[clinton/scratch.git] / arduino-rgb-led / pwm_rgb_led_corrected.ino
diff --git a/arduino-rgb-led/pwm_rgb_led_corrected.ino b/arduino-rgb-led/pwm_rgb_led_corrected.ino
new file mode 100644 (file)
index 0000000..e9b3c5a
--- /dev/null
@@ -0,0 +1,100 @@
+/* -*- c -*- */
+/* Arduino rgb led driver */
+/* Copyright (c) 2014 Clinton Ebadi <clinton@unknownlamer.org> */
+/* This program is free software: you can redistribute it and/or modify */
+/* it under the terms of the GNU General Public License as published by */
+/* the Free Software Foundation, either version 3 of the License, or */
+/* (at your option) any later version. */
+
+/* This program is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
+/* GNU General Public License for more details. */
+
+/* You should have received a copy of the GNU General Public License */
+/* along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <avr/pgmspace.h>
+#include "gamma.h"
+
+const int dither_steps = pow (2, DITHER_BITS);
+
+typedef struct rgb_pins
+{
+    int red;
+    int green;
+    int blue;
+} 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];
+
+void set_color_x (int r, int g, int b, int dither_step = 0)
+{
+ int offset = dither_step * 256;
+
+ 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));
+ }
+}
+
+void setup ()
+{
+ Serial.begin (115200);
+ Serial.setTimeout (100000);
+
+ for (int bank = 0; bank < rgb_banks; bank++)
+ {
+   pinMode (pins[bank].red, OUTPUT);
+   pinMode (pins[bank].green, OUTPUT);
+   pinMode (pins[bank].blue, OUTPUT); 
+ }
+
+  // Test sequence to confirm wiring
+  set_color_x (255, 255, 255);
+  delay(500);
+  set_color_x (255, 0, 0);
+  delay (500);
+  set_color_x (0, 255, 0);
+  delay (500);
+  set_color_x (0, 0, 255);
+  delay (500);
+}
+
+void loop ()
+{
+  static int r = 192;
+  static int g = 192;
+  static int b = 192;
+  static int step = -1;
+  
+  //step = (step + 1) % dither_steps;
+  step = 0; // dithering is bogus/i'm-not-doing-it-right
+
+  if (Serial.available () >= 3)
+    {
+      r = Serial.read ();
+      g = Serial.read ();
+      b = Serial.read ();
+
+      Serial.print ("set color ");
+      Serial.print (r);
+      Serial.print (" ");
+      Serial.print (g);
+      Serial.print (" ");
+      Serial.print (b);
+      Serial.println ();
+      
+      set_color_x (r, g, b);
+    }
+
+  set_color_x (r, g, b, step);
+
+}