Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: LedArrayController.h
- Revision:
- 1:0a051df78be2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LedArrayController.h Wed May 25 03:42:25 2016 +0000
@@ -0,0 +1,82 @@
+
+#define NLED (8)
+#define COLOR_WHITE 0xFFFFFF
+#define COLOR_BLACK 0x000000
+
+extern void AllOneColorLeds(neopixel::Pixel * out, uint32_t index, uintptr_t color);
+
+class LedArrayController {
+public:
+ LedArrayController(BusIn *bus, neopixel::PixelArray *array) : mBus(bus), mArray(array)
+ {
+ mBus->mode(PullUp);
+
+ }
+ void Stop()
+ {
+ mTimer.detach();
+ }
+ void OnChangeMode(uint32_t baseColor)
+ {
+ // 000 001 010 011 100 101 110, 111
+ const float mBlinkTiming[] = {1.0, 0.1, 0.5, 0.0, 2.0, 0.0, 0.0, 5.0};
+ mBaseColor = baseColor;
+ mIsBaseColor = false;
+ mBehavior.value = *mBus;
+
+ Update();
+
+ mTiming = mBlinkTiming[mBehavior.timingIndex];
+ if (mTiming > 0.0f)
+ {
+ mTimer.attach(this, &LedArrayController::Update, mTiming);
+ } else {
+ mTimer.detach();
+ }
+ }
+ void Update() {
+ // called by timer for updating LEDs:
+ if (mIsBaseColor)
+ {
+ switch (mBehavior.type) {
+ case (BLINK_TO_WHITE):
+ mArray->update(AllOneColorLeds, NLED, COLOR_WHITE);
+ break;
+ case (BLINK_TO_BLACK):
+ mArray->update(AllOneColorLeds, NLED, COLOR_BLACK);
+ break;
+ case (BLINK_TO_RANDOM):
+ mArray->update(AllOneColorLeds, NLED, (rand() & COLOR_WHITE));
+ break;
+ case (BLINK_TO_OPPOSITE):
+ mArray->update(AllOneColorLeds, NLED, ~mBaseColor);
+ break;
+ }
+ mIsBaseColor = false;
+
+ } else {
+ // set to base color
+ mArray->update(AllOneColorLeds, NLED, mBaseColor);
+ mIsBaseColor = true;
+ }
+ mTimer.attach(this, &LedArrayController::Update, mTiming);
+
+ }
+private:
+ float mTiming;
+ BusIn *mBus;
+ neopixel::PixelArray *mArray;
+ Timeout mTimer;
+ bool mIsBaseColor;
+ uint32_t mBaseColor;
+ typedef enum {BLINK_TO_WHITE=0, BLINK_TO_BLACK=1, BLINK_TO_RANDOM=2, BLINK_TO_OPPOSITE=3 } eBlinkType;
+ union {
+ struct {
+ uint32_t timingIndex:3;
+ uint32_t type:2;
+ uint32_t junk:28;
+ };
+ uint32_t value;
+ } mBehavior;
+};
+