Skip to content

Commit

Permalink
Reescrito codigo para lcd1602
Browse files Browse the repository at this point in the history
  • Loading branch information
surfzone-org committed Aug 22, 2022
1 parent 91e333b commit e5bfe28
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 127 deletions.
3 changes: 2 additions & 1 deletion include/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
//#define BAUDRATE 115200
#endif

#ifndef SERIAL_SCREEN

#if !defined SERIAL_SCREEN && !defined SCREEN_SERIAL_1602
#define SCREEN_CONTROL
#ifdef SCREEN_CONTROL // SELECT A DISPLAY:
#define LCD_1602_I2C
Expand Down
79 changes: 79 additions & 0 deletions include/hardware/lcd1602_LiquidCrystal_I2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/***************************************************************
openELECTRO
Home appliance control, based on arduino and other MPU
https://github.com/carlymx/openELECTRO
[email protected], [email protected]
2022
***************************************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <hardware/lcd1602_custom_char.h>
#include <hardware/lcd1602_layout.h>
#include <hardware/lcd1602_control.h>

#define ICON_RESIST 0 // RESERVED FROM 0 TO 3
#define ICON_BELL 4

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif

// LCD1602 I2C [ adress: 0x27 ]
LiquidCrystal_I2C lcd(0x27,16,2);

void screen_init(){
lcd.init(); // initialize the lcd
lcd.createChar(ICON_BELL, bell);
lcd.home();
lcd.clear();
}

void screen_backlight(bool active){
if (active == true) lcd.backlight();
else lcd.noBacklight();
}

void screen_clear(){
lcd.clear();
}

void screen_lines_write(){
}

void screen_resist_write() {
if (_screen_resist == 0) {
lcd.setCursor(LCD_POS_RES_L1_X, LCD_POS_RES_L1_Y);
lcd.print(" ");
lcd.setCursor(LCD_POS_RES_L2_X, LCD_POS_RES_L2_Y);
lcd.print(" ");
}
else {
byte res = (_screen_resist - 1) * 4;

// Cargamos iconos
for (byte i = 0; i < 4; i++)
lcd.createChar(ICON_RESIST + i, resis_char[res + i]);

lcd.setCursor(LCD_POS_RES_L1_X, LCD_POS_RES_L1_Y);
lcd.printByte(ICON_RESIST);
lcd.printByte(ICON_RESIST + 1);
lcd.setCursor(LCD_POS_RES_L2_X, LCD_POS_RES_L2_Y);
lcd.printByte(ICON_RESIST + 2);
lcd.printByte(ICON_RESIST + 3);
}
screen_lines_write();
}

void screen_write_xy(int x, int y, char *msg, int len) {
lcd.setCursor(x, y);
lcd.print(msg); // TODO: len se deberia usar?
}

void screen_alarm(byte alarm){
lcd.setCursor(13, 0);
lcd.print(alarm);
lcd.printByte(ICON_BELL);
}
187 changes: 68 additions & 119 deletions include/hardware/lcd1602_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@
[email protected], [email protected]
2022
***************************************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <hardware/lcd1602_custom_char.h>

// 0 1 3 4 5 6 7 8 9 A B C D E F G
// ===================================
// 0: | [ ] * * 1 8 0 / 2 0 0 * * 0 0 b |
// 1: | [ ] * C A L E N T A N D O * * * |
// ===================================

#define LCD_POS_RES_L1_X 0
#define LCD_POS_RES_L1_Y 0
#define LCD_POS_RES_L2_X 0
#define LCD_POS_RES_L2_Y 1
#define LCD_POS_TXT_X 4
#define LCD_POS_TXT_Y 1
#define LCD_POS_TXT_LEN 12
#define LCD_POS_INFO_X 4
#define LCD_POS_INFO_Y 0
#define LCD_POS_INFO_LEN 12
#define LCD_POS_CTEMP_X 5
#define LCD_POS_CTEMP_Y 0
#define LCD_POS_CTEMP_LEN 3
#define LCD_POS_PTEMP_X 9
#define LCD_POS_PTEMP_Y 0
#define LCD_POS_PTEMP_LEN 3

#define ICON_RESIST 0 // RESERVED FROM 0 TO 3
#define ICON_BELL 4

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif

// LCD1602 I2C [ adress: 0x27 ]
LiquidCrystal_I2C lcd(0x27,16,2);

String _screen_text = "";
byte _screen_text_index = 0;
Expand All @@ -55,135 +17,122 @@ byte _screen_info_index_zero = 0;
int _screen_current_temp = 0;
int _screen_prog_temp = 0;

//==================================//
// FUNCTIONS: //
//==================================//
#define RESSTR_MONITOR "Mon "

void screen_init(){
lcd.init(); // initialize the lcd
lcd.createChar(ICON_BELL, bell);
lcd.home();
lcd.clear();
}
inline int min_i(int a, int b) { return a < b ? a : b; };

void screen_backlight(bool active){
if (active == true) lcd.backlight();
else lcd.noBacklight();
}
// Funciones que definiremos en cada driver de pantalla
void screen_lines_write();
void screen_clear();
void screen_resist_write();
void screen_write_xy(int x, int y, char *msg, int len);

void screen_clear(){
lcd.clear();
void screen_text_write() {
char aux[LCD_LEN_TXT + 1];
int len = min_i(_screen_text.length(), LCD_LEN_TXT);
_screen_text.substring(_screen_text_index).toCharArray(aux, len + 1);
for(int i = len; i < LCD_LEN_TXT; i++) aux[i] = ' ';
aux[LCD_LEN_TXT] = 0;
screen_write_xy(LCD_POS_TXT_X, LCD_POS_TXT_Y, aux, LCD_LEN_TXT);
screen_lines_write();
}

void screen_write_len(String msg, byte len){
if(msg.length() <= len) {
lcd.print(msg);
for(int i=msg.length(); i < len; i++) lcd.print(" ");
}
else lcd.print(msg.substring(0, len - 1));
void screen_info_write() {
char aux[LCD_LEN_INFO + 1];
//memset(aux, ' ', sizeof(aux));

if(_screen_info != "") {
int len = min_i(_screen_info.length(), LCD_LEN_INFO);
_screen_info.substring(_screen_info_index).toCharArray(aux, len + 1);
} else {
if(_screen_prog_temp != 0)
sprintf(aux, " %3d/%3d%s", _screen_current_temp, _screen_prog_temp, " C");
else
sprintf(aux, " %3d%s", _screen_current_temp, " C");

}

for(int i = strlen(aux); i < LCD_LEN_INFO; i++) aux[i] = ' ';
aux[LCD_LEN_INFO] = 0;

screen_write_xy(LCD_POS_INFO_X, LCD_POS_INFO_Y, aux, LCD_LEN_INFO);
screen_lines_write();
}

void screen_refresh(){
if(_screen_text.length() > LCD_POS_TXT_LEN) {
if(_screen_text.length() > LCD_LEN_TXT) {
_screen_text_index_zero++;
if (_screen_text_index_zero > 2){
_screen_text_index++;
if (_screen_text.length() - _screen_text_index + 1 < LCD_POS_TXT_LEN) {
if (_screen_text.length() - _screen_text_index < LCD_LEN_TXT) {
_screen_text_index = 0;
_screen_text_index_zero = 0;
}

lcd.setCursor(LCD_POS_TXT_X, LCD_POS_TXT_Y);
lcd.print(_screen_text.substring(_screen_text_index, _screen_text_index + LCD_POS_TXT_LEN - 1));
screen_text_write();
}
}

if(_screen_info.length() > LCD_POS_INFO_LEN) {
if(_screen_info.length() > LCD_LEN_INFO) {
_screen_info_index_zero++;
if (_screen_info_index_zero > 2){
_screen_info_index++;
if (_screen_info.length() - _screen_info_index + 1 < LCD_POS_INFO_LEN) {
if (_screen_info.length() - _screen_info_index < LCD_LEN_INFO) {
_screen_info_index = 0;
_screen_info_index_zero = 0;
}

lcd.setCursor(LCD_POS_INFO_X, LCD_POS_INFO_Y);
lcd.print(_screen_info.substring(_screen_info_index, _screen_info_index + LCD_POS_INFO_LEN - 1));
screen_info_write();
}
}
}

void screen_write(byte x, byte y, String msg){
lcd.setCursor(x, y);
lcd.print(msg);
char aux[LCD_LEN_LINE + 1];
int len = min_i(msg.length(), LCD_LEN_LINE);
msg.toCharArray(aux, len + 1);
screen_write_xy(x, y, aux, len);
screen_lines_write();
}

void screen_text(String msg){
_screen_text = msg;
_screen_text_index = 0;
_screen_text_index_zero = 0;

lcd.setCursor(LCD_POS_TXT_X, LCD_POS_TXT_Y);
screen_write_len(msg, LCD_POS_TXT_LEN);
_screen_text = msg;
_screen_text_index = 0;
_screen_text_index_zero = 0;
screen_text_write();
}

void screen_info(String msg){
_screen_info = msg;
_screen_info_index = 0;
_screen_info_index_zero = 0;

lcd.setCursor(LCD_POS_INFO_X, LCD_POS_INFO_Y);
screen_write_len(msg, LCD_POS_INFO_LEN);
_screen_info = msg;
_screen_info_index = 0;
_screen_info_index_zero = 0;
screen_info_write();
}

void screen_resistances(byte resist){
_screen_resist = resist;

if (resist == 0) {
lcd.setCursor(LCD_POS_RES_L1_X, LCD_POS_RES_L1_Y);
lcd.print(" ");
lcd.setCursor(LCD_POS_RES_L2_X, LCD_POS_RES_L2_Y);
lcd.print(" ");
}
else {
byte res = (resist - 1) * 4;

// Cargamos iconos
for (byte i = 0; i < 4; i++)
lcd.createChar(ICON_RESIST + i, resis_char[res + i]);

lcd.setCursor(LCD_POS_RES_L1_X, LCD_POS_RES_L1_Y);
lcd.printByte(ICON_RESIST);
lcd.printByte(ICON_RESIST + 1);
lcd.setCursor(LCD_POS_RES_L2_X, LCD_POS_RES_L2_Y);
lcd.printByte(ICON_RESIST + 2);
lcd.printByte(ICON_RESIST + 3);
}
_screen_resist = resist;
screen_resist_write();
}

void screen_print_temp(int ctemp, int ptemp){
char buff[9];
char buff[10];

if(ptemp != 0)
sprintf(buff, "%3d/%3d%s", ctemp, ptemp, " C");
else
sprintf(buff, " %3d%s", ctemp, " C");

if(ptemp != 0)
sprintf(buff, "%3d/%3d%s", ctemp, ptemp, " C");
else
sprintf(buff, " %3d%s", ctemp, " C");
buff[9] = 0;

screen_info(buff);
screen_info(buff);
}

void screen_current_temp(int temp){
_screen_current_temp = temp;
screen_print_temp(temp, _screen_prog_temp);
_screen_current_temp = temp;
screen_info_write();
}

void screen_prog_temp(int temp){
_screen_prog_temp = temp;
screen_print_temp(_screen_current_temp, temp);
_screen_prog_temp = temp;
screen_info_write();
}

void screen_alarm(byte alarm){
lcd.setCursor(13, 0);
lcd.print(alarm);
lcd.printByte(ICON_BELL);
}
35 changes: 35 additions & 0 deletions include/hardware/lcd1602_layout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/***************************************************************
openELECTRO
Home appliance control, based on arduino and other MPU
https://github.com/carlymx/openELECTRO
[email protected], [email protected]
2022
***************************************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// ===================================
// 0: | [ ] * * 1 8 0 / 2 0 0 * * 0 0 b |
// 1: | [ ] * C A L E N T A N D O * * * |
// ===================================

#define LCD_POS_RES_L1_X 0
#define LCD_POS_RES_L1_Y 0
#define LCD_POS_RES_L2_X 0
#define LCD_POS_RES_L2_Y 1
#define LCD_LEN_LINE 16
#define LCD_NUM_LINES 2
#define LCD_POS_TXT_X 3
#define LCD_POS_TXT_Y 1
#define LCD_LEN_TXT 12
#define LCD_POS_INFO_X 3
#define LCD_POS_INFO_Y 0
#define LCD_LEN_INFO 12
#define LCD_POS_CTEMP_X 5
#define LCD_POS_CTEMP_Y 0
#define LCD_LEN_CTEMP 3
#define LCD_POS_PTEMP_X 9
#define LCD_POS_PTEMP_Y 0
#define LCD_LEN_PTEMP 3
Loading

0 comments on commit e5bfe28

Please sign in to comment.