/* Working on new logic for display */
/* add logic for angle display or not*/
#include <string>
#if defined(DMD_PARA)
#include "DMD_Monochrome_Parallel.h"
#else
#include "DMD_MonoChrome_SPI.h"
#endif
// Fonts includes
#include "st_fonts/SystemFont5x7.h"
#include "st_fonts/Arial_Black_16.h"
//Number of panels in x and y axis
#define DISPLAYS_ACROSS 2
#define DISPLAYS_DOWN 1
// Enable of output buffering
// if true, changes only outputs to matrix after
// swapBuffers(true) command
// If dual buffer not enabled, all output draw at matrix directly
// and swapBuffers(true) cimmand do nothing
#define ENABLE_DUAL_BUFFER true
// ----- Select pins for P10 matrix connection ------------
// pins A, B, SCLK may be any digital I/O, pin nOE should be Timer3 PWM pin as
PB0,PB1
// if connect as SPI, do not use corresponding MiSO pin - PA6 for SPI1 and PB14 for
SPI2
#define DMD_PIN_A PB5
#define DMD_PIN_B PB6
#define DMD_PIN_nOE PB1
#define DMD_PIN_SCLK PB0
#define NO_DATA_TIMEOUT 5000 // 5 second timeout
//=== Config for SPI connect ====
SPIClass dmd_spi(1);
DMD_MonoChrome_SPI dmd(DMD_PIN_A, DMD_PIN_B, DMD_PIN_nOE, DMD_PIN_SCLK,
DISPLAYS_ACROSS, DISPLAYS_DOWN, dmd_spi, ENABLE_DUAL_BUFFER);
// --- Define fonts ----
DMD_Standard_Font IndArial_Black_F(Arial_Black_16);
DMD_Standard_Font SystemFont(SystemFont5x7);
#define RS485_PIN PC13
void serial_read(void);
void reciv_str_sepration(void);
void display_data(void);
void check_for_timeout(void);
void display_no_data(void);
#define BUFFER_SIZE 100 // Define a buffer size
char receivedData[BUFFER_SIZE]; // Array to store received data
int g_index = 0; // To track position in the buffer
bool receiving = false; // Flag to indicate whether we are in receiving
mode
bool str_recv = false; // Flage to indicate string recived
bool disp_data = false; // Flage to indicate string recived
bool no_data = false; // Flag to indicate if we're in no data state
unsigned long last_data_time = 0; // Timestamp of last received data
void setup(void) {
[Link](500);
[Link](255);
[Link](1);
[Link](&IndArial_Black_F);
[Link](0, 0, "E-DASH", 1, 0); // Yellow
// [Link]("DYNAMIC", 6, 10, 0);
[Link](true);
delay(2000);
[Link](1);
// Start the serial communication
pinMode(RS485_PIN, OUTPUT);
digitalWrite(RS485_PIN, LOW);
[Link](9600);
// Initialize the last data time to current time
last_data_time = millis();
// [Link](&IndArial_Black_F);
// [Link](0, 1, "299.9", 1, 0);
// [Link](&SystemFont);
// [Link](43, 7, "E88", 1, 0);
// [Link](true);
// delay(2000);
}
/
*----------------------------------------------------------------------------------
----
loop
Arduino architecture main loop
-----------------------------------------------------------------------------------
---*/
void loop(void) {
serial_read();
reciv_str_sepration();
display_data();
check_for_timeout();
}
void serial_read(void) {
// Check if there is data available to read
if ([Link]() > 0) {
char incomingByte = [Link](); // Read one byte
// If we haven't started receiving yet and the byte is '$', begin receiving
if (incomingByte == '$') {
receiving = true;
g_index = 0; // Reset the buffer
// [Link]("Start receiving data...");
}
// If we are in receiving mode, add characters to the buffer
if (receiving) {
if (incomingByte != '#' && g_index < BUFFER_SIZE - 1) {
receivedData[g_index] = incomingByte; // Save the byte
g_index++; // Increment the buffer index
} else if (incomingByte == '#') {
// End of the message, terminate string and stop receiving
receivedData[g_index++] = incomingByte; // Null-terminate the string
receivedData[g_index] = '\0'; // Null-terminate the string
receiving = false; // Stop receiving
// [Link]("Received: ");
// [Link](receivedData);
str_recv = true;
// Update the time when data was last received
last_data_time = millis();
// Reset no_data flag if it was set
if (no_data) {
no_data = false;
}
}
}
}
}
// Function to check if we've exceeded the timeout since last received data
void check_for_timeout(void) {
// Check if time since last data exceeds the timeout
if (!no_data && (millis() - last_data_time > NO_DATA_TIMEOUT)) {
no_data = true;
display_no_data();
}
}
// Function to display the NO DATA message
void display_no_data(void) {
[Link](1);
[Link](&SystemFont);
[Link](0, 9, "E06", 1, 0); // Yellow
[Link](true);
}
char *current_weight, *load_condition, *x_axis_angle, *x_ang_max, *y_axis_angle,
*y_ang_max, *angle_condition, *colan, *angle_select;
void reciv_str_sepration(void) {
if (str_recv) {
char *start_ch, *stop_ch;
// Use strtok to split the string by "|"
start_ch = strtok(receivedData, "|");
colan = strtok(NULL, ":");
current_weight = strtok(NULL, "|");
load_condition = strtok(NULL, "|");
x_axis_angle = strtok(NULL, "|");
x_ang_max = strtok(NULL, "|");
y_axis_angle = strtok(NULL, "|");
y_ang_max = strtok(NULL, "|");
angle_condition = strtok(NULL, "|");
angle_select = strtok(NULL, "|");
//Print each tokenized value
// [Link]("Current Weight: ");
// [Link](current_weight);
// [Link]("load Condition: ");
// [Link](load_condition);
// [Link]("X-axis Angle: ");
// [Link](x_axis_angle);
// [Link]("Y-axis Angle:: ");
// [Link](y_axis_angle);
// [Link]("angle_condition: ");
// [Link](angle_condition);
str_recv = false;
disp_data = true;
}
}
void display_data(void) {
const uint8_t up_symbol[] = {
B00010000, B00000000,
B00111000, B00000000,
B01111100, B00000000,
B11111110, B00000000,
B11111111, B00000001,
B00111000, B00000000,
B00111000, B00000000,
B00111000, B00000000,
B00111000, B00000000
};
const uint8_t down_symbol[] = {
B00111000,
B00000000,
B00111000,
B00000000,
B00111000,
B00000000,
B00111000,
B00000000,
B11111111,
B00000001,
B11111110,
B00000000,
B01111100,
B00000000,
B00111000,
B00000000,
B00010000,
B00000000,
};
const uint8_t left_symbol[] = {
B00010000,
B00011000,
B00011100,
B00011110,
B00011111,
B00011110,
B00011100,
B00011000,
B00010000,
};
const uint8_t right_symbol[] = {
B00000001,
B00000011,
B00000111,
B00001111,
B00011111,
B00001111,
B00000111,
B00000011,
B00000001,
};
if (disp_data && !no_data) {
// [Link]("Display data");
int t_load_condition = atoi(load_condition);
int t_ang_condition = atoi(angle_condition);
int t_x_ang_max = atoi(x_ang_max);
int t_y_ang_max = atoi(y_ang_max);
char wt_ang_error[4] = "";
char t_cur_wt[10];
char show_max_angle = 0;
int weight_len = strlen(current_weight);
if (weight_len > 9) weight_len = 9;
memcpy(t_cur_wt, current_weight, 9);
t_cur_wt[9] = '\0'; // Ensure null-termination
char *temp1 = strtok(x_axis_angle, ":");
char *t_x_ang = strtok(NULL, "");
char *temp2 = strtok(y_axis_angle, ":");
char *t_y_ang = strtok(NULL, ":");
int t_x_angle = abs(atoi(t_x_ang));// convert t_x angle to string and send to
display.
int t_y_angle = abs(atoi(t_y_ang));
// Convert absolute integers back to strings for display
char t_x_angle_str[10];
char t_y_angle_str[10];
sprintf(t_x_angle_str, "%d", t_x_angle); // Convert to positive string
sprintf(t_y_angle_str, "%d", t_y_angle); // Convert to positive string
char *t_sel_max_ang;
int t_ang_sel = atoi(angle_select);
if (t_ang_sel == 1) {
if (t_x_angle == t_y_angle) {
t_sel_max_ang = "0";
} else if (t_x_angle > t_y_angle) {
t_sel_max_ang = t_x_angle_str;
} else {
t_sel_max_ang = t_y_angle_str;
}
} else {
t_sel_max_ang = " ";
}
if (t_load_condition != 0) {
switch (t_load_condition) {
case 0:
// [Link]("load safe");
strcpy(wt_ang_error, " ");
break;
case 1:
// [Link]("load Warning");
strcpy(wt_ang_error, "E1");
break;
case 2:
// [Link]("load ovld");
strcpy(wt_ang_error, "E2");
break;
case 3:
// [Link]("load ovld");
strcpy(wt_ang_error, "E3");
break;
default:
break;
}
} else if (t_ang_condition != 0) {
switch (t_ang_condition) {
case 0:
// [Link]("SAFE ANGLE");
strcpy(wt_ang_error, " ");
break;
case 1:
// [Link]("ANGLE ERROR");
strcpy(wt_ang_error, "E5");
show_max_angle = 1;
break;
default:
break;
}
}
[Link](1);
[Link](&SystemFont);
if (t_ang_sel == 1) {
[Link](0, 0, t_x_angle_str, 1, 0); // Yellow//int t_x_angle print
this here
[Link](0, 9, t_y_angle_str, 1, 0); //NA
} else {
[Link](0, 0, " ", 1, 0); // Yellow
[Link](0, 9, " ", 1, 0); //NA
}
if (show_max_angle == 1) {
show_max_angle = 0;
[Link](0, 0, t_sel_max_ang, 1, 0); // Yellow
}
[Link](0, 9, wt_ang_error, 1, 0); // Yellow
[Link](&IndArial_Black_F);
[Link](15, 1, t_cur_wt, 1, 0); // Yellow
[Link](12, 0, 12, 16, 1);
/*
switch (t_x_ang_max) {
case 0:
[Link](0, 16, x_axis_angle, angle_color); // Yellow
break;
case 1:
[Link](0, 16, "X:", angle_color);
[Link](19, 20, up_symbol, 16, 9, angle_color);
break;
case 2:
[Link](0, 16, "X:", angle_color);
[Link](19, 20, down_symbol, 16, 9, angle_color);
break;
}
switch (t_y_ang_max) {
case 0:
[Link](32, 16, y_axis_angle, angle_color); // Yellow
break;
case 1:
[Link](32, 16, "Y:", angle_color);
[Link](51, 20, up_symbol, 16, 9, angle_color);
break;
case 2:
[Link](32, 16, "Y:", angle_color);
[Link](51, 20, down_symbol, 16, 9, angle_color);
break;
}
*/
// [Link](32, 16, up_symbol,9, 5, 0x701f);
// [Link](32, 24, down_symbol,9, 5, 0x701f);
// [Link](48,16, left_symbol,8, 9, 0x701f);
// [Link](54, 16, right_symbol,8, 9, 0x701f);
[Link](true);
disp_data = false;
}
}