character LCD module to I2C adapter http://mbed.org/users/okini3939/notebook/i2c-lcd-library/

Files at this revision

API Documentation at this revision

Comitter:
Jordan406
Date:
Wed Mar 04 15:23:41 2020 +0000
Parent:
2:bc4583ce560e
Commit message:
okok

Changed in this revision

I2CLCD.cpp Show diff for this revision Revisions of this file
I2CLCD.h Show diff for this revision Revisions of this file
Pixy2_Library.h Show annotated file Show diff for this revision Revisions of this file
Pixy2_Libray.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/I2CLCD.cpp	Sun Feb 27 14:28:40 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,200 +0,0 @@
-/*
- * mbed library for I2C LCD
- * Copyright (c) 2011 Hiroshi Suga
- * Released under the MIT License: http://mbed.org/license/mit
- *
- * This product includes:
- * mbed TextLCD Library, for a 4-bit LCD based on HD44780
- * Copyright (c) 2007-2010, sford
- */
-
-/** @file I2CLCD.cpp
- * @brief I2C LCD library (mbed Phone Platform)
- */
-
-#include "mbed.h"
-#include "I2CLCD.h"
-
-/**
- * @brief put character to LCD
- * @param value ASCII character code
- * @retval value
- */
-int I2CLCD::_putc (int value) {
-
-    if (value == '\n') {
-        x = 0;
-        y ++;
-        if (y >= rows()) {
-            y = 0;
-            lcd_out(address(x, y), 0);
-        }
-
-    } else {
-
-        lcd_out(address(x, y), 0);
-        lcd_out(value, 1);
-        x ++;
-        if (x >= cols()) {
-            x = 0;
-            y ++;
-            if (y >= rows()) {
-                y = 0;
-                lcd_out(address(x, y), 0);
-            }
-        }
-    }
-
-    return value;
-}
-
-/**
- * @brief get character from LCD
- * @retval ASCII character code
- */
-int I2CLCD::_getc() {
-    return lcd_in(0);
-}
-
-
-/**
- * @brief put character to LCD
- * @param p_sda port of I2C SDA
- * @param p_scl port of I2C SCL
- * @param p_i2caddr I2C address
- */
-I2CLCD::I2CLCD (PinName p_sda, PinName p_scl, int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_sda, p_scl) {
-    init(p_i2caddr, p_type, p_config);
-}
-
-/**
- * @brief put character to LCD
- * @param p_i2c instance of I2C class
- * @param p_i2caddr I2C address
- */
-I2CLCD::I2CLCD (I2C& p_i2c, int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_i2c) {
-    init(p_i2caddr, p_type, p_config);
-}
-
-void I2CLCD::init (int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) {
-
-    i2caddr = p_i2caddr;
-    type = p_type;
-
-    lcd_cfg(p_config);
-
-    wait_ms(500);
-    lcd_out(0x30, 0);
-    wait_ms(5);
-    lcd_out(0x30, 0);
-    wait_ms(2);
-    lcd_out(0x30, 0);
-
-    lcd_out(0x38, 0); // func 
-    lcd_out(0x10, 0); // shift
-    lcd_out(0x0c, 0); // display
-    lcd_out(0x06, 0); // entry mode
-    cls();
-}
-
-void I2CLCD::cls() {
-    lcd_out(0x01, 0); // clear
-    wait_ms(2);
-    lcd_out(0x02, 0); // home
-    wait_ms(2);
-    locate(0, 0);
-}
-
-void I2CLCD::locate(int col, int row) {
-    x = col;
-    y = row;
-    lcd_out(address(x, y), 0);
-}
-
-int I2CLCD::address(int col, int row) {
-    switch (type) {
-        case LCD16x1:
-            return (col < 8 ? 0x80 : 0xc0) + (col & 0x03);
-        case LCD16x4:
-        case LCD20x4:
-            switch (row) {
-                case 0:
-                    return 0x80 + col;
-                case 1:
-                    return 0xc0 + col;
-                case 2:
-                    return 0x94 + col;
-                case 3:
-                    return 0xd4 + col;
-            }
-        case LCD16x2B:
-            return 0x80 + (row * 40) + col;
-        case LCD8x2:
-        case LCD16x2:
-        case LCD20x2:
-        default:
-            return 0x80 + (row * 0x40) + col;
-    }
-}
-
-int I2CLCD::cols() {
-    switch (type) {
-        case LCD8x2:
-            return 8;
-        case LCD20x4:
-        case LCD20x2:
-            return 20;
-        case LCD16x1:
-        case LCD16x2:
-        case LCD16x2B:
-        case LCD16x4:
-        default:
-            return 16;
-    }
-}
-
-int I2CLCD::rows() {
-    switch (type) {
-        case LCD16x1:
-            return 1;
-        case LCD16x4:
-        case LCD20x4:
-            return 4;
-        case LCD8x2:
-        case LCD16x2:
-        case LCD16x2B:
-        case LCD20x2:
-        default:
-            return 2;
-    }
-}
-
-void I2CLCD::lcd_cfg (I2CLCDConfig cfg) {
-    i2c.start();
-    i2c.write(i2caddr);
-    i2c.write(LCDCFG_ENABLE | (cfg & 0x1f));
-    i2c.stop();
-}
-
-void I2CLCD::lcd_out (char dat, char rs) {
-    i2c.start();
-    i2c.write(i2caddr);
-    i2c.write(rs ? 0x40 : 0);
-    i2c.write(dat);
-    i2c.stop();
-}
-
-char I2CLCD::lcd_in (char rs) {
-    char i;
-
-    i2c.start();
-    i2c.write(i2caddr);
-    i2c.write(rs ? 0x40 : 0);
-
-    i2c.start();
-    i2c.write(i2caddr | 0x01);
-    i = i2c.read(0);
-    i2c.stop();
-
-    return i;
-}
--- a/I2CLCD.h	Sun Feb 27 14:28:40 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,79 +0,0 @@
-/*
- * mbed library for I2C LCD
- * Copyright (c) 2011 Hiroshi Suga
- * Released under the MIT License: http://mbed.org/license/mit
- *
- * This product includes:
- * mbed TextLCD Library, for a 4-bit LCD based on HD44780
- * Copyright (c) 2007-2010, sford
- */
-
-/** @file I2CLCD.h
- * @brief I2C LCD library (mbed Phone Platform)
- */
- 
-#ifndef I2CLCD_H
-#define I2CLCD_H
-
-#include "mbed.h"
-
-/**
- * @brief default I2C address
- */
-#define I2CLCD_ADDR 0x7c
-
-/**
- * @brief LCD type
- */
-enum I2CLCDType {
-    LCD8x2,
-    LCD16x1,
-    LCD16x2,
-    LCD16x2B,
-    LCD16x4,
-    LCD20x2,
-    LCD20x4
-};
-
-/**
- * @brief LCD config
- */
-enum I2CLCDConfig {
-    LCDCFG_ENABLE   = 0x20,
-    LCDCFG_PWMCOUNT = 0x10,
-    LCDCFG_LED      = 0x08,
-    LCDCFG_3V       = 0x04,
-    LCDCFG_ADDR     = 0x02,
-    LCDCFG_INIT     = 0x01
-};
-
-/**
- * @brief I2CLCD class
- */
-class I2CLCD : public Stream {
-public:
-    I2CLCD (PinName p_sda, PinName p_scl, int p_i2caddr = I2CLCD_ADDR, I2CLCDType p_type = LCD16x2, I2CLCDConfig p_config = LCDCFG_3V);
-    I2CLCD (I2C& p_i2c, int p_i2caddr = I2CLCD_ADDR, I2CLCDType p_type = LCD16x2, I2CLCDConfig p_config = LCDCFG_3V);
-
-    void locate (int, int);
-    void cls ();
-    void lcd_cfg (I2CLCDConfig);
-
-protected:
-    virtual int _putc (int);
-    virtual int _getc ();
-
-    int address (int, int);
-    int rows ();
-    int cols ();
-    void init (int, I2CLCDType, I2CLCDConfig);
-    void lcd_out (char, char);
-    char lcd_in (char);
-
-    I2C i2c;
-    int i2caddr;
-    I2CLCDType type;
-    int x, y;
-};
-
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pixy2_Library.h	Wed Mar 04 15:23:41 2020 +0000
@@ -0,0 +1,30 @@
+/*
+ * mbed library for Pixy2 Camera
+ * Copyright (c) 2019 Jordan DUCHÊNE
+ */
+
+
+ 
+#ifndef Pixy2_Library_H
+#define Pixy2_Library_H
+#include "mbed.h"
+
+/**
+  Pixy2_Library class
+ */
+class Pixy2_Library {
+
+    I2C* composant;
+    int I2Caddress;
+    
+public:
+
+    Pixy2_Library (PinName p_sda, PinName p_scl, int address);
+    void Pixy2_WhiteLED(bool);
+    void Pixy2_SetServo(int,int);
+    void Pixy2_RGBLED(bool);
+    int Pixy2_GetBlock(char,int);
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pixy2_Libray.cpp	Wed Mar 04 15:23:41 2020 +0000
@@ -0,0 +1,123 @@
+/*
+ * mbed library for Pixy2 Camera
+ * Copyright (c) 2019 Jordan DUCHÊNE
+ */
+
+#include "mbed.h"
+#include "Pixy2_Library.h"
+
+
+
+
+Pixy2_Library::Pixy2_Library (PinName p_sda, PinName p_scl, int address)   // Class Constructor
+{
+    composant = new I2C(p_sda,p_scl);  //sda, sc1
+    composant ->frequency(100000);
+    I2Caddress = address;
+}
+
+
+void Pixy2_Library::Pixy2_WhiteLED(bool STATE)   // White LEDs BuiltIN ON/OFF methode
+{
+
+    unsigned char readdata[21];
+    unsigned char lampeON[6]= {0xae,0xc1,22,2,1,0};
+    unsigned char lampeOFF[6]= {0xae,0xc1,22,2,0,0};
+    if(STATE == true) {
+        do {
+            composant->write(I2Caddress,(char*)lampeON,6);
+            composant->read(I2Caddress,(char*)readdata,10);
+            wait(0.01);
+        } while(readdata[0] != 175 && readdata[1] != 193);
+    } else {
+        do {
+            composant->write(I2Caddress,(char*)lampeOFF,6);
+            composant->read(I2Caddress,(char*)readdata,10);
+            wait(0.01);
+        } while(readdata[0] != 175 && readdata[1] != 193);
+    }
+
+}
+void Pixy2_Library::Pixy2_SetServo(int PAN, int TILT)   // Head Servo Crontrol methode
+{
+    int lowP, hightP, lowT, hightT;
+    hightP = PAN >> 8 ;
+    lowP = PAN & 0x0f;
+    hightT = TILT >> 8 ;
+    lowT = TILT & 0x0f;
+    unsigned char readdata[21];
+    unsigned char SetServ[8]= {0xae,0xc1,18,4,lowP,hightP,lowT,hightP};
+    do {
+        composant->write(I2Caddress,(char*)SetServ,6);
+        composant->read(I2Caddress,(char*)readdata,10);
+        wait(0.01);
+    } while(readdata[0] != 175 && readdata[1] != 193);
+}
+
+
+void Pixy2_Library::Pixy2_RGBLED(bool STATE)
+{
+
+    unsigned char readdata[21];
+    unsigned char lampeON[6]= {0xae,0xc1,22,2,0,1};
+    unsigned char lampeOFF[6]= {0xae,0xc1,22,2,0,0};
+    if(STATE == true) {
+        do {
+            composant->write(I2Caddress,(char*)lampeON,6);
+            composant->read(I2Caddress,(char*)readdata,10);
+            wait(0.01);
+        } while(readdata[0] != 175 && readdata[1] != 193);
+    } else {
+        do {
+            composant->write(I2Caddress,(char*)lampeOFF,6);
+            composant->read(I2Caddress,(char*)readdata,10);
+            wait(0.01);
+        } while(readdata[0] != 175 && readdata[1] != 193);
+    }
+
+}
+
+int Pixy2_Library::Pixy2_GetBlock(char DATA, int BLOCK_NUMBER)
+{
+    int DataSize = BLOCK_NUMBER * 14 + 6;
+    unsigned char writedata[6]= {0xae,0xc1,32,2,255,20};
+    unsigned char readdata[DataSize + 1];
+    do {
+        composant->write(I2Caddress,(char*)writedata,6);
+        composant->read(I2Caddress,(char*)readdata,DataSize);
+        wait(0.1);
+    } while(readdata[0] != 175 && readdata[1] != 193);
+    switch(DATA) {
+        case 's':
+            if(readdata[DataSize - 13]<2) return readdata[7]*256+readdata[DataSize - 14];
+            if(readdata[DataSize - 13]>=2) return -2;
+            break;
+        case 'x':
+            if(readdata[DataSize - 11]<2) return readdata[DataSize - 11]*256+readdata[DataSize - 12];
+            if(readdata[DataSize - 11]>=2) return -2;
+            break;
+        case 'y':
+            if(readdata[DataSize - 9]<2) return readdata[DataSize - 9]*256+readdata[DataSize - 10];
+            if(readdata[DataSize - 9]>=2) return -2;
+            break;
+        case 'w':
+            if(readdata[DataSize - 7]<2) return readdata[DataSize - 7]*256+readdata[DataSize - 8];
+            if(readdata[DataSize - 7]>=2) return -2;
+            break;
+        case 'h':
+            if(readdata[DataSize - 5]<2) return readdata[DataSize - 5]*256+readdata[DataSize - 6];
+            if(readdata[DataSize - 5]>=2) return -2;
+            break;
+        case 'a':
+            if(readdata[DataSize - 3]<2) return readdata[DataSize - 3]*256+readdata[DataSize - 4];
+            if(readdata[DataSize - 3]>=2) return -2;
+            break;
+        case 'i':
+            if(readdata[DataSize - 1]<2) return readdata[DataSize - 1]*256+readdata[DataSize - 2];                                //////////////////////// 1
+            if(readdata[DataSize - 1]>=2) return -2;
+            break;
+    }
+}
+
+
+