| 1 | #ifndef TILTDEMO_H |
|---|
| 2 | #define TILTDEMO_H |
|---|
| 3 | |
|---|
| 4 | #include "Demo.h" |
|---|
| 5 | |
|---|
| 6 | // Define range of possible acceleration values. |
|---|
| 7 | #define MIN_ACCEL -10.0 |
|---|
| 8 | #define MAX_ACCEL 10.0 |
|---|
| 9 | |
|---|
| 10 | // Define range of colors (min color and max color) using their red, green, blue components. |
|---|
| 11 | // First the min color: |
|---|
| 12 | #define MIN_COLOR_RED 0xFF |
|---|
| 13 | #define MIN_COLOR_GREEN 0x00 |
|---|
| 14 | #define MIN_COLOR_BLUE 0x00 |
|---|
| 15 | |
|---|
| 16 | // Then the max color: |
|---|
| 17 | #define MAX_COLOR_RED 0x00 |
|---|
| 18 | #define MAX_COLOR_GREEN 0x00 |
|---|
| 19 | #define MAX_COLOR_BLUE 0xFF |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | class TiltDemo: public Demo { |
|---|
| 23 | public: |
|---|
| 24 | TiltDemo() { mode = 0; } |
|---|
| 25 | ~TiltDemo() {} |
|---|
| 26 | |
|---|
| 27 | virtual void loop() { |
|---|
| 28 | // Grab the acceleration for the current mode's axis. |
|---|
| 29 | float accel = 0; |
|---|
| 30 | switch (mode) { |
|---|
| 31 | case 0: |
|---|
| 32 | accel = CircuitPlayground.motionX(); |
|---|
| 33 | break; |
|---|
| 34 | case 1: |
|---|
| 35 | accel = CircuitPlayground.motionY(); |
|---|
| 36 | break; |
|---|
| 37 | case 2: |
|---|
| 38 | accel = CircuitPlayground.motionZ(); |
|---|
| 39 | break; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // Now interpolate the acceleration into a color for the pixels. |
|---|
| 43 | uint8_t red = (uint8_t)lerp(accel, MIN_ACCEL, MAX_ACCEL, MIN_COLOR_RED, MAX_COLOR_RED); |
|---|
| 44 | uint8_t green = (uint8_t)lerp(accel, MIN_ACCEL, MAX_ACCEL, MIN_COLOR_GREEN, MAX_COLOR_GREEN); |
|---|
| 45 | uint8_t blue = (uint8_t)lerp(accel, MIN_ACCEL, MAX_ACCEL, MIN_COLOR_BLUE, MAX_COLOR_BLUE); |
|---|
| 46 | |
|---|
| 47 | // Gamma correction makes LED brightness appear more linear |
|---|
| 48 | red = CircuitPlayground.gamma8(red); |
|---|
| 49 | green = CircuitPlayground.gamma8(green); |
|---|
| 50 | blue = CircuitPlayground.gamma8(blue); |
|---|
| 51 | |
|---|
| 52 | // Light up all the pixels the interpolated color. |
|---|
| 53 | for (int i=0; i<10; ++i) { |
|---|
| 54 | CircuitPlayground.strip.setPixelColor(i, red, green, blue); |
|---|
| 55 | } |
|---|
| 56 | CircuitPlayground.strip.show(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | virtual void modePress() { |
|---|
| 60 | // Change the mode (axis being displayed) to a value inside 0-2 for X, Y, Z. |
|---|
| 61 | mode += 1; |
|---|
| 62 | if (mode > 2) { |
|---|
| 63 | mode = 0; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | private: |
|---|
| 68 | int mode; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | #endif |
|---|