Hello my project incorporates a Nextion Display. I have based my code on the examples provided by the Nextion Display library. My current code uses up a lot of RAM due to global variables. There has to be a better way of achieving the desired functionality whilst using less RAM. I had an attempt but I didn't foresee all the complications.
Here is my code(filtered out most of the irrelevant bits):
.ino file:
#include <NexButton.h>
#include <Nextion.h>
#include "Nextion_Display.h"
NexTouch* nex_listen_list[] =
{
//Page 0
&NextionDisplay::btn_slow_down,
&NextionDisplay::btn_speed_up,
&NextionDisplay::btn_turn_right,
&NextionDisplay::btn_turn_left,
//Page 1
&NextionDisplay::tire_circ_b3,
&NextionDisplay::tire_circ_b2,
&NextionDisplay::tire_circ_b1,
&NextionDisplay::tire_circ_b0,
&NextionDisplay::start_transmission_b5,
NULL
};
void setup()
{
nexInit();
NextionDisplay::nex_init_comps();
}
void loop()
{
nexLoop(nex_listen_list);
}
Nextion_Display.h:
#ifndef NEXTION_DISPLAY_H
#define NEXTION_DISPLAY_H
#include <NexButton.h>
#include <Nextion.h>
namespace NextionDisplay
{
//Global
enum pictures
{
speed_gauge_background,
radio_connected_icon,
radio_disconnected_icon
};
//Page 0
extern NexGauge speed_gauge;
extern NexText distance_meter, speed_meter;
extern NexPicture radio_status_icon;
extern NexButton btn_slow_down, btn_speed_up,
btn_turn_right, btn_turn_left, btn_settings;
extern NexTimer btn_slw_dwn_tmr, btn_spd_up_tmr,
btn_trn_rght_tmr, btn_trn_lft_tmr;
void settings_btn_callback();
void slow_down_btn_callback();
void speed_up_btn_callback();
void turn_right_btn_callback();
void turn_left_btn_callback();
//Page 1
extern NexButton tire_circ_b3, tire_circ_b2, tire_circ_b1,
tire_circ_b0, start_transmission_b5;
extern NexText tire_circ_text;
void start_transmission_callback();
//Setup
void nex_init_comps();
}
#endif
Nextion_Display.cpp:
#include <Arduino.h>
#include "Nextion_Display.h"
#include "Radio.h"
#include "Bicycle_Computer.h"
namespace NextionDisplay
{
//Page 0
NexGauge speed_gauge{0, 2, "z0"};
NexText distance_meter{0, 6, "t0"}, speed_meter{0, 7, "t1"};
NexPicture radio_status_icon{0, 13, "p0"};
NexButton btn_slow_down{0, 7, "b0"}, btn_speed_up{0, 8, "b1"},
btn_turn_right{0, 9, "b2"}, btn_turn_left{0, 5, "b3"},
btn_settings{0, 4, "b3"};
NexTimer btn_slw_dwn_tmr{0, 9, "tm0"}, btn_spd_up_tmr{0, 10, "tm1"},
btn_trn_rght_tmr{0, 11, "tm2"}, btn_trn_lft_tmr{0, 12, "tm3"};
//Page 1
NexButton tire_circ_b3{1, 5, "b3"}, tire_circ_b2{1, 4, "b2"},
tire_circ_b1{1, 3, "b1"}, tire_circ_b0{1, 2, "b0"},
start_transmission_b5{1, 8, "b5"};
NexText tire_circ_text{1, 7, "t1"};
void nex_init_comps()
{
//Page 0
btn_settings.attachPop(settings_btn_callback, &btn_settings);
btn_slow_down.attachPop(slow_down_btn_callback, &btn_slow_down);
btn_speed_up.attachPop(speed_up_btn_callback, &btn_speed_up);
btn_turn_right.attachPop(turn_right_btn_callback, &btn_turn_right);
btn_turn_left.attachPop(turn_left_btn_callback, &btn_turn_left);
//Page 1
start_transmission_b5.attachPop(start_transmission_callback, &start_transmission_b5);
}
///Callbacks
//Page 0
void settings_btn_callback()
{
char tire_circ_string[BC_NUM_TIRE_CIRC_CHARS + 1];
sprintf(tire_circ_string, "%04u", BicycleComputer::tire_circ);
tire_circ_text.setText(tire_circ_string);
}
void slow_down_btn_callback()
{
radio_cont.radio_transmit_message(MSG_SLOW_DOWN);
}
void speed_up_btn_callback()
{
radio_cont.radio_transmit_message(MSG_SPEED_UP);
}
void turn_right_btn_callback()
{
radio_cont.radio_transmit_message(MSG_TURN_RIGHT);
}
void turn_left_btn_callback()
{
radio_cont.radio_transmit_message(MSG_TURN_LEFT);
}
//Page 1
void start_transmission_callback()
{
radio_cont.radio_transmit_message(MSG_START_TRANSMISSION);
}
}