Basically i glued Peter Drescher and Simon Ford libs in a GraphicsDisplay class, then derived TFT or LCD class (which inherits Protocols class), then the most derived ones (Inits), which are per-display and are the only part needed to be adapted to diff hw.
Dependents: testUniGraphic_150217 maze_TFT_MMA8451Q TFT_test_frdm-kl25z TFT_test_NUCLEO-F411RE ... more
Revision 33:f87f06292637, committed 2017-02-06
- Comitter:
- dreschpe
- Date:
- Mon Feb 06 12:29:33 2017 +0000
- Parent:
- 32:9cd6227dc7a6
- Child:
- 34:c66986d80f72
- Commit message:
- Add I2C protocol. ; Add SSD1306 i2C modus
Changed in this revision
--- a/Display/LCD.cpp Mon May 02 22:44:05 2016 +0000
+++ b/Display/LCD.cpp Mon Feb 06 12:29:33 2017 +0000
@@ -95,6 +95,24 @@
// locate(0,0);
}
+LCD::LCD(proto_t displayproto, int Hz, int address, PinName sda, PinName scl, const int lcdsize_x, const int lcdsize_y, const int ic_x_segs, const int ic_y_coms, const char* name)
+ : GraphicsDisplay(name), screensize_X(lcdsize_x), screensize_Y(lcdsize_y), _LCDPAGES(lcdsize_y>>3), _IC_X_SEGS(ic_x_segs), _IC_Y_COMS(ic_y_coms), _IC_PAGES(ic_y_coms>>3)
+{
+ if(displayproto==I2C_){
+ proto = new I2C_bus(Hz,address,sda,scl);
+ useNOP=false;
+ }
+ buffer = (unsigned char*) malloc (screensize_X*_LCDPAGES);
+ buffer16 = (unsigned short*)buffer;
+ draw_mode = NORMAL;
+ // cls();
+ set_orientation(1);
+ foreground(White);
+ background(Black);
+ set_auto_up(true);
+ tftID=0;
+}
+
LCD::~LCD()
{
free(buffer);
--- a/Display/LCD.h Mon May 02 22:44:05 2016 +0000
+++ b/Display/LCD.h Mon Feb 06 12:29:33 2017 +0000
@@ -10,6 +10,7 @@
#include "BUS8.h"
#include "SPI8.h"
#include "SPI16.h"
+#include "I2C_bus.h"
#include "Protocols.h"
#include "GraphicsDisplay.h"
@@ -51,6 +52,12 @@
*/
LCD(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const int lcdsize_x, const int lcdsize_y, const int ic_x_segs, const int ic_y_coms, const char* name);
+ /** Create a monochrome LCD I2C interface
+ * @param name The name used by the parent class to access the interface
+ */
+ LCD(proto_t displayproto, int Hz, int address,PinName sda, PinName scl, const int lcdsize_x, const int lcdsize_y, const int ic_x_segs, const int ic_y_coms, const char* name);
+
+
/** Destructor
* will free framebuffer
*/
--- a/Inits/SSD1306.cpp Mon May 02 22:44:05 2016 +0000
+++ b/Inits/SSD1306.cpp Mon Feb 06 12:29:33 2017 +0000
@@ -56,6 +56,17 @@
set_orientation(1);
locate(0,0);
}
+
+SSD1306::SSD1306(proto_t displayproto, int Hz, int address, PinName sda, PinName scl, const char* name , unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y)
+ : LCD(displayproto, Hz, address, sda, scl, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
+{
+ init();
+ cls();
+ set_orientation(1);
+ locate(0,0);
+}
+
+
// reset and init the lcd controller
void SSD1306::init()
{
--- a/Inits/SSD1306.h Mon May 02 22:44:05 2016 +0000
+++ b/Inits/SSD1306.h Mon Feb 06 12:29:33 2017 +0000
@@ -41,6 +41,20 @@
*/
SSD1306(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const char* name , unsigned int LCDSIZE_X = 128, unsigned int LCDSIZE_Y = 64);
+ /** Create an I2C display interface
+ * @param displayproto I2C_
+ * @param Hz I2C speed in Hz
+ * @param address I2C address
+ * @param sda I2C pin
+ * @param scl I2C pin
+ * @param name The name used by the parent class to access the interface
+ * @param LCDSIZE_X x size in pixel - optional
+ * @param LCDSIZE_Y y size in pixel - optional
+ */
+ SSD1306(proto_t displayproto, int Hz, int address, PinName sda, PinName scl, const char* name , unsigned int LCDSIZE_X = 128, unsigned int LCDSIZE_Y = 64);
+
+
+
/** set the contrast of the screen
* @note here overrided because of not standard value range
* @param o contrast 0-255
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Protocols/I2C_bus.cpp Mon Feb 06 12:29:33 2017 +0000
@@ -0,0 +1,132 @@
+/* mbed UniGraphic library - I2C protocol class
+ * Copyright (c) 2017 Peter Drescher
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "I2C_bus.h"
+
+I2C_bus::I2C_bus(int Hz, int address, PinName sda, PinName scl)
+ : _i2c(sda,scl)
+{
+ _i2c.frequency(Hz);
+ _address = address;
+ //hw_reset();
+}
+
+void I2C_bus::wr_cmd8(unsigned char cmd)
+{
+ char tmp[2];
+ tmp[0] = 0x00; //command
+ tmp[1] = cmd;
+ _i2c.write(_address,tmp,2);
+}
+void I2C_bus::wr_data8(unsigned char data)
+{
+ _i2c.write(data); // write 8bit
+}
+void I2C_bus::wr_cmd16(unsigned short cmd)
+{
+ char tmp[3];
+ tmp[0] = 00; //command
+ tmp[1] = cmd>>8;
+ tmp[2] = cmd&0xFF;
+
+ _i2c.write(_address,tmp,3);
+}
+void I2C_bus::wr_data16(unsigned short data)
+{
+ _i2c.write(data>>8); // write 8bit
+ _i2c.write(data&0xFF); // write 8bit
+}
+void I2C_bus::wr_gram(unsigned short data)
+{
+ _i2c.write(data>>8); // write 8bit
+ _i2c.write(data&0xFF); // write 8bit
+}
+void I2C_bus::wr_gram(unsigned short data, unsigned int count)
+{
+ _i2c.start();
+ _i2c.write(_address);
+ _i2c.write(0x40); // data continue
+ if((data>>8)==(data&0xFF))
+ {
+ count<<=1;
+ while(count)
+ {
+ _i2c.write(data); // write 8bit
+ count--;
+ }
+ }
+ else
+ {
+ while(count)
+ {
+ _i2c.write(data>>8); // write 8bit
+ _i2c.write(data&0xFF); // write 8bit
+ count--;
+ }
+ }
+ _i2c.stop();
+}
+void I2C_bus::wr_grambuf(unsigned short* data, unsigned int lenght)
+{
+ _i2c.start();
+ _i2c.write(_address);
+ _i2c.write(0x40); // data continue
+ while(lenght)
+ {
+ _i2c.write((*data)>>8); // write 8bit
+ _i2c.write((*data)&0xFF); // write 8bit
+ data++;
+ lenght--;
+ }
+ _i2c.stop();
+}
+
+void I2C_bus::hw_reset()
+{
+
+}
+void I2C_bus::BusEnable(bool enable)
+{
+}
+
+void I2C_bus::reg_select(unsigned char reg, bool forread)
+{
+}
+
+unsigned int I2C_bus::rd_reg_data32(unsigned char reg)
+{
+ return 0;
+}
+
+unsigned int I2C_bus::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
+{
+ return 0;
+}
+
+void I2C_bus::dummyread()
+{
+}
+
+unsigned short I2C_bus::rd_gram(bool convert)
+{
+ return (0);
+}
+
+unsigned short I2C_bus::reg_read(unsigned char reg)
+{
+ return (0);
+}
+
+void I2C_bus::reg_write(unsigned char reg, unsigned short data)
+{
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Protocols/I2C_bus.h Mon Feb 06 12:29:33 2017 +0000
@@ -0,0 +1,139 @@
+#ifndef I2C_bus_H
+#define I2C_bus_H
+
+#include "mbed.h"
+#include "Protocols.h"
+
+/** I2C interface
+*/
+class I2C_bus : public Protocols
+{
+ public:
+
+ /** Create an I2C display interface
+ *
+ * @param I2C frquency
+ * @param I2C address
+ * @param I2C pin sda
+ * @param I2C pin scl
+ */
+ I2C_bus(int Hz, int address,PinName sda, PinName scl);
+
+protected:
+
+ /** Send 8bit command to display controller
+ *
+ * @param cmd: byte to send
+ *
+ */
+ virtual void wr_cmd8(unsigned char cmd);
+
+ /** Send 8bit data to display controller
+ *
+ * @param data: byte to send
+ *
+ */
+ virtual void wr_data8(unsigned char data);
+
+ /** Send 2x8bit command to display controller
+ *
+ * @param cmd: halfword to send
+ * @note in SPI_16 mode a single 16bit transfer will be done
+ */
+ virtual void wr_cmd16(unsigned short cmd);
+
+ /** Send 2x8bit data to display controller
+ *
+ * @param data: halfword to send
+ * @note in SPI_16 mode a single 16bit transfer will be done
+ */
+ virtual void wr_data16(unsigned short data);
+
+ /** Send 16bit pixeldata to display controller
+ *
+ * @param data: halfword to send
+ *
+ */
+ virtual void wr_gram(unsigned short data);
+
+ /** Send same 16bit pixeldata to display controller multiple times
+ *
+ * @param data: halfword to send
+ * @param count: how many
+ *
+ */
+ virtual void wr_gram(unsigned short data, unsigned int count);
+
+ /** Send array of pixeldata shorts to display controller
+ *
+ * @param data: unsigned short pixeldata array
+ * @param lenght: lenght (in shorts)
+ *
+ */
+ virtual void wr_grambuf(unsigned short* data, unsigned int lenght);
+
+ /** Read 16bit pixeldata from display controller (with dummy cycle)
+ *
+ * @param convert true/false. Convert 18bit to 16bit, some controllers returns 18bit
+ * @returns 16bit color
+ */
+ virtual unsigned short rd_gram(bool convert);
+
+ /** Read 4x8bit register data (
+ * reading from display ia I2C is not implemented in most controllers !
+ *
+ */
+ virtual unsigned int rd_reg_data32(unsigned char reg);
+
+ /** Read 3x8bit ExtendedCommands register data
+ * reading from display ia I2C is not implemented in most controllers !
+ */
+ virtual unsigned int rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd);
+
+ /** ILI932x specific, does a dummy read cycle, number of bits is protocol dependent
+ * reading from display ia I2C is not implemented in most controllers !
+ */
+ virtual void dummyread ();
+
+ /** ILI932x specific, select register for a successive write or read
+ *
+ * reading from display ia I2C is not implemented in most controllers !
+ */
+ virtual void reg_select(unsigned char reg, bool forread =false);
+
+ /** ILI932x specific, write register with data
+ *
+ * @param reg register to write
+ * @param data 16bit data
+ * not implemented for I2C !
+ */
+ virtual void reg_write(unsigned char reg, unsigned short data);
+
+ /** ILI932x specific, read register
+ *
+ * @param reg register to be read
+ * @returns 16bit register value
+ * not implemented for I2C !
+ */
+ virtual unsigned short reg_read(unsigned char reg);
+
+ /** HW reset sequence (without display init commands)
+ * most I2C displays have no reset signal !
+ */
+ virtual void hw_reset();
+
+ /** Set ChipSelect high or low
+ * @param enable 0/1
+ * not implemented for I2C !
+ */
+ virtual void BusEnable(bool enable);
+
+private:
+
+ I2C _i2c;
+ int _address;
+
+};
+
+
+#endif
\ No newline at end of file
--- a/Protocols/Protocols.h Mon May 02 22:44:05 2016 +0000
+++ b/Protocols/Protocols.h Mon Feb 06 12:29:33 2017 +0000
@@ -31,6 +31,7 @@
,BUS_16 /**< Parallel 16bit, scattered pins */
,SPI_8 /**< SPI 8bit */
,SPI_16 /**< SPI 16bit */
+ ,I2C_ /**< I2C */
};
#else
enum proto_t {
@@ -38,6 +39,7 @@
,BUS_16 /**< Parallel 16bit, scattered pins */
,SPI_8 /**< SPI 8bit */
,SPI_16 /**< SPI 16bit */
+ ,I2C_ /**< I2C */
};
#endif
GraphicsDisplay