Skip to content
Snippets Groups Projects
Commit daa9ce00 authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Add option to the ESP web server to show numbers

parent beec6e8d
No related branches found
No related tags found
1 merge request!7Merge old hardware code with new hardware code
......@@ -16,10 +16,12 @@
#define COM_WIFI_BUTTONS 0b0001100
#define COM_WIFI_INDICATORS 0b0001101
#define COM_WIFI_SWITCHES 0b0001110
#define COM_WIFI_NUMBERS 0b0010010
#define COM_WIFI_BUTTONS_STR 0b0001111
#define COM_WIFI_SWITCHES_STR 0b0010000
#define COM_WIFI_INDICATORS_STR 0b0010001
#define COM_WIFI_NUMBERS_STR 0b0010011
#define ACK 0
#define NACK 1
......@@ -42,6 +44,7 @@
#define BTN_COL 3
#define NUM_IND 5
#define NUM_SWT 5
#define NUM_NUM 5
#define VAR_LEN 3
#define MAX_LABEL_LEN 10
\ No newline at end of file
......@@ -20,10 +20,26 @@ R"=====(
grid-template-columns: repeat(5,1fr);
padding-top: 20px;
}
.grid-container-num{
width: 100%;
display: grid;
grid-template-columns: repeat(10,1fr);
padding: 20px;
}
.grid-item{
display: grid;
place-items: center;
padding: 20px;
}
.grid-item-num{
display: grid;
place-items: center start;
padding: 10px;
}
.grid-item-str{
display: grid;
place-items: center end;
padding: 10px;
}
.button {
box-shadow:inset 0px 1px 3px 0px #91b8b3;
......@@ -167,6 +183,19 @@ R"=====(
<div class="indicator" id="ind05">5</div>
</div>
</div>
<h2>Numbers</h2>
<div class="grid-container-num">
<div class="grid-item-str" id="num11">1: </div>
<div class="grid-item-num" id="num01">0</div>
<div class="grid-item-str" id="num12">2: </div>
<div class="grid-item-num" id="num02">0</div>
<div class="grid-item-str" id="num13">3: </div>
<div class="grid-item-num" id="num03">0</div>
<div class="grid-item-str" id="num14">4: </div>
<div class="grid-item-num" id="num04">0</div>
<div class="grid-item-str" id="num15">5: </div>
<div class="grid-item-num" id="num05">0</div>
</div>
<!-----------------------------JavaScript--------------------------->
<script>
......@@ -208,6 +237,11 @@ R"=====(
}
}
}
if(JSONobj["num"]) {
for (let i = 0; i < 5; i++) {
document.getElementById("num0"+(i+1)).innerHTML= JSONobj["num"][i];
}
}
if(JSONobj["swt_l"]) {
for (let i = 0; i < 5; i++) {
if (JSONobj["swt_l"][i]) {
......@@ -238,6 +272,18 @@ R"=====(
}
}
}
if(JSONobj["num_l"]) {
for (let i = 0; i < 5; i++) {
if (JSONobj["num_l"][i]) {
document.getElementById("num1"+(i+1)).style.display = '';
document.getElementById("num0"+(i+1)).style.display = '';
document.getElementById("num1"+(i+1)).innerHTML= JSONobj["num_l"][i];
} else {
document.getElementById("num1"+(i+1)).style.display = 'none';
document.getElementById("num0"+(i+1)).style.display = 'none';
}
}
}
......
......@@ -16,6 +16,12 @@ void init_labels() {
j_swt.add(0);
j_swt_labels.add(switch_labels[i]);
}
for (int i = 0; i < NUM_NUM; i++) {
memset(number_labels[i], '\0', sizeof(number_labels[i]));
number_labels[i][0] = 49+i;
j_num.add(0);
j_num_labels.add(number_labels[i]);
}
}
void setup() {
......
......@@ -47,20 +47,24 @@ WebSocketsServer webSocket = WebSocketsServer(81);
StaticJsonDocument<1024> m_json;
JsonArray j_ind = m_json.createNestedArray("ind");
JsonArray j_swt = m_json.createNestedArray("swt");
JsonArray j_num = m_json.createNestedArray("num");
StaticJsonDocument<1024> m_json_labels;
JsonArray j_ind_labels = m_json_labels.createNestedArray("ind_l");
JsonArray j_swt_labels = m_json_labels.createNestedArray("swt_l");
JsonArray j_btn_labels = m_json_labels.createNestedArray("btn_l");
JsonArray j_num_labels = m_json_labels.createNestedArray("num_l");
String JSONtxt;
char JSONchr[1024];
bool indicators[NUM_IND];
bool buttons[BTN_COL*BTN_ROW];
bool switches[NUM_SWT];
float numbers[NUM_NUM] = { 0.0 };
char indicator_labels[NUM_IND][MAX_LABEL_LEN];
char button_labels[BTN_COL*BTN_ROW][MAX_LABEL_LEN];
char switch_labels[NUM_SWT][MAX_LABEL_LEN];
char number_labels[NUM_NUM][MAX_LABEL_LEN];
hw_timer_t* My_timer = NULL;
hw_timer_t* My_timer2 = NULL;
int broadcast_counter = 0;
......
......@@ -16,6 +16,9 @@ void wifi_server_broadcast() {
j_swt[i] = 0;
}
}
for (int i = 0; i < NUM_NUM; i++) {
j_num[i] = numbers[i];
}
memset(JSONchr, '\0', sizeof(JSONchr));
serializeJson(m_json, JSONchr, sizeof(JSONchr));
webSocket.broadcastTXT(JSONchr);
......@@ -31,6 +34,9 @@ void wifi_broadcast_labels() {
for (int i = 0; i < NUM_SWT; i++) {
j_swt_labels[i] = switch_labels[i];
}
for (int i = 0; i < NUM_NUM; i++) {
j_num_labels[i] = number_labels[i];
}
memset(JSONchr, '\0', sizeof(JSONchr));
serializeJson(m_json_labels, JSONchr, sizeof(JSONchr));
webSocket.broadcastTXT(JSONchr);
......@@ -84,6 +90,11 @@ void wifi_get_indicators() {
}
}
void wifi_get_numbers() {
for (int i = 0; i < NUM_NUM; i++) {
memcpy(&numbers[i], &message_rx[2+4*i], 4);
}
}
void wifi_label_buttons() {
memset(button_labels[message_rx[2]], '\0', sizeof(button_labels[message_rx[2]]));
if (message_rx[1] > 1) {
......@@ -107,7 +118,13 @@ void wifi_label_indicators() {
}
wifi_broadcast_labels();
}
void wifi_label_numbers() {
memset(number_labels[message_rx[2]], '\0', sizeof(number_labels[message_rx[2]]));
if (message_rx[1] > 1) {
memcpy(&number_labels[message_rx[2]][0], &message_rx[3], message_rx[1] - 1);
}
wifi_broadcast_labels();
}
void IRAM_ATTR onTimer() {
if (!cmd_pin && !bt_on) {
wifi_send_buttons();
......@@ -135,6 +152,9 @@ void wifi_read_msg() {
case COM_WIFI_INDICATORS:
wifi_get_indicators();
break;
case COM_WIFI_NUMBERS:
wifi_get_numbers();
break;
case COM_WIFI_BUTTONS_STR:
wifi_label_buttons();
break;
......@@ -144,6 +164,9 @@ void wifi_read_msg() {
case COM_WIFI_INDICATORS_STR:
wifi_label_indicators();
break;
case COM_WIFI_NUMBERS_STR:
wifi_label_numbers();
break;
default:
break;
}
......
......@@ -27,6 +27,8 @@ class Esp:
COM_WIFI_BUTTONS_STR = const(0b0001111)
COM_WIFI_SWITCHES_STR = const(0b0010000)
COM_WIFI_INDICATORS_STR = const(0b0010001)
COM_WIFI_NUMBERS = const(0b0010010)
COM_WIFI_NUMBERS_STR = const(0b0010011)
ACK = const(0)
NACK = const(1)
......@@ -40,6 +42,7 @@ class Esp:
NUM_INDICATORS = const(5)
NUM_BUTTONS = const(9)
NUM_SWITCHES = const(5)
NUM_NUMBERS = const(5)
MODE_BT = const(0)
MODE_WIFI = const(1)
......@@ -158,7 +161,17 @@ class Esp:
message += self.i2b(payload_len)
message += self.make_bytes_from_bools(payload_len, indicators)
self.send_message(message)
def wifi_set_numbers(self, numbers: list):
if len(numbers) != Esp.NUM_NUMBERS:
return
message = self.i2b(Esp.COM_WIFI_NUMBERS)
payload_len = Esp.NUM_NUMBERS*4
for number in numbers:
message += struct.pack('<f', number)
message += self.make_bytes_from_bools(payload_len, numbers)
self.send_message(message)
def wifi_set_buttons_labels(self, labels: list):
if len(labels) != Esp.NUM_BUTTONS:
return
......@@ -177,6 +190,12 @@ class Esp:
for i in range(Esp.NUM_INDICATORS):
self.wifi_set_label(Esp.COM_WIFI_INDICATORS_STR, i, labels[i])
def wifi_set_numbers_labels(self, labels: list):
if len(labels) != Esp.NUM_NUMBERS:
return
for i in range(Esp.NUM_NUMBERS):
self.wifi_set_label(Esp.COM_WIFI_NUMBERS_STR, i, labels[i])
def wifi_set_label(self, cmd, id: int, label: str):
if len(label) > 9:
return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment