From 03bd72808afc3d61f73734b2a710a7bcb647e7f0 Mon Sep 17 00:00:00 2001
From: Tangent 128 <Tangent128@gmail.com>
Date: Sun, 27 Oct 2019 02:08:48 -0400
Subject: [PATCH] refactor the heartbeat led logic into the driver

---
 hello_gradient/src/bin/halloween2019.rs | 12 +++---------
 hello_gradient/src/bin/july4.rs         | 12 +++---------
 itsybitsy_m0_lights/src/lib.rs          | 14 +++++++++++++-
 3 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/hello_gradient/src/bin/halloween2019.rs b/hello_gradient/src/bin/halloween2019.rs
index ad08957..63827b3 100644
--- a/hello_gradient/src/bin/halloween2019.rs
+++ b/hello_gradient/src/bin/halloween2019.rs
@@ -10,12 +10,12 @@ use lights::{
         Rgb
     }
 };
-use lights_hal::{boot, delay, entry, OutputPin};
+use lights_hal::{boot, delay, entry};
 
 #[entry]
 fn main() -> ! {
     let mut lights = boot();
-    lights.red_led.set_high().unwrap();
+    lights.heartbeat();
 
     let pattern =
         repeat(&Rgb(255,0,255)).take(150)
@@ -24,15 +24,9 @@ fn main() -> ! {
         ).cycle();
 
     let mut buffer = [HardwareRgb(255,255,0); 200];
-    let mut light = false;
     loop {
         // blink light to demonstrate loop is still running
-        if light {
-            lights.red_led.set_high().unwrap();
-        } else {
-            lights.red_led.set_low().unwrap();
-        }
-        light = !light;
+        lights.heartbeat();
 
         for (i, pixel) in pattern.clone().take(200).enumerate() {
             buffer[i] = correct(pixel);
diff --git a/hello_gradient/src/bin/july4.rs b/hello_gradient/src/bin/july4.rs
index f7af1f9..16b3543 100644
--- a/hello_gradient/src/bin/july4.rs
+++ b/hello_gradient/src/bin/july4.rs
@@ -10,12 +10,12 @@ use lights::{
         Rgb
     }
 };
-use lights_hal::{boot, delay, entry, OutputPin};
+use lights_hal::{boot, delay, entry};
 
 #[entry]
 fn main() -> ! {
     let mut lights = boot();
-    lights.red_led.set_high().unwrap();
+    lights.heartbeat();
 
     let mut pattern =
         repeat(&Rgb(255,0,0)).take(5)
@@ -34,15 +34,9 @@ fn main() -> ! {
         ).cycle();
 
     let mut buffer = [HardwareRgb(255,255,0); 200];
-    let mut light = false;
     //delay(48_000_000 * 3);
     loop {
-        if light {
-            lights.red_led.set_high().unwrap();
-        } else {
-            lights.red_led.set_low().unwrap();
-        }
-        light = !light;
+        lights.heartbeat();
 
         for (i, pixel) in pattern.clone().take(200).enumerate() {
             buffer[i] = correct(pixel);
diff --git a/itsybitsy_m0_lights/src/lib.rs b/itsybitsy_m0_lights/src/lib.rs
index aa3062a..9cd4099 100644
--- a/itsybitsy_m0_lights/src/lib.rs
+++ b/itsybitsy_m0_lights/src/lib.rs
@@ -37,6 +37,7 @@ pub fn boot() -> NeopixelLights<impl OutputPin<Error = ()>, impl OutputPin<Error
         out: pins.d7.into_push_pull_output(&mut pins.port),
         high_out: pins.d5.into_push_pull_output(&mut pins.port),
         red_led: pins.d13.into_open_drain_output(&mut pins.port),
+        debug_light: false,
     }
 }
 
@@ -58,7 +59,8 @@ const LATCH_CYCLES: u32 = 15000; // about 300us
 pub struct NeopixelLights<T: OutputPin, U: OutputPin, V: OutputPin> {
     pub out: T,
     pub high_out: U,
-    pub red_led: V
+    pub red_led: V,
+    pub debug_light: bool
 }
 
 impl<T: OutputPin, U: OutputPin, V: OutputPin> NeopixelLights<T, U, V>
@@ -88,6 +90,16 @@ where
         self.write(byte & 0x02 != 0);
         self.write(byte & 0x01 != 0);
     }
+
+    /// Blink light, probably to indicate a loop is still running
+    pub fn heartbeat(&mut self) {
+        if self.debug_light {
+            self.red_led.set_low().unwrap();
+        } else {
+            self.red_led.set_high().unwrap();
+        }
+        self.debug_light = !self.debug_light;
+    }
 }
 
 impl<T: OutputPin, U: OutputPin, V: OutputPin> Lights for NeopixelLights<T, U, V>