Minimal library to work with APA102-based LED strips. Uses code from FastLED library.

Dependents:   MobileArcade

Files at this revision

API Documentation at this revision

Comitter:
abraha2d
Date:
Tue Dec 11 06:29:34 2018 +0000
Commit message:
Initial commit

Changed in this revision

APA102.cpp Show annotated file Show diff for this revision Revisions of this file
APA102.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r c223864b981f APA102.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/APA102.cpp	Tue Dec 11 06:29:34 2018 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+#include "APA102.h"
+
+void fill_solid( struct CRGB * leds, int numToFill, const struct CRGB& color) {
+    for(int i = 0; i < numToFill; i++) {
+        leds[i] = color;
+    }
+}
+
+void APA102_write(mbed::SPI& mSPI, struct CRGB * leds, int numToFill) {
+    
+    mSPI.write(0);
+    mSPI.write(0);
+    mSPI.write(0);
+    mSPI.write(0);
+    
+    for (int i = 0; i < numToFill; ++i) {
+        mSPI.write(0xE0 | 31);
+        mSPI.write(leds[i].b);
+        mSPI.write(leds[i].g);
+        mSPI.write(leds[i].r);
+    }
+    
+    int nDWords = numToFill / 32;
+    do {
+        mSPI.write(0xFF);
+        mSPI.write(0x00);
+        mSPI.write(0x00);
+        mSPI.write(0x00);
+    } while(nDWords--); 
+    
+}
diff -r 000000000000 -r c223864b981f APA102.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/APA102.h	Tue Dec 11 06:29:34 2018 +0000
@@ -0,0 +1,53 @@
+#ifndef APA102_H_
+#define APA102_H_
+
+#include "mbed.h"
+
+struct CRGB {
+    
+    union {
+        struct {
+            union {
+                uint8_t r;
+                uint8_t red;
+            };
+            union {
+                uint8_t g;
+                uint8_t green;
+            };
+            union {
+                uint8_t b;
+                uint8_t blue;
+            };
+        };
+        uint8_t raw[3];
+    };
+    
+    inline CRGB() __attribute__((always_inline)) {}
+    
+    inline CRGB( uint8_t ir, uint8_t ig, uint8_t ib)  __attribute__((always_inline))
+        : r(ir), g(ig), b(ib) {}
+    
+    inline CRGB( uint32_t colorcode)  __attribute__((always_inline))
+        : r((colorcode >> 16) & 0xFF), g((colorcode >> 8) & 0xFF), b((colorcode >> 0) & 0xFF) {}
+    
+    typedef enum {
+        Black=0x000000,
+    } HTMLColorCode;
+    
+};
+
+inline __attribute__((always_inline)) bool operator== (const CRGB& lhs, const CRGB& rhs) {
+    return (lhs.r == rhs.r) && (lhs.g == rhs.g) && (lhs.b == rhs.b);
+}
+
+__attribute__((always_inline))
+inline CRGB operator*( const CRGB& p1, double d) {
+    return CRGB(p1.r * d, p1.g * d, p1.b * d);
+}
+
+void fill_solid( struct CRGB * leds, int numToFill, const struct CRGB& color);
+
+void APA102_write(mbed::SPI& mSPI, struct CRGB * leds, int numToFill);
+
+#endif
\ No newline at end of file