BERTL17 WS2812B Test

Dependencies:   mbed WS2812 PixelArray

Committer:
EliasN
Date:
Thu Apr 04 16:59:17 2019 +0000
Revision:
2:f85e3daacaa0
Parent:
1:c2876522bf56
Child:
3:8b5336449a8a
Mehr Kommentare

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EliasN 2:f85e3daacaa0 1 /*
EliasN 2:f85e3daacaa0 2 Name: RGBertil
EliasN 2:f85e3daacaa0 3 Beschreibung: WS2812B am Bertl über serielle Schnittstelle kontrollieren
EliasN 2:f85e3daacaa0 4 Autor: Elias Nestl
EliasN 2:f85e3daacaa0 5 Für: Bulme BERTL17
EliasN 2:f85e3daacaa0 6 Klasse: 3BHEL 2018/19
EliasN 2:f85e3daacaa0 7 */
EliasN 2:f85e3daacaa0 8
EliasN 0:2536675f1748 9 #include "mbed.h"
EliasN 0:2536675f1748 10 #include "stdlib.h"
EliasN 0:2536675f1748 11 #include "string"
EliasN 0:2536675f1748 12 #include "WS2812.h"
EliasN 0:2536675f1748 13 #include "PixelArray.h"
EliasN 0:2536675f1748 14
EliasN 0:2536675f1748 15 // Anzahl der LEDs
EliasN 0:2536675f1748 16 #define WS2812_BUF 3
EliasN 0:2536675f1748 17
EliasN 0:2536675f1748 18 // Serielle Verbindung mit PC herstellen
EliasN 0:2536675f1748 19 Serial pc(USBTX, USBRX, 9600);
EliasN 0:2536675f1748 20 // Buffer mit 3 Pixeln erstellen (vgl. Grafikspeicher einer Grafikkarte)
EliasN 0:2536675f1748 21 PixelArray px(WS2812_BUF);
EliasN 1:c2876522bf56 22 // WS2812 Libraryobjekt erstellen, an P1_22 hängen die LEDs, die letzten 4 Ziffern sind das Timing
EliasN 0:2536675f1748 23 WS2812 ws(P1_22, WS2812_BUF, 0, 5, 5, 0);
EliasN 0:2536675f1748 24
EliasN 0:2536675f1748 25 // Lesen einer Farbe aus dem Buffer
EliasN 0:2536675f1748 26 int getCol(int index) {
EliasN 0:2536675f1748 27 return px.getBuf()[index];
EliasN 0:2536675f1748 28 }
EliasN 0:2536675f1748 29
EliasN 0:2536675f1748 30 // Die Led an "index" auf "color" setzen, "color" im format 0xRRGGBB
EliasN 0:2536675f1748 31 void setCol(int index, unsigned int color) {
EliasN 0:2536675f1748 32 // Farbe in den Buffer setzen
EliasN 0:2536675f1748 33 px.Set(index, color);
EliasN 0:2536675f1748 34 // Buffer an LEDs sendern
EliasN 0:2536675f1748 35 ws.write(px.getBuf());
EliasN 0:2536675f1748 36 }
EliasN 0:2536675f1748 37
EliasN 0:2536675f1748 38 // Wie setCol, nur wechselnd (ein wenn aus, aus wenn ein)
EliasN 0:2536675f1748 39 void toggleCol(int index, unsigned int color) {
EliasN 0:2536675f1748 40 if (getCol(index) == 0) setCol(index, color);
EliasN 0:2536675f1748 41 else setCol(index, 0);
EliasN 0:2536675f1748 42 }
EliasN 0:2536675f1748 43
EliasN 0:2536675f1748 44 // String vom Serial Port lesen
EliasN 0:2536675f1748 45 string readString(char * prompt) {
EliasN 0:2536675f1748 46 string str = "";
EliasN 0:2536675f1748 47 char readc;
EliasN 0:2536675f1748 48 pc.printf("%s> ", prompt);
EliasN 0:2536675f1748 49 while (true) {
EliasN 0:2536675f1748 50 readc = pc.getc(); // Einen Charakter lesen
EliasN 1:c2876522bf56 51 if (readc == '\r' || readc == '\n') break; // Aufhören zu lesen bei Enter
EliasN 0:2536675f1748 52 pc.printf("%c", readc); // Rückgabe jedes Buchstabens
EliasN 0:2536675f1748 53 str += readc;
EliasN 0:2536675f1748 54 }
EliasN 0:2536675f1748 55 pc.printf("\n");
EliasN 0:2536675f1748 56 return str;
EliasN 0:2536675f1748 57 }
EliasN 0:2536675f1748 58
EliasN 0:2536675f1748 59 int main()
EliasN 0:2536675f1748 60 {
EliasN 0:2536675f1748 61 pc.printf("Hello world!\n");
EliasN 0:2536675f1748 62
EliasN 0:2536675f1748 63 unsigned int col = 0xff0000; // Standardfarbe = rot
EliasN 0:2536675f1748 64 string s;
EliasN 0:2536675f1748 65 while (true) {
EliasN 0:2536675f1748 66 char c = pc.getc(); // Charakter lesen
EliasN 2:f85e3daacaa0 67 pc.printf("-> %c\n", c); ); // Gelesenen Charakter ausgeben
EliasN 0:2536675f1748 68
EliasN 0:2536675f1748 69 switch (c) {
EliasN 0:2536675f1748 70 case '1': // Erste LED schalten
EliasN 0:2536675f1748 71 toggleCol(0, col);
EliasN 0:2536675f1748 72 break;
EliasN 0:2536675f1748 73 case '2': // Zweite LED schalten
EliasN 0:2536675f1748 74 toggleCol(1, col);
EliasN 0:2536675f1748 75 break;
EliasN 0:2536675f1748 76 case '3': // Dritte LED schalten
EliasN 0:2536675f1748 77 toggleCol(2, col);
EliasN 0:2536675f1748 78 break;
EliasN 0:2536675f1748 79 case 's': // Farbe ändern
EliasN 0:2536675f1748 80 s = readString("R"); // Rot einlesen
EliasN 2:f85e3daacaa0 81 col = atoi(s.c_str()) * 256 * 256; // String in integer wandeln und an richtige Stelle bitshiften
EliasN 0:2536675f1748 82
EliasN 0:2536675f1748 83 s = readString("G"); // Grün einlesen
EliasN 0:2536675f1748 84 col += atoi(s.c_str()) * 256;
EliasN 0:2536675f1748 85
EliasN 0:2536675f1748 86 s = readString("B"); // Blau einlesen
EliasN 0:2536675f1748 87 col += atoi(s.c_str());
EliasN 0:2536675f1748 88
EliasN 0:2536675f1748 89 pc.printf("Success!\n");
EliasN 0:2536675f1748 90 break;
EliasN 0:2536675f1748 91 default:
EliasN 0:2536675f1748 92 pc.printf("Unbekannter Befehl!\n");
EliasN 0:2536675f1748 93 break;
EliasN 0:2536675f1748 94 }
EliasN 0:2536675f1748 95 }
EliasN 0:2536675f1748 96 }