175 lines
No EOL
4.6 KiB
C++
175 lines
No EOL
4.6 KiB
C++
#include <cstdint>
|
|
#include <Arduino.h>
|
|
#include <FreeRTOS.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_NeoPixel.h>
|
|
#include <U8x8lib.h>
|
|
|
|
#define PIXEL_COUNT 124
|
|
#define PIXEL_PIN 16
|
|
#define PIXEL_BRIGHTNESS 10
|
|
#define SCREEN_RESET_PIN 22
|
|
//#define SCREEN_RESET_PIN U8X8_PIN_NONE
|
|
|
|
U8X8_SSD1306_72X40_ER_HW_I2C u8x8(SCREEN_RESET_PIN);
|
|
|
|
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
|
|
|
|
int count = 0;
|
|
|
|
void rainbow(int wait) {
|
|
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
|
|
// apply rainbow to the first 64 pixels
|
|
for(int i=0; i<=63; i++) {
|
|
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
|
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
|
|
}
|
|
|
|
// Blank everything over pixel 64
|
|
//for(int j=65; j<=124; j++) strip.setPixelColor(j-1, strip.Color(0,0,0));
|
|
// 65->72 to mirror 4
|
|
for(int j=65; j<=72; j++) strip.setPixelColor(j-1, strip.getPixelColor(4-1));
|
|
// 73->79 to mirror 12
|
|
for(int j=73; j<=79; j++) strip.setPixelColor(j-1, strip.getPixelColor(12-1));
|
|
// 80->86 to mirror 20
|
|
for(int j=80; j<=86; j++) strip.setPixelColor(j-1, strip.getPixelColor(20-1));
|
|
// 87->94 to mirror 28
|
|
for(int j=87; j<=94; j++) strip.setPixelColor(j-1, strip.getPixelColor(28-1));
|
|
// 95->102 to mirror 36
|
|
for(int j=95; j<=102; j++) strip.setPixelColor(j-1, strip.getPixelColor(36-1));
|
|
// 103->109 to mirror 44
|
|
for(int j=103; j<=109; j++) strip.setPixelColor(j-1, strip.getPixelColor(44-1));
|
|
// 110->116 to mirror 52
|
|
for(int j=110; j<=116; j++) strip.setPixelColor(j-1, strip.getPixelColor(52-1));
|
|
// 117->124 to mirror 60
|
|
for(int j=117; j<=124; j++) strip.setPixelColor(j-1, strip.getPixelColor(60-1));
|
|
|
|
// Show and wait
|
|
strip.show();
|
|
delay(wait);
|
|
}
|
|
|
|
}
|
|
void all(uint8_t red, uint8_t green, uint8_t blue){
|
|
for(int i=0; i<strip.numPixels(); i++) {
|
|
strip.setPixelColor(i, strip.Color(red,green,blue));
|
|
}
|
|
strip.show();
|
|
}
|
|
void i2cScan(){
|
|
byte error, address;
|
|
int nDevices;
|
|
|
|
Serial.println("i2c Scan: Scanning bus for devices...");
|
|
|
|
nDevices = 0;
|
|
for(address = 1; address < 127; address++ )
|
|
{
|
|
// The i2c_scanner uses the return value of
|
|
// the Write.endTransmisstion to see if
|
|
// a device did acknowledge to the address.
|
|
Wire.beginTransmission(address);
|
|
error = Wire.endTransmission();
|
|
|
|
if (error == 0)
|
|
{
|
|
Serial.print("i2c Scan: I2C device found at address 0x");
|
|
if (address<16)
|
|
Serial.print("0");
|
|
Serial.print(address,HEX);
|
|
Serial.println(" !");
|
|
|
|
nDevices++;
|
|
}
|
|
else if (error==4)
|
|
{
|
|
Serial.print("i2c Scan: Unknown error at address 0x");
|
|
if (address<16)
|
|
Serial.print("0");
|
|
Serial.println(address,HEX);
|
|
}
|
|
}
|
|
if (nDevices == 0)
|
|
Serial.println("i2c Scan: No I2C devices found");
|
|
else
|
|
Serial.println("i2c Scan: Complete");
|
|
|
|
}
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(2000);
|
|
|
|
// Delay startup
|
|
Serial.println("Early startup");
|
|
|
|
// Initialise i2c
|
|
Wire.begin();
|
|
// Initialise Neopixels
|
|
strip.begin();
|
|
Serial.print("Setting Neopixel brightness to ");
|
|
Serial.print(PIXEL_BRIGHTNESS);
|
|
Serial.println("%");
|
|
strip.setBrightness(PIXEL_BRIGHTNESS);
|
|
all(10,10,10);
|
|
strip.show();
|
|
|
|
// Run i2cScan
|
|
i2cScan();
|
|
|
|
// Initialise oled display using internal VCC pump
|
|
Serial.print("Configuring display... ");
|
|
|
|
u8x8.begin();
|
|
u8x8.setPowerSave(false); // Power save keeps the controller alive but turns off the pixels
|
|
u8x8.setFlipMode(true);
|
|
|
|
u8x8.setFont(u8x8_font_5x7_f);
|
|
u8x8.drawString(0,0,"123456789");
|
|
u8x8.drawString(0,1,"abcdefghi");
|
|
u8x8.drawString(0,2,"ABCDEFGHI");
|
|
u8x8.drawString(0,3,"2020-12-01");
|
|
u8x8.drawString(0,4,"12:00:00");
|
|
u8x8.drawString(0,5,"---------");
|
|
u8x8.drawString(0,6,"*********");
|
|
delay(300);
|
|
u8x8.fillDisplay();
|
|
delay(300);
|
|
u8x8.clearDisplay();
|
|
Serial.println("Complete!");
|
|
}
|
|
|
|
void loop() {
|
|
count++;
|
|
Serial.print("Cycle ");
|
|
Serial.print(count);
|
|
Serial.print(".\n");
|
|
|
|
Serial.println("Red...");
|
|
all(255,0,0);
|
|
u8x8.clearDisplay();
|
|
u8x8.drawString(0,0,"Hi pupper!");
|
|
u8x8.drawString(0,2,"Red!");
|
|
delay(500);
|
|
|
|
Serial.println("Green...");
|
|
all(0,255,0);
|
|
u8x8.clearDisplay();
|
|
u8x8.drawString(0,0,"Hi pupper!");
|
|
u8x8.drawString(0,1,"Green!");
|
|
delay(500);
|
|
|
|
Serial.println("Blue...");
|
|
all(0,0,255);
|
|
u8x8.clearDisplay();
|
|
u8x8.drawString(0,0,"Hi pupper!");
|
|
u8x8.drawString(0,4,"Blue!");
|
|
delay(500);
|
|
|
|
|
|
Serial.println("Rainbow!");
|
|
u8x8.clearDisplay();
|
|
u8x8.drawString(0,0,"Hi pupper!");
|
|
u8x8.drawString(0,3,"Rainbow!");
|
|
rainbow(1);
|
|
|
|
} |