Fernando Morales / Mbed 2 deprecated LiquidCrystal_I2C_for_Nucleo

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
fernando_moraless
Date:
Mon Apr 25 05:52:27 2022 +0000
Parent:
1:b2c40f461dbd
Commit message:
Slave LCD

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 25 02:20:54 2022 +0000
+++ b/main.cpp	Mon Apr 25 05:52:27 2022 +0000
@@ -1,25 +1,74 @@
+//CÓDIGO 1-B
+
 #include "mbed.h"
+#include "stdlib.h"
 #include <LiquidCrystal_I2C.h>
+#include <iostream>
+#include <string>
 
-// Set the LCD address to 0x27 for a 16 chars and 2 line display
+Serial pc(USBTX, USBRX); // tx, rx
+
+//Los puertos SDA  y SD8 están referenciados en el archivo LiquidCrystal_I2C.cpp
+// SDA -> PTC9
+// SCL -> PTC8
+//                     DIR   TIPO
 LiquidCrystal_I2C lcd(0x4E, 16, 2);
 
+I2CSlave slave(I2C_SDA, I2C_SCL);
+
+char addr = 0xA0;
 
 
 int main()
 {
+    pc.printf("\x1b[2J");       //CLEAR
+    pc.printf("\033[1;1H");     //Mueve cursor al origen
+    
+   
+    char buf[50];
+    
  
-    // initialize the LCD
+    // Inicia la LCD
     lcd.begin();
-
-    // Turn on the blacklight and print a message.
+    
+    slave.address(addr);
+    slave.frequency (100000);
+    pc.printf("SLAVE: %d\r\n", addr);
+    
+    // Turn on the blacklight.
     lcd.backlight();
-    lcd.print("Hello, world!");
-    
+    lcd.clear();
+    lcd.print("SLAVE");
+    char msg[] = " *Si* ";
     while (1) 
     {
+        int i = slave.receive();
+        
+        switch (i) {
+            case I2CSlave::ReadAddressed:
+                slave.write(msg, strlen(msg) + 1); // Includes null char
+                slave.stop();
+                slave.receive();
+                break;
+            case I2CSlave::WriteGeneral:
+                slave.read(buf, 30);
+                printf("Read G: %s\n", buf);
+                slave.stop();
+                slave.receive();
+                break;
+            case I2CSlave::WriteAddressed:
+                slave.read(buf, 30);
+                pc.printf("Read A: %s\n", buf);
+                lcd.clear();
+                lcd.print(buf);
+                slave.stop();
+                slave.receive();
+                break;
+        }
+        for (int i = 0; i < 10; i++) {
+            buf[i] = 0;    // Clear buffer
+        }
        
     }
- 
 }
- 
+ 
\ No newline at end of file