Grupo 4 2020-1 / Mbed 2 deprecated Classe_LCD

Dependencies:   mbed TextLCD

Files at this revision

API Documentation at this revision

Comitter:
kaiquedog
Date:
Tue Jan 18 14:53:25 2022 +0000
Parent:
2:edc5d3177f86
Commit message:
Funcionando;

Changed in this revision

Joystick.cpp Show annotated file Show diff for this revision Revisions of this file
Joystick.h Show annotated file Show diff for this revision Revisions of this file
LCD_ka.cpp Show diff for this revision Revisions of this file
LCD_ka.h Show diff for this revision Revisions of this file
Telas.h Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Joystick.cpp	Tue Jan 18 14:53:25 2022 +0000
@@ -0,0 +1,51 @@
+#include "Joystick.h"
+
+Joystick::Joystick(PinName vrx, PinName vry, PinName Sw) : VRX(vrx), VRY(vry), Switch(Sw){
+}
+/*
+               ***************
+               **** LER_X ****
+               ***************
+*/               
+int Joystick::ler_x(void){
+    int a = 2;
+    if (VRX.read()*1000 > 800){
+        a = 1;
+    }
+    if (VRX.read()*1000 < 200){
+        a = 0;
+    }
+    return a;
+}
+
+/*
+               ***************
+               **** LER_Y ****
+               ***************
+*/               
+int Joystick::ler_y(void){
+    int a = 2;
+    if (VRY.read()*1000 > 800){
+        a = 1;                  
+    }
+    if (VRY.read()*1000 < 200){
+        a = 0;
+    }
+    return a;
+}
+
+/*
+               ********************
+               **** BOT_SELECT ****
+               ********************
+*/               
+int Joystick::bot_select(void){
+    int a = 2;
+    switch(Switch){
+        case 1: a = 1; break; // Botão desapertado
+        case 0: a= 0; break;  // Botão apertado
+    }
+    
+    return a;
+}
+        
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Joystick.h	Tue Jan 18 14:53:25 2022 +0000
@@ -0,0 +1,32 @@
+#ifndef JOYSTICK_H
+#define JOYSTICK_H
+#include "mbed.h"
+
+class Joystick {
+public:
+    Joystick(PinName vrx, PinName vry, PinName Sw);
+/*    
+               *******************************
+               **** DEFINIÇÕES DE FUNÇÕES ****
+               *******************************
+*/               
+// Função que retorna o valor de VRX     
+    int ler_x(); // 1 --> VRX > 800 || 0 --> VRX < 200
+    
+// Função que retorna o valor de VRX
+    int ler_y(); // 1 --> VRY > 800 || 0 --> VRY < 200
+    
+// Função que retorna 0 quando o botão for apertado
+    int bot_select();  
+    
+/*    
+               ********************************
+               **** DEFINIÇÕES DE ENTRADAS ****
+               ********************************
+*/    
+    AnalogIn VRX;     // Entrada analógica VRX
+    AnalogIn VRY;     // Entrada analógica VRX
+    DigitalIn Switch; // Entrada digital SW 
+};
+
+#endif
\ No newline at end of file
--- a/LCD_ka.cpp	Wed May 27 15:16:45 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,243 +0,0 @@
-#include "LCD_ka.h"
-
-//**** Define as portas que o LCD lê: ****
-LCD::LCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7) : RS(rs), E(e), dados(d4,d5,d6,d7){
-    Inicia_LCD();
-}
-
-
-int _coluna = 1;
-int _linha = 1;
-
-string LCD::Lepos(void){
-    string buffer;
-    char li[6];
-    sprintf(li,"(%d,%d)", _linha, _coluna);
-    buffer.append(li);
-    return buffer;
-    
-}
-
-//**** Comando que indica para o LCD que metade dos bits foram enviados: ****
-void LCD::bate_enable(void){
-    E = 1;
-    wait(0.000040f);
-    E=0;
-    wait(0.000040f);
-}
-
-
-//**** Função de inicialização do LCD: ****
-void LCD::Inicia_LCD(void){
-    RS = 0;
-    E = 0;
-    dados = 0x0;
-        
-    wait(0.05);
-    for (int i=0; i<3; i++){
-        // Define como 4 bits
-        dados = 0x2;
-        bate_enable(); 
-        
-        // Define 0 0 1 DL - N F _ _ 
-        //(DL = Data Lengh: 0 --> 4 Bits
-        //                  1 --> 8 Bits)
-        //(N = n° de linhas:0 --> 1 linha
-        //                  1 --> 2 linhas)
-        //(F = n° de pontos:0 --> 5 x 8 pontos
-        //                  1 --> 5 x 10 pontos)
-        dados = 0x2;
-        bate_enable();
-        dados = 0x8; 
-        bate_enable();
-        
-        wait(0.02);
-        
-        // Define 0 0 0 0 - 1 D C B 
-        //(D = Display on/off: 0 --> off
-        //                     1 --> on)
-        //(C = Cursor on/off : 0 --> off
-        //                     1 --> on)
-        //(B = Blink on/off  : 0 --> off
-        //                     1 --> on)
-        dados = 0x0;
-        bate_enable();
-        dados = 0xE;
-        bate_enable();
-        
-        wait(0.02);
-        
-        // Define 0 0 0 0 - 0 1 I/D S
-        //(I/D = Direção de escrita: 0 --> Direita
-        //                           1 --> Esquerda)
-        //(S = Display shift :       0 --> Não
-        //                           1 --> Sim)
-        dados = 0x0;
-        bate_enable();
-        dados = 0x6;
-        bate_enable();
-        }
-        
-        // Limpa o diplay
-        clc(); 
-}
-
-
-
-//**** Função que limpa o display e volta o cursor para (1,1): ****
-void LCD::clc(void){
-    wait(0.02);
-    
-    dados = 0x0;
-    bate_enable();
-    dados = 0x1;
-    bate_enable();
-    
-    wait(0.02);
-    
-    _coluna = 1;
-    _linha = 1;
-}
-
-
-//**** Função que volta o cursor para (1,1): ****
-void LCD::home(void){
-    wait(0.002);
-    
-    dados = 0x0;
-    bate_enable();
-    dados = 0x2;
-    bate_enable();
-    
-    wait(0.002);
-    
-    _coluna = 1;
-    _linha = 1;
-}
-
-
-//**** Função que manda o cursor para a direita: ****
-void LCD::direita(void){
-    wait(0.0002);
-    
-    dados = 0x1;
-    bate_enable();
-    dados = 0x4;
-    bate_enable();
-    
-    wait(0.0002);
-    
-    _coluna = _coluna+1;
-    
-    fim_de_curso(); 
-}
-
-
-//**** Função quem manda o cursor para a esquerda: ****
-void LCD::esquerda(void){
-    wait(0.0002);
-    
-    dados = 0x1;
-    bate_enable();
-    dados = 0x0;
-    bate_enable();
-    
-    wait(0.0002);
-    
-    _coluna = _coluna - 1;
-    
-    fim_de_curso();
-}
-
-
-//**** Função que apaga o que está no cursor: ****
-void LCD::apaga(void){
-    RS = 1;
-    
-    wait(0.002);
-    
-    dados = 0x2;
-    bate_enable();
-    dados = 0x0;
-    bate_enable();
-    
-    RS = 0;
-    
-    pos(_linha,_coluna);
-}
-
-
-//**** Função que manda o display para linha de cima: ****
-void LCD::cima(void){
-    pos(1,_coluna);
-
-    _linha = 1;
-}
-
-
-//**** Função que manda o display para linha de baixo: ****
-void LCD::baixo(void){
-    pos(2,_coluna);
-    
-    _linha = 2;
-}
-
-
-//**** Função que posiciona o cursor na linha e coluna inseridas: ****
-void LCD::pos(int li, int co){
-    int POSI = 0x80 + ((li-1) * 0x40) + (co-1);
-    
-    dados = POSI>>4;
-    bate_enable();
-    dados = POSI;
-    bate_enable();
-    
-    _coluna = co;
-    _linha = li;
-}   
-
-
-//**** Função que define se o cursor saiu do display: ****
-void LCD::fim_de_curso (void){
-    switch (_coluna)
-    {
-        case 17 : 
-        switch (_linha){
-            case 1 : pos(2,1); break;
-            case 2 : home(); break;
-        }
-        break;
-        case 0 :
-        switch (_linha){
-            case 1 : pos(2,16); break;
-            case 2 : pos(1,16); break;
-        }
-        break;
-    }
-}
-    
-    
- 
- 
- 
-int LCD::_putc(int value) {
-    if (value == '\n') {
-        baixo();
-    }else{
-        RS = 1;
-        dados = value>>4;
-        bate_enable();
-        dados = value;
-        bate_enable();
-        RS = 0;
-    }
-    _chars[_linha-1][_coluna-1] = value;
-    _coluna++;
-    fim_de_curso();
-    
-    return value;
-}
-
-int LCD::_getc() {
-    return -1;
-}
\ No newline at end of file
--- a/LCD_ka.h	Wed May 27 15:16:45 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,71 +0,0 @@
-#ifndef LCD_H
-#define LCD_H
-#include "mbed.h"
-#include <string>
-#include <stdio.h>
-class LCD : public Stream {
-public:
-    LCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7);
-    
-    string Lepos(void);
-    
-// Comando que indica para o LCD que metade dos bits foram enviados:
-    void bate_enable(void);
-
-
-// Função de inicialização do LCD:
-    void Inicia_LCD(void);
-
-
-// Função que recebe string e escreve no display:
-    void disp(char* value);
-
-
-// Função que limpa o display e volta o cursor para (1,1):
-    void clc(void);
-
-
-// Função que volta o cursor para (1,1):
-    void home(void);
-
-
-// Função que manda o cursor para a direita:
-    void direita(void);
-
-
-// Função quem manda o cursor para a esquerda:
-    void esquerda(void);
-
-
-// Função que apaga o que está no cursor:
-    void apaga(void);
-
-
-// Função que manda o display para linha de cima:
-    void cima(void);
-
-
-// Função que manda o display para linha de baixo:
-    void baixo(void);
-
-
-// Função que posiciona o cursor na linha e coluna inseridas:
-    void pos(int linha, int coluna);
-
-
-// Função que define se o cursor saiu do display:
-    void fim_de_curso(void);
-
-//**** Variáveis que o código utiliza para funções internas: ****    
-    DigitalOut RS, E;
-    BusOut dados;
-    int _coluna;
-    int _linha;
-    char _chars[2][16];
-
-// Stream implementation functions
-    virtual int _putc(int value);
-    virtual int _getc();
-};
-  
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Telas.h	Tue Jan 18 14:53:25 2022 +0000
@@ -0,0 +1,70 @@
+/******************************************************************************\
+ INFORMAÇÕES IMPORTANTES:
+ 
+    - O endereço padrão para o I2C é 0x7E
+    - Para utilizar o I2C:
+        - Configurar no arquivo TextLCD_Config.h a partir da linha 71:
+        #define DEFAULT        0
+        (...)
+        #define YWROBOT        1
+        (...)
+    - Site para criar caracteres customizados:
+    https://maxpromer.github.io/LCD-Character-Creator/
+        
+\******************************************************************************/
+
+
+// I2C Communication
+I2C i2c_lcd(PB_9,PB_8); // SDA, SCL
+
+TextLCD_I2C lcd(&i2c_lcd, 0x7E, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type
+
+    
+void LCD_Init(){
+    
+// Turn Backlight on    
+    lcd.setBacklight(TextLCD::LightOn);
+    
+// Set Cusor Blink off and Blink on
+    lcd.setCursor(TextLCD::CurOff_BlkOn);
+    
+// Clear screen
+    lcd.cls();
+    
+}
+    
+void Tela_0(){
+    lcd.locate(0,0);
+    lcd.printf("> Pagina 1");
+    lcd.locate(0,1);
+    lcd.printf("> Pagina 2");
+    lcd.locate(0,2);
+    lcd.printf("> Pagina 3");
+    lcd.locate(0,3);
+    lcd.printf("> Pagina 4");
+    lcd.locate(0,0);    
+}
+void Tela_1(){
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("Ola Mundo!");
+    lcd.locate(0,0);    
+}
+void Tela_2(){
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("Bom dia Silvio!");
+    lcd.locate(0,0);    
+}
+void Tela_3(){
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("Como vai voce?");
+    lcd.locate(0,0);    
+}
+void Tela_4(){
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("E isso ai!");
+    lcd.locate(0,0);    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Tue Jan 18 14:53:25 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/kaiquedog/code/TextLCD/#19a68df2a72a
--- a/main.cpp	Wed May 27 15:16:45 2020 +0000
+++ b/main.cpp	Tue Jan 18 14:53:25 2022 +0000
@@ -1,61 +1,54 @@
 #include "mbed.h"
-#include "LCD_ka.h"
-LCD lcd(D8, D9, D4, D5, D6, D7);
-AnalogIn Buts(A0);
-AnalogIn Ya(A1);
-AnalogIn Xa(A2);
+#include "TextLCD.h"
+#include "Joystick.h"
+#include "Telas.h"
+
+Joystick Joy (A2, A3, USER_BUTTON); // VRX, VRY, SW
+
+
+
 
-int main(){
-    int y = Ya.read()*1000;
-    int x = Xa.read()*1000;
-    while(1){    
-        y = Ya.read()*1000;
-        x = Xa.read()*1000;
-        
-        if ((Buts.read()*1000 > 750) && (Buts.read()*1000 < 850)){
-            lcd.printf("select");
-            wait(0.5);
-        }
-        if ((Buts.read()*1000 > 400) && (Buts.read()*1000 < 500)){
-            lcd.printf("left");
-            wait(0.5);
-        }
-        if ((Buts.read()*1000 > 200) && (Buts.read()*1000 < 300)){
-            lcd.printf("down");
-            wait(0.5);
-        }
-        if ((Buts.read()*1000 > 60) && (Buts.read()*1000 < 150)){
-            lcd.printf("up");
-            wait(0.5);
+int main() {
+    LCD_Init();
+    Tela_0();
+    
+    int row = 0;
+    int col = 0;
+    while(1){
+        switch (Joy.ler_x()) // Rotina para movimentar eixo X
+        {
+            case 0: 
+                if (row == 0){ row = 0; } else{ row--;}
+                lcd.locate(col,row);
+                wait(0.2); 
+                break;
+            case 1: 
+                if (row == 3){ row = 3; } else{ row++;}
+                lcd.locate(col,row);
+                wait(0.2); 
+                break;
         }
-        if ((Buts.read()*1000 > 0) && (Buts.read()*1000 < 50)){
-            lcd.printf("right");
-            wait(0.5);
-        }
-        
-        if((x < 400)||(x > 600)||(y < 400)||(y > 600)){
-            y = Ya.read()*1000;
-            x = Xa.read()*1000;
-            
-            if (x > 900){
-                lcd.direita();
-                wait(0.2);
+        if (Joy.bot_select() == 0){
+            switch (row){
+                case 0: Tela_1(); break;
+                case 1: Tela_2(); break;
+                case 2: Tela_3(); break;
+                case 3: Tela_4(); break;
             }
-            
-            if (x < 100){
-                lcd.esquerda();
-                wait(0.2);
-            }
-            if (y > 900){
-                lcd.baixo();
-                wait(0.2);
-            }
-            if (y < 100){
-                lcd.cima();
-                wait(0.2);
-            }    
         }
-            
-        
+
     }
-}
+
+//// Create custom characters
+//    const char udc_I1[]   = {0x0A, 0x1F, 0x0E, 0x04, 0x00, 0x00, 0x00, 0x00};
+//    
+//// Set custom char to address 0
+//    lcd.setUDC(0, (char *) udc_I1);
+//    
+//// Go to col=3 row=0
+//    lcd.locate(3,0);
+//    
+//// Print the custom char
+//    lcd.putc(0);  
+
+}
\ No newline at end of file