#include <ArduinoJson.h>
#include <U8g2lib.h>
#include <Wire.h>
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SH1107_128X64_F_HW_I2C u8g2(U8G2_R3, /* reset=*/ 8);
//U8G2_SH1107_64X128_F_4W_HW_SPI u8g2(U8G2_R3, /* cs=*/7, /* dc=*/ 9, /* reset=*/ 8);
const float BETA = 3950;
int temp = 25;
floar celsius;
#define readpin 25
#define UP_KEY 13//gpio13
#define DOWN_KEY 14//gpio12
#define ENTER_KEY 15//gpio14
#define keyup digitalRead(UP_KEY)
#define keyenter digitalRead(ENTER_KEY)
#define keydown digitalRead(DOWN_KEY)
uint8_t func_index= 0x00;
typedef struct
{
uint8_t current;
uint8_t up; //向上翻索引号
uint8_t down; //向下翻索引号
uint8_t enter; //确认索引号
void(*current_operation)();
}key_table;
void (*current_operation_index)();
void fun_first();
void fun_Second();
void fun_Second_1();
void fun_Second_cursor_1();
void fun_Second_cursor_2();
void fun_Second_cursor_3();
void fun_Second_cursor_4();
void fun_Second_1_cursor_1();
void fun_Second_1_cursor_2();
void fun_Second_1_cursor_3();
void fun_Second_1_cursor_4();
void fun_Second_1_cursor_5();
void fun_Second_1_cursor_6();
key_table table[30] =
{
{0, 0, 0, 1, (*fun_first)},
{1, 4, 2, 0, (*fun_Second_cursor_1)},
{2, 1, 3, 5, (*fun_Second_cursor_2)},
{3, 2, 4, 5, (*fun_Second_cursor_3)},
{4, 3, 1, 5, (*fun_Second_cursor_4)},
{5, 10, 6, 5, (*fun_Second_1_cursor_1)},
{6, 5, 7, 5, (*fun_Second_1_cursor_2)},
{7, 6, 8, 5, (*fun_Second_1_cursor_3)},
{8, 7, 9, 5, (*fun_Second_1_cursor_4)},
{9, 8, 10, 5, (*fun_Second_1_cursor_5)},
{10, 9, 5, 1, (*fun_Second_1_cursor_6)},
};
void U8G2_SHOW(void *pt)
{
u8g2.begin();
u8g2.enableUTF8Print();
pinMode(UP_KEY, INPUT_PULLUP);
pinMode(ENTER_KEY, INPUT_PULLUP);
pinMode(DOWN_KEY, INPUT_PULLUP);
while(1)
{
if ((keyup== LOW ) || (keydown == LOW ) || (keyenter == LOW ))
{
delay(10); //消抖
if (keyuo == LOW)
{
fun_index = table[func_index].up; //向上翻
while (keyup==LOW);
}
if (keydown == LOW)
{
fun_index = table[func_index].down;
while (keydown == LOW);
}
if (keyenter == LOW)
{
fun_index = table[func_index].enter;
while (keyenter == LOW);
}
}
current_operation_index = table[func_index].current_operation;
(*curent_operation_index)();
vTaskDelay(10);
}
}
void setup(void)
{
xTaskCreatePinnedToCore(U8G2_SHOW,"show",1024*100,NULL,1,NULL,1);
//xTaskCreatePinnedToCore(temp_rec,"show",1024*100,NULL,1,NULL,1);
}
void setup() { //loopBack , Priority 1, Core 1
Serial.begin(9600);
u8g2.begin();
ledcSetup(0, 5000, 8);
ledcAttachPin(12, 0);
}
void loop(void)
{
}
void fun_first()
{
int analogValue = analogRead(readin);
celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_wqy14_t_gb2312a); // choose a suitable font
u8g2.setCursor(20, 16 * 2 - 2);
u8g2.print("当前温度:");
u8g2.setCursor(80, 16 * 2 - 2);
u8g2.print(celsius);
u8g2.setCursor(20, 16 * 4 - 2);
u8g2.print("设定温度:");
u8g2.setCursor(80, 16 * 4 - 2);
u8g2.print(temp);
u8g2.sendBuffer(); // transfer internal memory to the display
}
void fun_Second_cursor_1()
{
u8g2.clearBuffer();
fun_Second();
u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
u8g2.drawGlyph(5, 20* (1 % 4 == 0 ? 4 : 1 % 4) - 4, 118);
u8g2.sendBuffer();
}