#include <Wire.h>
#include <U8g2lib.h>
// #include <SoftwareSerial.h>
// SoftwareSerial SoftSerial(12, 13);
// https://wokwi.com/projects/376501375827109889
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset= */ U8X8_PIN_NONE);
//static const unsigned char image_Volup_8x6_bits[] U8X8_PROGMEM = {0x48, 0x8c, 0xaf, 0xaf, 0x8c, 0x48};
//static const unsigned char image_Lock_8x8_bits[] U8X8_PROGMEM = {0x3c, 0x42, 0x42, 0xff, 0xff, 0xe7, 0xff, 0xff};
//static const unsigned char image_Bluetooth_Idle_5x8_bits[] U8X8_PROGMEM = {0x04, 0x0d, 0x16, 0x0c, 0x0c, 0x16, 0x0d, 0x04};
//static const unsigned char image_Alert_9x8_bits[] U8X8_PROGMEM = {0x10, 0x00, 0x38, 0x00, 0x28, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0xfe, 0x00, 0xee, 0x00, 0xff, 0x01};
#include "OneButton.h"
#define ESP8266
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
// attachInterrupt only supports pin 2 and 3 on UNO.
// See https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
#define PIN_INPUT 13
#define PIN_LED 12
#elif defined(ESP8266)
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
// This LED is lighting on output level LOW.
#define PIN_INPUT 14
#define PIN_LED 12
#elif defined(ESP32)
// Example pin assignments for a ESP32 board
// Some boards have a BOOT switch using GPIO 0.
#define PIN_INPUT 0
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
#define PIN_LED 25
#endif
// Setup a new OneButton on pin PIN_INPUT
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button(PIN_INPUT, true);
// current LED state, staring with LOW (0)
int ledState = LOW;
// save the millis when a press has started.
unsigned long pressStartTime;
// In case the momentary button puts the input to HIGH when pressed:
// The 2. parameter activeLOW is false when the external wiring sets the button to HIGH when pressed.
// The 3. parameter can be used to disable the PullUp .
// OneButton button(PIN_INPUT, false, false);
// This function is called from the interrupt when the signal on the PIN_INPUT has changed.
// do not use Serial in here.
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
void checkTicks() {
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}
#elif defined(ESP8266)
ICACHE_RAM_ATTR void checkTicks() {
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}
#elif defined(ESP32)
void IRAM_ATTR checkTicks() {
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}
#endif
// this function will be called when the button was pressed 1 time only.
void singleClick() {
Serial.println("singleClick() detected.");
} // singleClick
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleClick() {
Serial.println("doubleClick() detected.");
ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // doubleClick
// this function will be called when the button was pressed multiple times in a short timeframe.
void multiClick() {
int n = button.getNumberClicks();
if (n == 3) {
Serial.println("tripleClick detected.");
} else if (n == 4) {
Serial.println("quadrupleClick detected.");
} else {
Serial.print("multiClick(");
Serial.print(n);
Serial.println(") detected.");
}
ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // multiClick
// this function will be called when the button was held down for 1 second or more.
void pressStart() {
Serial.println("pressStart()");
pressStartTime = millis() - 1000; // as set in setPressMs()
} // pressStart()
// this function will be called when the button was released after a long hold.
void pressStop() {
Serial.print("pressStop(");
Serial.print(millis() - pressStartTime);
Serial.println(") detected.");
} // pressStop()
char buffer[100];
//byte sendDATA[] = {0x01,0x00,0x65,0x01,0x00,0x00,0x00,0x30,0x2D,0x5A,0x10,0x0A,0x64,0x00,0x01,0x00,0x7D,0x01,0x00,0x00,0x00,0x30,0x2D,0x5A,0x10,0x0A,0x64,0x00,0x01,0x00};
//byte sendDATA[] = {0x5A,0x10,0x0A,0x64,0x00,0x01,0x00,0x7D,0x01,0x00,0x00,0x00,0x30,0x2D};
int buf[2] = {0x00,0x00};
int battery=0;
byte test = 0x00;
void setup() {
//SoftSerial.begin(9600);
Serial.begin(9600); //Input <- 디바이스
u8g2.begin(); // start the u8g2 library
//u8g2.enableUTF8Print();
u8g2.setFont(u8g2_font_fub14_tr);
sprintf(buffer, "Initializing OK!");
u8g2.drawStr(10, 40, buffer);
u8g2.sendBuffer();
// enable the led output.
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
digitalWrite(PIN_LED, ledState);
// setup interrupt routine
// when not registering to the interrupt the sketch also works when the tick is called frequently.
attachInterrupt(digitalPinToInterrupt(PIN_INPUT), checkTicks, CHANGE);
// link the xxxclick functions to be called on xxxclick event.
button.attachClick(singleClick);
button.attachDoubleClick(doubleClick);
button.attachMultiClick(multiClick);
button.setPressMs(1000); // that is the time when LongPressStart is called
button.attachLongPressStart(pressStart);
button.attachLongPressStop(pressStop);
}
void loop() {
buf[0] = 59;
buf[1] = 35.2;
u8g2.clearBuffer();
u8g2.setBitmapMode(1);
//u8g2.drawFrame(0, 0, 128, 64); // 외곽선 시험
// u8g2.setFont(u8g2_font_streamline_interface_essential_wifi_t);
// sprintf(buffer, "\u0032"); // Wifi Icon 30~32
// u8g2.drawStr(107, 21, buffer);
//u8g2.setFont(u8g2_font_10x20_tr);
u8g2.setFont(u8g2_font_fub14_tr);
//u8g2.setFont(u8g2_font_unifont_t_korean1);
//u8g2.setFont(u8g2_font_guildenstern_nbp_t_all);
//u8g2.setFont(u8g2_font_spleen12x24_mr);
//sprintf(buffer, "DST %d% km", buf[1]); // 배터리 %
sprintf(buffer, "D %d% km | A %d% km/h", buf[1], buf[1]); // 주행거리
u8g2.drawStr(0, 14, buffer);
// 경계선
u8g2.drawLine(0, 17, 126, 17);
// Battey
// u8g2.drawBox(5, 21, 6, 2); // 양극
// u8g2.drawFrame(0, 23, 16, 41); // 5
// u8g2.drawBox(2, 25, 12, 5); // 5
// u8g2.drawLine(1, 31, 15, 31); // 4
// u8g2.drawBox(2, 33, 12, 5); // 4
// u8g2.drawLine(1, 39, 15, 39); // 3
// u8g2.drawBox(2, 41, 12, 5); // 3
// u8g2.drawLine(1, 47, 15, 47); // 2
// u8g2.drawBox(2, 49, 12, 5); // 2
// u8g2.drawLine(1, 55, 15, 55); // 1
// u8g2.drawBox(2, 57, 12, 5); // 1
u8g2.drawBox(5, 21, 6, 2); // 양극
u8g2.drawFrame(0, 23, 16, 41); // 5
u8g2.drawBox(3, 26, 10, 3); // 5
u8g2.drawLine(1, 31, 15, 31); // 4
u8g2.drawBox(3, 34, 10, 3); // 4
u8g2.drawLine(1, 39, 15, 39); // 3
u8g2.drawBox(3, 42, 10, 3); // 3
u8g2.drawLine(1, 47, 15, 47); // 2
u8g2.drawBox(3, 50, 10, 3); // 2
u8g2.drawLine(1, 55, 15, 55); // 1
u8g2.drawBox(3, 58, 10, 3); // 1
// 상단 가로 게이지
// u8g2.drawFrame(0, 0, 54, 14);
// u8g2.drawBox(2, 2, (buf[0]/2), 10); // 배터리 게이지
// u8g2.setFont(u8g2_font_helvR14_tr);
// sprintf(buffer, "%d%%", buf[0]); // 배터리 %
// u8g2.drawStr(58, 14, buffer);
// u8g2.setFont(u8g2_font_7_Seg_41x21_mn);
// sprintf(buffer,"%.2d", buf[0]); // 속도
// u8g2.drawStr(0, 25, buffer);
u8g2.setFont(u8g2_font_7Segments_26x42_mn);
sprintf(buffer,"%.2d", buf[0]); // 속도
u8g2.drawStr(35, 64, buffer);
// u8g2.setFont(u8g2_font_helvB08_tr);
// sprintf(buffer,"Km/h");
// u8g2.drawStr(72, 64, buffer);
u8g2.setFont(u8g2_font_open_iconic_arrow_2x_t);
sprintf(buffer, "\u0053"); // 3단
u8g2.drawStr(113, 68, buffer);
sprintf(buffer, "\u0053"); // 2단
u8g2.drawStr(113, 68-10, buffer);
sprintf(buffer, "\u0053"); // 1단
u8g2.drawStr(113, 68-20, buffer);
// // https://github.com/olikraus/u8g2/wiki/fntgrpu8g#battery24
// u8g2.setFont(u8g2_font_battery19_tn);
// sprintf(buffer, "\u0035"); // 배터리 %
// u8g2.drawStr(50, 60, buffer);
// u8g2.setFont(u8g2_font_battery24_tr);
// sprintf(buffer, "\u0033"); // 배터리 %
// u8g2.drawStr(0, 64, buffer);
// sprintf(buffer, "\u0034"); // 배터리 %
// u8g2.drawStr(70, 50, buffer);
u8g2.sendBuffer();
//delay(180);
button.tick();
delay(10);
}