Display_Avr_3/Lcd_print.ino

84 lines
3.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MyLCD.h"
struct TextCounter {
unsigned long startTime;
int incrementValue;
int startIndex; // Новая переменная для отслеживания начального индекса строки
};
struct TextCounter textCounter;
void init_lcd(){
lcd_init(LCD_DISP_ON_BLINK); // инициализация дисплея
lcd_home(); // домой курсор
lcd_led(0); // вкл подсветки
textCounter.startTime = millis(); // Запоминаем время запуска программы
textCounter.startIndex = 0; // Инициализируем начальный индекс
}
void fillBuffer1(const char* source, char* buffer, size_t bufferSize, int incrementValue) {
int startIndex = incrementValue % strlen(source); // Определяем начальный индекс на основе incrementValue
int endIndex = startIndex + 16;
for (int i = 0; i < 16; ++i) {
buffer[i] = source[(startIndex + i) % strlen(source)];
}
buffer[16] = '\0';
}
void fillBuffer2(float value1, float value2, float value3, char* buffer, size_t bufferSize){
int val1_int = (int) value1;
float val1_float = (abs(value1) - abs(val1_int)) * 10;
int val1_fra = (int)val1_float;
int val2_int = (int) value2;
float val2_float = (abs(value2) - abs(val2_int)) * 10;
int val2_fra = (int)val2_float;
int val3_int = (int) value3;
float val3_float = (abs(value3) - abs(val3_int)) * 10;
int val3_fra = (int)val3_float;
snprintf(buffer, bufferSize, "%d.%d:%d.%d:%d.%d",
val1_int, val1_fra,
val2_int, val2_fra,
val3_int, val3_fra);
}
void print_lcd(struct DisplayData* disp){
unsigned long currentTime = millis(); // Текущее время
// Проверяем, прошло ли 500 мс с момента последнего увеличения incrementValue
if (currentTime - textCounter.startTime >= 500) {
textCounter.incrementValue++; // Увеличиваем incrementValue на 1
textCounter.startTime = currentTime; // Обновляем время
}
struct DisplayData displayData;
strncpy(displayData.topLine, disp->topLine, sizeof(displayData.topLine) - 1);
displayData.topLine[sizeof(displayData.topLine) - 1] = '\0';
displayData.value1 = disp->value1;
displayData.value2 = disp->value2;
displayData.value3 = disp->value3;
// Буферы для заполнения данных
char buffer1[17];
char buffer2[17];
// Заполнение буфера 1
fillBuffer1(displayData.topLine, buffer1, sizeof(buffer1), textCounter.incrementValue);
// Заполнение буфера 2
fillBuffer2(displayData.value1, displayData.value2, displayData.value3, buffer2, sizeof(buffer2));
// Создание массива для вывода на дисплей
char displayArray[32];
strncpy(displayArray, buffer1, 16); // Копирование первых 16 символов из buffer1 в displayArray
strncpy(displayArray + 16, buffer2, 16); // Копирование первых 16 символов из buffer2 в displayArray, начиная с позиции 16
// Вывод данных на экран
lcd_gotoxy(0, 0);
lcd_puts(displayArray);
lcd_gotoxy(0, 1);
lcd_puts(displayArray + 16);
}