Forked PololuLedStrip and modified it to work with the KL25Z. Renamed it to Adafruit_NeoPixel.

Dependents:   idd_hw3 idd_fa15_hw3_lauren_bill_tomas idd_fa15_hw3_lauren_bill_tomas Raiden ... more

Fork of PololuLedStrip by David Grayson

Revision:
15:d69eebdee025
Parent:
14:672baf3cf941
Child:
16:eaed541b08b0
--- 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;
     }
 }