simple_display.ino
· 3.1 KiB · Arduino
Brut
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
#define LARGURA 8
uint32_t matrix[5][LARGURA];
const float heartPeriod = 500;
const int rainbowPeriod = 10;
const float textCyclePeriod = 200;
byte img[5][64] = {
{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},
{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},
{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},
{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},
{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}
};
int stateMachine = 0;
int textCycleCycles = 0;
bool textCycleLock = true;
uint32_t lastTime = 0;
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show(); // Initialize all pixels to 'off'
for (int i=0; i<40; i++) {
strip.setPixelColor(i, 0);
}
clearMatrix();
Serial.begin(115200);
}
inline byte breath(float period) {
return (byte) (127 * (sin(2 * PI * (1.0/period) * (millis()- lastTime) / PI) + 1));
}
inline void clearMatrix() {
for (int x=0;x<LARGURA;x++) {
for (int y=0;y<5;y++) {
matrix[y][x] = 0;
}
}
}
inline void refreshMatrix() {
// 5x8
for (int x=0;x<LARGURA;x++) {
for (int y=0;y<5;y++) {
setColor(x, y, matrix[y][x]);
}
}
strip.show();
}
inline void setColor(uint8_t x, uint8_t y, uint32_t color) {
strip.setPixelColor(x + y * LARGURA, color);
}
inline uint32_t proportional(uint32_t value, uint32_t intensity) {
return (intensity * value / 255) & 0xFF;
}
void dotextCycle() {
byte pos = ((int)((millis()- lastTime) / textCyclePeriod)) % (64 - LARGURA);
if (pos == 0 && !textCycleLock) {
textCycleCycles++;
textCycleLock = true;
} else if (pos != 0) {
textCycleLock = false;
}
for (int x=0; x<LARGURA; x++) {
for (int y=0; y<5; y++) {
matrix[y][x] = img[y][x+pos] ? Wheel((x+pos)*LARGURA, 64) : 0x000000;
}
}
}
uint32_t Wheel(byte WheelPos, byte intensity) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(proportional(255 - WheelPos * 3, intensity), 0, proportional(WheelPos * 3, intensity));
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity));
}
WheelPos -= 170;
return strip.Color(proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity), 0);
}
void loop() {
dotextCycle();
refreshMatrix();
delay(1);
}
| 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 | } |