Последняя активность 1 month ago

simple_display.ino Исходник
1#include <Adafruit_NeoPixel.h>
2#define PIN 6
3
4Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
5
6#define LARGURA 8
7
8uint32_t matrix[5][LARGURA];
9const float heartPeriod = 500;
10const int rainbowPeriod = 10;
11const float textCyclePeriod = 200;
12
13byte 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
21int stateMachine = 0;
22int textCycleCycles = 0;
23bool textCycleLock = true;
24uint32_t lastTime = 0;
25
26void 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
38inline byte breath(float period) {
39 return (byte) (127 * (sin(2 * PI * (1.0/period) * (millis()- lastTime) / PI) + 1));
40}
41
42inline 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
50inline 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
60inline void setColor(uint8_t x, uint8_t y, uint32_t color) {
61 strip.setPixelColor(x + y * LARGURA, color);
62}
63
64inline uint32_t proportional(uint32_t value, uint32_t intensity) {
65 return (intensity * value / 255) & 0xFF;
66}
67
68void 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
85uint32_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
98void loop() {
99 dotextCycle();
100 refreshMatrix();
101 delay(1);
102}