#include <Wire.h>
#include <U8x8lib.h>
#include <U8g2lib.h>
#define RIGHT_BUTTON_PIN A1
#define LEFT_BUTTON_PIN A0
#define ENCODER_BUTTON_PIN 5
enum Show_modes{
Normal,
edit_t1_ON,
edit_t1_OFF,
edit_t2_ON,
edit_t2_OFF
}show_mode=0;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
#define cross_width 14
#define cross_height 14
static const unsigned char cross_bits[] U8X8_PROGMEM ={
0x00,0x00,0xf0,0x03,0xf0,0x05,0xf4,0x00,0xe2,0x1c,0x06,0x1e,0x1e,0x1e,0x1e,
0x1e,0x1e,0x19,0xcc,0x11,0xc0,0x03,0xe4,0x0b,0xf0,0x03,0x00,0x00 };
#define cross_block_width 14
#define cross_block_height 14
static const unsigned char cross_block_bits[] U8X8_PROGMEM = {
0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
0x01, 0x20, 0xFF, 0x3F, };
unsigned int flasher_interval=100;
unsigned long last_update_flasher=0;
void flasher_blink(){
if(millis()-last_update_flasher>=flasher_interval){
last_update_flasher=millis();
digitalWrite(13,!digitalRead(13));
}
}
#define heat_width 93
#define heat_height 14
static const unsigned char heat_bits[] U8X8_PROGMEM= {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x87,0xff,0xc1,
0xcf,0xff,0xff,0xff,0xff,0xf1,0xff,0xff,0x1f,0x8f,0xff,0xc1,0xcf,0xff,0xff,
0xff,0xff,0xf1,0xff,0xff,0x1f,0x8f,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xf3,
0xff,0xff,0x1f,0x8f,0xff,0xff,0x87,0xff,0x0f,0xff,0xff,0xf3,0xff,0xff,0x1f,
0x8f,0xff,0xc1,0x07,0xff,0xff,0xff,0xff,0xf3,0xff,0xff,0x1f,0x0f,0xf0,0xc1,
0x07,0xff,0x0f,0xff,0xc3,0xf3,0x3f,0x0c,0x1e,0x0f,0xf0,0xc1,0x07,0xff,0x0f,
0xfe,0xcf,0xf3,0xff,0x3c,0x1c,0xc7,0xf1,0xc7,0x3f,0xff,0x67,0xfe,0xcf,0xf3,
0xff,0xfc,0x18,0x01,0x00,0x00,0x00,0xff,0x07,0xfe,0xcf,0xf3,0xff,0x00,0x10,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc7,0xff,0x7f,0xfc,0x1f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xc3,0xff,0x3f,0xfc,0x1f,0xff,0xff,0xff,0xff,0xff,0xff,
0x1f,0xe0,0xff,0x01,0xfe,0x1f,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xf0,0xff,
0x07,0xff,0x1f };
#define HEAT_TIMER_width 122
#define HEAT_TIMER_height 15
static const unsigned char heat_timer_bits[] U8X8_PROGMEM= {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xf8,0x00,0x00,0x00,0x00,0x00,0xc0,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xf8,0x00,0x00,0x00,0x1f,0x00,0xc0,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x00,0x1f,0x00,
0xc0,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x70,0x00,0x07,0x00,0x00,0x00,
0x00,0xc0,0x01,0x00,0x00,0x00,0x80,0x07,0x0e,0x70,0x70,0x00,0x07,0x1e,0x00,
0x1e,0x80,0xc7,0x01,0x78,0x3f,0x00,0x00,0x06,0x1f,0x60,0x70,0x00,0x06,0x1b,
0x00,0x3e,0x00,0xc7,0x01,0x70,0x7c,0x00,0x00,0x86,0x19,0x60,0x70,0x00,0x06,
0x1f,0x00,0x33,0x00,0xc7,0x01,0x70,0xf0,0x00,0x00,0xfe,0xff,0x7f,0xf0,0xff,
0x07,0x1c,0x00,0x3f,0x00,0xc7,0x01,0xf0,0xff,0x01,0x00,0x06,0x00,0x00,0x00,
0x00,0x00,0x1c,0x00,0x1e,0x00,0x07,0x00,0x70,0x00,0x00,0x00,0x07,0x00,0x78,
0x00,0x00,0x00,0x1e,0x00,0x00,0x80,0x07,0x00,0x78,0x00,0x00,0xf8,0x03,0x00,
0x78,0x00,0x00,0xe0,0x0f,0x00,0x00,0xfc,0x03,0xc0,0x3f,0x00,0x00,0xe0,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x0f,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
//Modes
enum Control_types{
PURE_TIMER=1,
PURE_BYPASS_TEMPRETURE,
PURE_TEMPRETURE,
TIMERx2_WITH_TEMPRETURE,
PURE_TEMPRETURE_half,
PURE_TEMPRETURE_3PARTS,
PURE_TEMPRETURE_ONEBYONE,
TIMER_WITH_TEMPRETURE,
OXYGEN_WITH_TEMPRETURE,
ALL_FAN_ON_BUMB_OFF,
ALL_ON,
ALL_ON_HALFLOAD,
ALL_ON_THIRDLOAD,
ALL_OFF,
TESTING
};
//TIMERx2_WITH_TEMPRETURE
//PURE_BYPASS_TEMPRETURE
Control_types control_type=TIMERx2_WITH_TEMPRETURE;
unsigned int on_time1=20;
unsigned int off_time1=30;
unsigned int on_time2=4;
unsigned int off_time2=5;
unsigned int on_time1_cal=20;
unsigned int off_time1_cal=30;
unsigned int on_time2_cal=4;
unsigned int off_time2_cal=5;
uint16_t fan_state=0;
void u8g2_fans_work() {
const uint8_t frame_size_3 = 25;
const uint8_t frame_size_x = 20;
const uint8_t frame_size_1 = 25;
if(control_type==PURE_BYPASS_TEMPRETURE){
u8g2.drawXBMP(35, 0, heat_width, heat_height, heat_bits);
}else if (control_type==TIMERx2_WITH_TEMPRETURE){
u8g2.drawXBMP(8, 0, HEAT_TIMER_width, HEAT_TIMER_height, heat_timer_bits);
u8g2.setCursor(5, 15);
u8g2.print(on_time1);
if(show_mode==edit_t1_ON){
u8g2.print("*: ");
}else if(show_mode==edit_t1_OFF){
u8g2.print(" :*");
}else{
u8g2.print(" : ");
}
u8g2.print(off_time1);
u8g2.setCursor(80, 15);
u8g2.print(on_time2);
if(show_mode==edit_t2_ON){
u8g2.print("*: ");
}else if(show_mode==edit_t2_OFF){
u8g2.print(" :*");
}else{
u8g2.print(" : ");
}
u8g2.print(off_time2);
////////////////////calculation
u8g2.setCursor(5, 25);
u8g2.print(on_time1_cal);
u8g2.print(" : ");
u8g2.print(off_time1_cal);
u8g2.setCursor(80, 25);
u8g2.print(on_time2_cal);
u8g2.print(" : ");
u8g2.print(off_time2_cal);
}
if(fan_state&1){
u8g2.drawXBMP(frame_size_x*5+4, frame_size_3*2, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_x*5+4, frame_size_3*2, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&2){
u8g2.drawXBMP(frame_size_1*3+14, frame_size_3*1.5, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_1*3+14, frame_size_3*1.5, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&4){
u8g2.drawXBMP(frame_size_x*4+4, frame_size_3*2, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_x*4+4, frame_size_3*2, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&8){
u8g2.drawXBMP(frame_size_1*2+14, frame_size_3*1.5, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_1*2+14, frame_size_3*1.5, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&16){
u8g2.drawXBMP(frame_size_x*3+4, frame_size_3*2, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_x*3+4, frame_size_3*2, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&32){
u8g2.drawXBMP(frame_size_x*2+4, frame_size_3*2, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_x*2+4, frame_size_3*2, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&64){
u8g2.drawXBMP(frame_size_1+14, frame_size_3*1.5, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_1+14, frame_size_3*1.5, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&128){
u8g2.drawXBMP(frame_size_x+4, frame_size_3*2, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(frame_size_x+4, frame_size_3*2, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&256){
u8g2.drawXBMP(14, frame_size_3*1.5, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(14, frame_size_3*1.5, cross_block_width, cross_block_height, cross_block_bits);
}
if(fan_state&512){
u8g2.drawXBMP(4, frame_size_3*2, cross_width, cross_height, cross_bits);
}else{
u8g2.drawXBMP(4, frame_size_3*2, cross_block_width, cross_block_height, cross_block_bits);
}
}
void u8g2_prepare(void) {
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
u8g2.setBitmapMode(false /* solid */);
u8g2.setDrawColor(1);// White
}
unsigned long last_update_u8g2=0;
void u8g2_update(){
last_update_u8g2=millis();
u8g2.clearBuffer();
u8g2_fans_work();
u8g2.sendBuffer();
}
void setup() {
pinMode(13,OUTPUT);
pinMode(RIGHT_BUTTON_PIN,INPUT_PULLUP);
pinMode(LEFT_BUTTON_PIN,INPUT_PULLUP);
pinMode(ENCODER_BUTTON_PIN,INPUT_PULLUP);
u8g2.begin();
u8g2_prepare();
u8g2_update();
}
bool button_right_clicked=0;
void loop() {
flasher_blink();
if(digitalRead(RIGHT_BUTTON_PIN)==0 && TIMERx2_WITH_TEMPRETURE && button_right_clicked == 0){
show_mode++;
if(show_mode>4){
show_mode==0;
}
button_right_clicked = 1;
u8g2_update();
}
if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_right_clicked == 1)) { // unclick
button_right_clicked = 0;
}
}