最后活跃于 1 month ago

racerxdl's Avatar Lucas Teske 修订了这个 Gist 9 years ago. 转到此修订

1 file changed, 102 insertions

simple_display.ino(文件已创建)

@@ -0,0 +1,102 @@
1 + #include <Adafruit_NeoPixel.h>
2 + #define PIN 6
3 +
4 + Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
5 +
6 + #define LARGURA 8
7 +
8 + uint32_t matrix[5][LARGURA];
9 + const float heartPeriod = 500;
10 + const int rainbowPeriod = 10;
11 + const float textCyclePeriod = 200;
12 +
13 + byte img[5][64] = {
14 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
15 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
16 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
17 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
18 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
19 + };
20 +
21 + int stateMachine = 0;
22 + int textCycleCycles = 0;
23 + bool textCycleLock = true;
24 + uint32_t lastTime = 0;
25 +
26 + void setup() {
27 + // put your setup code here, to run once:
28 +
29 + strip.begin();
30 + strip.show(); // Initialize all pixels to 'off'
31 + for (int i=0; i<40; i++) {
32 + strip.setPixelColor(i, 0);
33 + }
34 + clearMatrix();
35 + Serial.begin(115200);
36 + }
37 +
38 + inline byte breath(float period) {
39 + return (byte) (127 * (sin(2 * PI * (1.0/period) * (millis()- lastTime) / PI) + 1));
40 + }
41 +
42 + inline void clearMatrix() {
43 + for (int x=0;x<LARGURA;x++) {
44 + for (int y=0;y<5;y++) {
45 + matrix[y][x] = 0;
46 + }
47 + }
48 + }
49 +
50 + inline void refreshMatrix() {
51 + // 5x8
52 + for (int x=0;x<LARGURA;x++) {
53 + for (int y=0;y<5;y++) {
54 + setColor(x, y, matrix[y][x]);
55 + }
56 + }
57 + strip.show();
58 + }
59 +
60 + inline void setColor(uint8_t x, uint8_t y, uint32_t color) {
61 + strip.setPixelColor(x + y * LARGURA, color);
62 + }
63 +
64 + inline uint32_t proportional(uint32_t value, uint32_t intensity) {
65 + return (intensity * value / 255) & 0xFF;
66 + }
67 +
68 + void dotextCycle() {
69 + byte pos = ((int)((millis()- lastTime) / textCyclePeriod)) % (64 - LARGURA);
70 + if (pos == 0 && !textCycleLock) {
71 + textCycleCycles++;
72 + textCycleLock = true;
73 + } else if (pos != 0) {
74 + textCycleLock = false;
75 + }
76 +
77 + for (int x=0; x<LARGURA; x++) {
78 + for (int y=0; y<5; y++) {
79 + matrix[y][x] = img[y][x+pos] ? Wheel((x+pos)*LARGURA, 64) : 0x000000;
80 + }
81 + }
82 + }
83 +
84 +
85 + uint32_t Wheel(byte WheelPos, byte intensity) {
86 + WheelPos = 255 - WheelPos;
87 + if(WheelPos < 85) {
88 + return strip.Color(proportional(255 - WheelPos * 3, intensity), 0, proportional(WheelPos * 3, intensity));
89 + }
90 + if(WheelPos < 170) {
91 + WheelPos -= 85;
92 + return strip.Color(0, proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity));
93 + }
94 + WheelPos -= 170;
95 + return strip.Color(proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity), 0);
96 + }
97 +
98 + void loop() {
99 + dotextCycle();
100 + refreshMatrix();
101 + delay(1);
102 + }
上一页 下一页