-
Notifications
You must be signed in to change notification settings - Fork 1
/
alzaergo.ino
155 lines (122 loc) · 3.29 KB
/
alzaergo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//#include <NeoSWSerial.h>
//#include <Wire.h>
#include <U8g2lib.h>
#define ALZAET1NG_THREADSAFE
#include "src/AlzaET1Ng.h"
#include <WiFi.h>
#include "WiFiConfig.h"
#include "esp_http_server.h"
#define KEY_PIN 22
const int key_pins[] = {27, 14, 12, 13};
#define KEY_UP 0
#define KEY_DOWN 1
#define KEY_M 2
TaskHandle_t AlzaTask;
AlzaET1Ng::ControlPanel AlzaControl(Serial2, KEY_PIN);
httpd_handle_t httpServer;
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 5, /* dc=*/ 32, /* reset=*/ 33);
esp_err_t get_height_handler(httpd_req_t *req)
{
httpd_resp_send(req, String(AlzaControl.getHeight()).c_str(), HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
httpd_uri_t uri_get_height = {
.uri = "/height",
.method = HTTP_GET,
.handler = get_height_handler,
.user_ctx = NULL
};
httpd_handle_t startWebServer(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_handle_t server = NULL;
if (httpd_start(&server, &config) == ESP_OK) {
httpd_register_uri_handler(server, &uri_get_height);
}
return server;
}
void stop_webserver(httpd_handle_t server)
{
if (server) {
httpd_stop(server);
}
}
void AlzaMainLoop( void * parameter) {
for(;;) {
AlzaControl.handleLoop();
}
}
void ConnectToWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to "); Serial.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);
if ((++i % 16) == 0)
{
Serial.println(F(" still trying to connect"));
}
}
Serial.print(F("Connected. My IP address is: "));
Serial.println(WiFi.localIP());
}
void setup()
{
u8g2.begin();
Serial.begin(115200);
Serial.write("Hello world");
for(int x=0; x<4; x++)
{
pinMode(key_pins[x], INPUT);
}
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(15,10,"Hello World!");
u8g2.sendBuffer();
xTaskCreatePinnedToCore(
AlzaMainLoop,
"AlzaTask",
10000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&AlzaTask,
0); /* Core where the task should run */
ConnectToWiFi();
httpServer = startWebServer();
}
void writeln(String ln) {
u8g2.clearBuffer();
u8g2.setCursor(0,15);
u8g2.print(ln);
u8g2.sendBuffer();
}
int up, down, memory;
unsigned long last_refresh = 0;
char displayData[] = {0, 0, 0, 0, 0};
void loop()
{
up = digitalRead(key_pins[KEY_UP]);
down = digitalRead(key_pins[KEY_DOWN]);
memory = digitalRead(key_pins[KEY_M]);
if (up == HIGH) {
AlzaControl.holdCommand(AlzaET1Ng::Commands::Up);
} else if (down == HIGH) {
AlzaControl.holdCommand(AlzaET1Ng::Commands::Down);
} else if (memory == HIGH) {
AlzaControl.holdCommand(AlzaET1Ng::Commands::M);
} else {
AlzaControl.holdCommand(AlzaET1Ng::Commands::Status);
}
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB14_tr);
AlzaControl.getBcdDisplayAsString(displayData);
u8g2.drawStr(35, 40, displayData);
u8g2.setFont(u8g2_font_helvR08_te);
u8g2.drawStr(10, 10, WiFi.localIP().toString().c_str());
u8g2.sendBuffer();
}