This library lets you control the addressable RGB LED strips from Pololu Robotics. Forked to add selectable colour order (Support RGB or GRB Leds)

Fork of PololuLedStrip by David Grayson

Files at this revision

API Documentation at this revision

Comitter:
DavidEGrayson
Date:
Fri Mar 01 05:05:09 2013 +0000
Parent:
14:672baf3cf941
Child:
16:eaed541b08b0
Commit message:
Cleaned up calculateDelays more. The library might be done now.

Changed in this revision

PololuLedStrip.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/PololuLedStrip.cpp	Fri Mar 01 05:00:49 2013 +0000
+++ b/PololuLedStrip.cpp	Fri Mar 01 05:05:09 2013 +0000
@@ -2,47 +2,37 @@
 
 bool PololuLedStrip::interruptFriendly = false;
 
-// The three timed delays, in units of half-instructions.
+// The three timed delays, in units of half-cycles.
 uint8_t led_strip_write_delays[3];
 
 void PololuLedStrip::calculateDelays()
 {
-    // Get the clock frequency in MHz.
-    int f_mhz = SystemCoreClock / 1000000;
+    int f_mhz = SystemCoreClock / 1000000;   // Clock frequency in MHz.
 
     if (f_mhz <= 48)
     {
         // The delays below result in 800/1590 ns pulses and a 2500 ns period on the mbed NXP LPC11U24.        
         led_strip_write_delays[0] = 0;
         led_strip_write_delays[1] = 0;
-        led_strip_write_delays[2] = 5 << 1;
+        led_strip_write_delays[2] = 5;
     }
     else
     {
-        // Try to generally compute what the delays should be for any frequency clock.
+        // Try to generally compute what the delays should be for a ide range of clock frequencies.
         
         // The fudge factors below were experimentally chosen so that we would have
-        // 700/1300 ns pulses and a ~ 2500 ns period on the mbed NXP LPC1768 (96 MHz Cortex-M3).
+        // 700/1300 ns pulses and a ~2500 ns period on the mbed NXP LPC1768 (96 MHz Cortex-M3).
         // If you ever change these numbers, it is important to check the the subtractions below
-        // will not overflow in the worst case, which is f_mhz = 48.
-        static const uint8_t delay_fudges[] = { 23, 28, 23 };
-        
-        led_strip_write_delays[0] = 700*f_mhz/1000;
-        led_strip_write_delays[1] = 600*f_mhz/1000;
-        led_strip_write_delays[2] = 1200*f_mhz/1000;
-    
-        for(int i = 0; i < 3; i++)
-        {
-            if (led_strip_write_delays[i] < delay_fudges[i])
-            {
-                led_strip_write_delays[i] = 0;
-            }
-            else
-            {
-                led_strip_write_delays[i] -= delay_fudges[i];
-                led_strip_write_delays[i] <<= 1;
-            }
-        }
+        // will not overflow in the worst case, which is f_mhz = 49.
+        led_strip_write_delays[0] = 700*f_mhz/1000 - 23;
+        led_strip_write_delays[1] = 600*f_mhz/1000 - 28;
+        led_strip_write_delays[2] = 1200*f_mhz/1000 - 23;    
+    }
+ 
+    // Convert from units of cycles to units of half-cycles; it makes the assembly faster.   
+    for(int i = 0; i < 3; i++)
+    {
+        led_strip_write_delays[i] <<= 1;
     }
 }