summary refs log tree commit diff
path: root/blade.cpp
blob: 381c1dc634aa75fcc43ab6357875f074fab36255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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();
  }
}