Browse Source

implement our own WS2812 driver

Peter Cai 7 months ago
parent
commit
673060fbc2
Signed by: Peter Cai <[email protected]> GPG Key ID: 71F5FB4E4F3FD54F
5 changed files with 60 additions and 6 deletions
  1. 5
    1
      .vscode/settings.json
  2. 7
    4
      WTFRGB.ino
  3. 2
    1
      main.h
  4. 29
    0
      ws2812.cpp
  5. 17
    0
      ws2812.h

+ 5
- 1
.vscode/settings.json View File

@@ -1,7 +1,11 @@
1 1
 {
2 2
   "editor.tabSize": 2,
3
+  "[cpp]": {
4
+    "editor.tabSize": 2
5
+  },
3 6
   "C_Cpp.clang_format_fallbackStyle": "Google",
4 7
   "files.associations": {
5
-    "optional": "cpp"
8
+    "optional": "cpp",
9
+    "cstddef": "cpp"
6 10
   }
7 11
 }

+ 7
- 4
WTFRGB.ino View File

@@ -17,8 +17,10 @@ void setup() {
17 17
   Serial.begin(9600);
18 18
   for (int i = 0; i < NUM_LEDS; i++) {
19 19
     leds[i] = CRGB(0, 0, 0);
20
+    realLeds[i] = CRGB(0, 0, 0);
20 21
   }
21
-  FastLED.addLeds<WS2812, LED_PIN, GRB>(realLeds, NUM_LEDS);
22
+
23
+  pinMode(LED_PIN, OUTPUT);
22 24
   loadEEPROM(false);
23 25
 }
24 26
 
@@ -56,7 +58,8 @@ void loop() {
56 58
         CRGB(leds[i].r * brightness / 255, leds[i].g * brightness / 255,
57 59
              leds[i].b * brightness / 255);
58 60
   }
59
-  FastLED.show();
61
+
62
+  postLedUpdate(realLeds, NUM_LEDS, LED_PIN);
60 63
   delay(CYCLE);
61 64
 }
62 65
 
@@ -68,10 +71,10 @@ void processCommand() {
68 71
     if (len > 0) {
69 72
       // A command might be available to execute
70 73
       if (strncmp("MODE ", cmdBuf, 5) == 0) {
71
-        curEffect = atoi(&cmdBuf[5]);
74
+        curEffect = atoi(&cmdBuf[5]) % EFFECTS_NUM;
72 75
         EEPROM.put(EEPROM_MODE, (byte)curEffect);
73 76
       } else if (strncmp("BRIGHT ", cmdBuf, 7) == 0) {
74
-        brightness = atoi(&cmdBuf[7]);
77
+        brightness = atoi(&cmdBuf[7]) % 256;
75 78
         EEPROM.put(EEPROM_BRIGHTNESS, brightness);
76 79
       }
77 80
     }

+ 2
- 1
main.h View File

@@ -1,6 +1,7 @@
1 1
 #ifndef MAIN_H
2 2
 #define MAIN_H
3
-#include <FastLED.h>
3
+#include "ws2812.h"
4
+//#include <FastLED.h>
4 5
 #include "eeprom.h"
5 6
 
6 7
 #define MAX_BRIGHTNESS 255

+ 29
- 0
ws2812.cpp View File

@@ -0,0 +1,29 @@
1
+#include <Arduino.h>
2
+#include "ws2812.h"
3
+
4
+void postByte(unsigned char b, unsigned char portMask) {
5
+  for (int i = 7; i >= 0; i--) {
6
+    if ((b >> i) & 0x01) {
7
+      PORTD |= portMask;
8
+      __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
9
+      PORTD &= ~portMask;
10
+      __asm__("nop\n\t");
11
+    } else {
12
+      PORTD |= portMask;
13
+      __asm__("nop\n\t");
14
+      PORTD &= ~portMask;
15
+      __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
16
+    }
17
+  }
18
+}
19
+
20
+void postLedUpdate(CRGB *leds, int len, int ledPin) {
21
+  unsigned char portMask = 1 << ledPin;
22
+  cli();
23
+  for (int i = 0; i < len; i++) {
24
+    postByte(leds[i].g, portMask);
25
+    postByte(leds[i].r, portMask);
26
+    postByte(leds[i].b, portMask);
27
+  }
28
+  sei();
29
+}

+ 17
- 0
ws2812.h View File

@@ -0,0 +1,17 @@
1
+#ifndef WS2812_H
2
+#define WS2812_H
3
+
4
+class CRGB {
5
+  public:
6
+    unsigned char r, g, b;
7
+    CRGB() {};
8
+    CRGB(unsigned char r, unsigned char g, unsigned char b) {
9
+      this->r = r;
10
+      this->g = g;
11
+      this->b = b;
12
+    };
13
+};
14
+
15
+void postLedUpdate(CRGB *leds, int len, int ledPin);
16
+
17
+#endif

Loading…
Cancel
Save