#include <Arduino.h>
#include <Wire.h> // specify use of Wire.h library for use of I2C communication
#include <U8g2lib.h> // OLED display library (controller SSD1306, etc.)
// #include <qrcodegen.h>
// HARDWARE PINS ASSIGNMENTS
uint16_t WD_LED = 2;
uint16_t WD_state = 1;
int SDA_1 = 13; // I2C interface pin SDA
int SCL_1 = 16; // I2C interface pin SCL
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, SCL_1, SDA_1 );
String _s;
char char_arr[64];
uint8_t qrcodeArray[60][60];
const uint8_t qrcodeSize = 25;
void OLED_setup();
void OLED_print(int x, int y, int clear, const char* txt);
void OLED_barcode();
void setup() {
Serial.begin(115200);
delay(200);
pinMode(WD_LED, OUTPUT); // Watch dog LED pin init
WD_state = 1;
digitalWrite(WD_LED, WD_state);
Wire.begin(SDA_1, SCL_1); // define pins for I2C
OLED_setup(); // OLED display setup
// OLED_print(0,37,1,"Controller boot...");
delay(1000);
Serial.println("Making buffer...");
makeBuffer();
delay(1000);
OLED_pixel_test();
delay(2000);
}
void loop() {
if (WD_state == 1) {
WD_state = 0;
} else {
WD_state = 1;
}
digitalWrite(WD_LED, WD_state);
Serial.print("WD_state = "); Serial.println(WD_state);
if (WD_state == 1) {
_s = "WD state: " + String(WD_state);
_s.toCharArray(char_arr, 22);
OLED_print(0,37,1,char_arr);
} else {
// OLED_print(0,37,1,"QR code");
OLED_barcode();
}
delay(10000);
}
void OLED_setup() {
u8g2.begin(); // OLED display init
u8g2.enableUTF8Print(); // OLED display init
u8g2.clearBuffer(); // clear the OLED display internal memory
u8g2.setFont(u8g2_font_6x13_t_cyrillic);
}
void OLED_print(int x, int y, int clear, const char* txt) {
if (clear == 1) u8g2.clearDisplay();
u8g2.setFont(u8g2_font_6x13_t_cyrillic); // u8g2.setFont(u8g2_font_6x13_t_cyrillic);
u8g2.drawStr(x,y,txt);
u8g2.sendBuffer();
}
void OLED_pixel_test() {
u8g2.clearDisplay();
u8g2.setDrawColor(1);
for (uint8_t i = 0; i < 100; i++) {
u8g2.drawPixel(i, 10);
}
u8g2.display();
u8g2.setDrawColor(2);
for (uint8_t i = 0; i < 100; i++) {
u8g2.drawPixel(i, 20);
}
u8g2.sendBuffer();
}
void OLED_barcode() {
uint8_t _dot;
u8g2.clearDisplay();
for (uint8_t y = 0; y < qrcodeSize*2; y++)
{
for (uint8_t x = 0; x < qrcodeSize*2; x++)
{
_dot = qrcodeArray[y][x];
if (_dot == 1) {
u8g2.setDrawColor(1);
} else {
u8g2.setDrawColor(0);
}
u8g2.drawPixel(x, y);
}
}
u8g2.sendBuffer();
}
void makeBuffer() {
uint8_t qrcodeSample[25][25] =
{
{1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,0,1,0,1,1,1,1,1,1,1}, // 1
{1,0,0,0,0,0,1,0,1,0,0,1,1,1,1,0,1,0,1,0,0,0,0,0,1}, // 2
{1,0,1,1,1,0,1,0,1,1,0,0,1,0,1,1,1,0,1,0,1,1,1,0,1}, // 3
{1,0,1,1,1,0,1,0,1,1,1,0,0,0,1,1,1,0,1,0,1,1,1,0,1}, // 4
{1,0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1}, // 5
{1,0,0,0,0,0,1,0,1,1,1,0,1,0,1,0,0,0,1,0,0,0,0,0,1}, // 6
{1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1}, // 7
{0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,0}, // 8
{1,1,0,0,1,1,1,0,0,0,1,0,1,1,0,0,0,0,0,1,0,1,1,1,1}, // 9
{1,0,0,1,1,0,0,0,1,0,0,0,0,1,1,1,0,1,0,0,1,1,1,0,1}, // 10
{1,0,1,1,1,0,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,1,0,0}, // 11
{1,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,1,1,1,1}, // 12
{0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,1,0,1,0,0,0,1,0,1}, // 13
{1,0,0,0,1,0,0,1,1,1,1,0,0,1,1,1,0,0,0,0,1,1,1,0,1}, // 14
{0,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1,0,0,1,0,1,0,1,0,0}, // 15
{0,0,1,0,0,1,0,0,0,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0}, // 16
{1,1,0,0,0,1,1,0,0,0,0,0,1,1,0,1,1,1,1,1,1,0,0,1,0}, // 17
{0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,1,0,0,0,1,1,1,0,1}, // 18
{1,1,1,1,1,1,1,0,0,1,0,1,1,1,0,1,1,0,1,0,1,0,1,0,0}, // 19
{1,0,0,0,0,0,1,0,1,0,1,1,1,0,1,0,1,0,0,0,1,0,1,1,0}, // 20
{1,0,1,1,1,0,1,0,1,0,0,0,1,1,0,0,1,1,1,1,1,0,1,1,0}, // 21
{1,0,1,1,1,0,1,0,0,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,0,1,0,0,1,0,1,0,0,1,0,1,1,0,1,0,0,0,1,0},
{1,0,0,0,0,0,1,0,1,1,1,1,1,0,0,0,1,0,1,0,0,0,1,1,0},
{1,1,1,1,1,1,1,0,1,1,1,0,1,1,0,1,0,0,1,0,1,0,1,1,1}
};
uint8_t _pixel = 0;
for (uint8_t y = 0; y < qrcodeSize; y++)
{
for (uint8_t x = 0; x < qrcodeSize; x++)
{
_pixel = qrcodeSample[y][x];
qrcodeArray[y*2][x*2] = _pixel;
qrcodeArray[y*2][x*2+1] = _pixel;
qrcodeArray[y*2+1][x*2] = _pixel;
qrcodeArray[y*2+1][x*2+1] = _pixel;
}
}
}
/*
void makeBuffer() {
uint8_t _pixel = 0;
for (uint8_t y = 0; y < qrcodeSize; y++)
{
for (uint8_t x = 0; x < qrcodeSize; x++)
{
qrcodeArray[y*2][x*2] = _pixel;
qrcodeArray[y*2][x*2+1] = _pixel;
qrcodeArray[y*2+1][x*2] = _pixel;
qrcodeArray[y*2+1][x*2+1] = _pixel;
if (_pixel == 1) {
_pixel = 0;
} else {
_pixel = 1;
}
}
}
}
*/