summary refs log tree commit diff
path: root/blade.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'blade.cpp')
-rw-r--r--blade.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/blade.cpp b/blade.cpp
new file mode 100644
index 0000000..381c1dc
--- /dev/null
+++ b/blade.cpp
@@ -0,0 +1,80 @@
+#include "blade.h"
+
+#define LED_PIN 6
+#define LED_COUNT 32
+
+CRGB blade[LED_COUNT];
+CHSV blade_color = CHSV(144, 255, 216);
+
+#define BLADE_OFF 0
+#define BLADE_ON 1
+#define BLADE_IGNITE 2
+#define BLADE_RETRACT 3
+uint8_t state = BLADE_OFF;
+
+#define IGNITE_MS 200
+#define RETRACT_MS 400
+uint32_t anim_start;
+
+void setup_blade() {
+  FastLED.addLeds<WS2812B, LED_PIN, GRB>(blade, LED_COUNT);
+}
+
+void draw_blade() {
+  int num_lit = LED_COUNT;
+
+  switch(state) {
+    case BLADE_OFF:
+      return;
+
+    case BLADE_ON:
+      if(blade[0] == blade_color) return;
+      break;
+
+    case BLADE_IGNITE:
+      num_lit = (millis() - anim_start) * LED_COUNT / IGNITE_MS;
+      if(num_lit >= LED_COUNT) {
+        state = BLADE_ON;
+        num_lit = LED_COUNT;
+      }
+      break;
+
+    case BLADE_RETRACT:
+      num_lit = LED_COUNT - (millis() - anim_start) * LED_COUNT / RETRACT_MS;
+      if(num_lit <= 0) {
+        state = BLADE_OFF;
+        num_lit = 0;
+      }
+      break;
+  }
+
+  for(int i = 0; i < num_lit; i++) {
+    blade[i] = CHSV(blade_color.h - i, blade_color.s - i - i, blade_color.v);
+  }
+  for(int i = num_lit; i < LED_COUNT; i++) {
+    blade[i] = CRGB::Black;
+  }
+  FastLED.show();
+}
+
+// good blade colors
+// skywalker blue (136, 255, 216)
+// blue-white (144, 128, 216)
+
+void change_hue(uint8_t d) {
+  blade_color.h += d;
+}
+
+void change_sat(uint8_t d) {
+  blade_color.s += d;
+}
+
+void toggle_blade() {
+  if(state == BLADE_OFF) {
+    state = BLADE_IGNITE;
+    anim_start = millis();
+  } else {
+    state = BLADE_RETRACT;
+    anim_start = millis();
+  }
+}
\ No newline at end of file