Davin Nicholas / Mbed OS NixieClock

Dependencies:   BufferedSerial

Files at this revision

API Documentation at this revision

Comitter:
abestat2
Date:
Fri Apr 20 01:52:39 2018 +0000
Parent:
0:b45298b78bbe
Commit message:
Changed Backlight from library to folder

Changed in this revision

Backlight.lib Show diff for this revision Revisions of this file
Backlight/Backlight.cpp Show annotated file Show diff for this revision Revisions of this file
Backlight/Backlight.h Show annotated file Show diff for this revision Revisions of this file
Backlight/ColorDefs.h Show annotated file Show diff for this revision Revisions of this file
--- a/Backlight.lib	Fri Apr 20 01:50:09 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-Backlight#0c1cdf6d98ad
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Backlight/Backlight.cpp	Fri Apr 20 01:52:39 2018 +0000
@@ -0,0 +1,79 @@
+#include "mbed.h"
+#include "Backlight.h"
+#include "ColorDefs.h"
+
+Backlight::Backlight(PinName mosiPin) : _bus(mosiPin,NC,D13) {
+    
+    _bus.format(8,1);
+    _bus.frequency(3333333);  // 1/0.3us (4 pixels per out clock)
+    _brightness = 1.0;
+    
+    _statusColor = COLOR_OFF;
+ 
+    CreateRawData();   
+    WritePixels();
+}
+
+void Backlight::UpdateBrightness(float brightness) {
+    _brightness = brightness;
+}
+
+void Backlight::UpdateBacklight(BacklightStatus status) {
+       
+    switch(status) {
+        case BACKLIGHT_OK:
+            _statusColor = COLOR_OK;            
+        break;
+        case BACKLIGHT_CONFIG:
+            _statusColor = COLOR_CONFIG;
+        break;
+        case BACKLIGHT_OFF:
+            _statusColor = COLOR_OFF;
+        break;
+        case BACKLIGHT_ERROR:
+        default:
+            _statusColor = COLOR_ERROR;
+        break;  
+    }
+    
+    CreateRawData();   
+    
+    WritePixels();
+}
+
+void Backlight::CreateRawData(void) {
+    // Encode '0' bits as 1000 and '1' bits as 1100.
+    // Rearrange this partern: abcdefgh into this: 1a001b0001c001d001e001f001g001h00
+    
+    // 255 - 0xCCCCCCCC; 0 - 0x88888888
+    
+    uint32_t pixelColor,base;
+    base = 0x88888888;
+    
+    for(int i = 0; i < NUM_PIXELS; i++) {
+        for(int j = 0; j < NUM_COLORS_PER_PIXEL; j++) {
+            uint32_t tempColor = (uint32_t)_statusColor[i][j]*_brightness;
+            pixelColor  = (tempColor << 26) & 0x40000000;
+            pixelColor |= (tempColor << 23) & 0x04000000;
+            pixelColor |= (tempColor << 20) & 0x00400000;
+            pixelColor |= (tempColor << 17) & 0x00040000;
+            pixelColor |= (tempColor << 14) & 0x00004000;
+            pixelColor |= (tempColor << 11) & 0x00000400;
+            pixelColor |= (tempColor << 8)  & 0x00000040;
+            pixelColor |= (tempColor << 5)  & 0x00000004;
+            pixelColor |= base;
+            // Invert signal since MOSI is idle high so idle can be 0
+            pixelColor = ~pixelColor;       
+            _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)]   = pixelColor & 0x000000FF;
+            _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)+1] = (pixelColor >> 8)  & 0x000000FF;
+            _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)+2] = (pixelColor >> 16) & 0x000000FF;
+            _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)+3] = (pixelColor >> 24) & 0x000000FF;
+        }
+    }
+}
+
+void Backlight::WritePixels(void) {
+    _bus.lock();
+    _bus.write((char*)_rawPixelData,TOTAL_PIXEL_BYTES,(char*)_rxBuffer,TOTAL_PIXEL_BYTES);
+    _bus.unlock();    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Backlight/Backlight.h	Fri Apr 20 01:52:39 2018 +0000
@@ -0,0 +1,53 @@
+#ifndef NEOPIXEL_H
+#define NEOPIXEL_H
+
+#include "mbed.h"
+
+#define NUM_PIXELS 5
+#define NUM_COLORS_PER_PIXEL 3
+#define NUM_BYTES_PER_COLOR 4
+#define TOTAL_PIXEL_BYTES (NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL*NUM_PIXELS)
+
+enum BacklightStatus {
+    BACKLIGHT_OK,
+    BACKLIGHT_ERROR,
+    BACKLIGHT_CONFIG,
+    BACKLIGHT_OFF
+};
+
+class Backlight
+{
+public:
+    /** Initialize a NeoPixel.
+     *
+     * @param mosiPin - transmit data on. Must be a SPI MOSI output
+     */
+    Backlight(PinName mosiPin);
+    
+    /** Updates the clock backlight status
+     *
+     *  @param status - sets the backlight based on predefined color pattern
+     *                  matched with the status.
+     */
+    void UpdateBacklight(BacklightStatus status);
+    
+    /** Updates the bightness of the backlight.
+     *
+     * @param birghtness - float of brightness from 0.0 to 1.0
+     *
+     */
+     void UpdateBrightness(float brightness);
+     
+private:
+    SPI _bus;
+    const uint8_t (*_statusColor)[NUM_COLORS_PER_PIXEL];
+    float _brightness;
+    uint8_t _rawPixelData[TOTAL_PIXEL_BYTES];
+    uint8_t _rxBuffer[TOTAL_PIXEL_BYTES];
+    
+    void WritePixels(void);
+    void CreateRawData(void);
+    void WriteComplete(void);    
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Backlight/ColorDefs.h	Fri Apr 20 01:52:39 2018 +0000
@@ -0,0 +1,28 @@
+#ifndef COLORDEFS_H
+#define COLORDEFS_H
+
+const uint8_t COLOR_OK[NUM_PIXELS][3] = {{0,255,0},
+                                         {0,255,0},
+                                         {0,255,0},
+                                         {0,255,0},
+                                         {0,255,0}};
+
+const uint8_t COLOR_ERROR[NUM_PIXELS][3] = {{255,0,0},
+                                            {255,0,0},
+                                            {255,0,0},
+                                            {255,0,0},
+                                            {255,0,0}};
+
+const uint8_t COLOR_CONFIG[NUM_PIXELS][3] = {{0,0,255},
+                                             {0,0,255},
+                                             {0,0,255},
+                                             {0,0,255},
+                                             {0,0,255}};
+
+const uint8_t COLOR_OFF[NUM_PIXELS][3] = {{0,0,0},
+                                          {0,0,0},
+                                          {0,0,0},
+                                          {0,0,0},
+                                          {0,0,0}};
+
+#endif
\ No newline at end of file