LCD module Switch Science SKU#1405, which driver IC is ST7032i.

Files at this revision

API Documentation at this revision

Comitter:
coisme
Date:
Sat Jul 01 16:01:37 2017 +0000
Commit message:
First commit.

Changed in this revision

st7032i.cpp Show annotated file Show diff for this revision Revisions of this file
st7032i.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 92bfc61fb13b st7032i.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/st7032i.cpp	Sat Jul 01 16:01:37 2017 +0000
@@ -0,0 +1,148 @@
+#include "st7032i.h"
+
+#define SLAVE_ADDR  0x3e
+
+#define MAX_VAL_CONTRAST  63
+
+#define CONTINUE     0x80
+#define INSTRUCTION  0x00
+#define NOT_CONTINUE 0x00
+#define DATA         0x40
+
+#define MAX_VAL_COLUMN    8
+#define MAX_VAL_ROW       2
+
+St7032i::St7032i(I2C *obj){
+    i2c = obj;
+}
+
+St7032i::Status St7032i::init(){
+    // Initialization command sequence. Check the data sheet.
+    const char initSequence[] = {
+            0x38,    // Function set
+            0x39,    // Function set
+            0x14,    // Internal OSC frequency
+            0x70,    // Contrast set
+            0x56,    // Power/ICON/Contrast control
+            0x6c,    // Follower control
+            0x38,    // Function set
+            0x0c,    // Display ON/OFF control
+            0x01     // Clear Display
+        };
+        
+    // wait time after a command sent. Check the data sheet.
+    const int t_wait_us[] = { 
+            27, 27, 27, 27, 27, 200000, 27, 27, 1080  // micro-seconds
+        };
+
+    /* Somehow this workaround was needed on my test environment (BLENano) to 
+     * make the module work correctly. Maybe the following issue is related to
+     * this problem.
+     * https://strawberry-linux.com/pub/i2c_lcd-an004.pdf
+     */
+    {
+        i2c->start();    // start condition
+        wait_us(100);
+        i2c->stop();     // stop condition
+        wait_us(100);
+    }
+
+    // number of commands
+    int n_cmd = sizeof(initSequence)/sizeof(char);    
+
+    // Execute the initialization sequence.
+    for(int i=0; i < n_cmd; i++){
+        char data[2] = {(INSTRUCTION | NOT_CONTINUE), initSequence[i]};
+        if(i2c->write((SLAVE_ADDR << 1), data, 2) != 0){
+            return ERROR_I2C_NO_ACK;
+        }
+        wait_us(t_wait_us[i]);
+    }
+    
+    return SUCCESS;    
+}
+
+St7032i::Status St7032i::setContrast(uint8_t val){
+    const int t_wait_us = 27;   // wait time in micro-seconds
+    
+    // sanity check
+    if(MAX_VAL_CONTRAST < val){
+        return INPUT_OUT_OF_RANGE;
+    }
+    
+    char high = (0x54 | ((val & 0x30) >> 4));
+    char low = (0x70 | (val & 0x0F));
+
+    char sequence[] = {0x39, high, low, 0x38};    
+    
+    // Execute the command sequence
+    int n = sizeof(sequence)/sizeof(char);
+    for(int i=0; i < n; i++){
+        char data[2] = {(INSTRUCTION | NOT_CONTINUE), sequence[i]};
+        if(i2c->write((SLAVE_ADDR << 1), data, 2) != 0){
+            return ERROR_I2C_NO_ACK;
+        }
+        wait_us(t_wait_us);
+    }
+    
+    return SUCCESS;
+}
+
+St7032i::Status St7032i::setCursorPosition(uint8_t col, uint8_t row){
+    // sanity check
+    if( col > MAX_VAL_COLUMN || row > MAX_VAL_ROW){
+        return INPUT_OUT_OF_RANGE;
+    }
+    
+    char ddramAddr = (0x80 | (((row & 0x01) << 6) | (col & 0x0f)));
+    
+    char data[2] = {0x00, ddramAddr};
+    if(i2c->write((SLAVE_ADDR << 1), data, 2) != 0){
+        return ERROR_I2C_NO_ACK;
+    }
+    
+    return SUCCESS;
+}
+
+St7032i::Status St7032i::putc(char c){
+    char data[2] = {(NOT_CONTINUE | DATA), c};
+    if(i2c->write((SLAVE_ADDR << 1), data, 2) != 0){
+        return ERROR_I2C_NO_ACK;
+    }
+    return SUCCESS;
+}
+
+
+St7032i::Status St7032i::clear(bool wait){
+    const int TIME_WAIT = 1080; // in micro-seconds
+    char data[2] = {(NOT_CONTINUE | INSTRUCTION), 0x01};
+    if(i2c->write((SLAVE_ADDR << 1), data, 2) != 0){
+        return ERROR_I2C_NO_ACK;
+    }
+    if(wait){
+        wait_us(TIME_WAIT);
+    }
+    return SUCCESS;
+}
+
+St7032i::Status St7032i::puts(const char *str, bool wrap){
+    Status st;
+    if((st=setCursorPosition(0,0)) != SUCCESS){
+        return st;
+    }
+    for(int i=0; i < MAX_VAL_ROW; i++){
+        for(int j=0; j < MAX_VAL_COLUMN; j++){
+            char c = str[i*MAX_VAL_COLUMN+j];
+            if(c == '\0'){
+                return SUCCESS;
+            }
+            putc(c);
+        }
+        if(wrap){
+            if((st=setCursorPosition(0,1)) != SUCCESS){
+                return st;
+            }
+        }
+    }          
+    return SUCCESS;
+}
diff -r 000000000000 -r 92bfc61fb13b st7032i.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/st7032i.h	Sat Jul 01 16:01:37 2017 +0000
@@ -0,0 +1,127 @@
+#ifndef __ST7072I_H__
+#define __ST7072I_H__
+
+#include "mbed.h"
+
+/**
+ * Device driver for LCD SSCI-014052 (Switch Science SKU#1405),
+ * which display controller is ST7032i. 
+ * https://www.switch-science.com/catalog/1405/
+ *
+ * Tested with BLENano.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "st7032i.h"
+ *
+ * int main(void){
+ *     // Creates an I2C object
+ *     I2C i2c(I2C_SDA0, I2C_SCL0);
+ *
+ *     // Creates an display object
+ *     St7032i st7032i(&i2c);
+ *
+ *     // Waits for settling the system.
+ *     wait_ms(1000);
+ *     
+ *     // Initialize the display module.
+ *     st7032i.init();
+ *   
+ *     while(1){
+ *         st7032i.puts("Hello, World!!");
+ *         // Waits 1 second.
+ *         wait(1.0);
+ *         // Clears the display
+ *         st7032i.clear();
+ *         // Waits 0.5 second.
+ *         wait(0.5);
+ *     }
+ * }
+ * @endcode
+ */
+class St7032i
+{
+public:
+    /**
+     * Return value from functions.
+     */
+    typedef enum {
+        SUCCESS,            /**< Success */
+        ERROR_I2C_NO_ACK,   /**< I2C error with no ACK. */
+        INPUT_OUT_OF_RANGE, /**< Given input value is out of range. */
+    } Status;
+
+    /**
+     * Constructor.
+     * @param obj Pointer to an I2C object.
+     * @return no return value.
+     */
+    St7032i(I2C *obj);
+
+
+    /**
+     * Initialize the LCD.
+     * @returns
+     *     SUCCESS on the device was successfully initialized. 
+     *     others on error.
+     */     
+    Status init();
+    
+    /**
+     * Sets display's contrast.
+     * @param val Contrast (0-63)
+     * @returns
+     *     SUCCESS on the device was successfully set constrast.
+     *     others on error.
+     */
+    Status setContrast(uint8_t val);
+    
+    /**
+     * Puts a character at the current cursor position.
+     * @param c A character to be shown.
+     * @returns
+     *     SUCCESS on the device was successfully set cursor position.
+     *     others on error.
+     */
+    Status putc(char c);    
+    
+    /**
+     * Sets the cursor position.
+     * @param col column (0-7)
+     * @param row row (0 or 1)
+     * @returns
+     *     SUCCESS on the device was successfully set cursor position.
+     *     others on error.
+     */
+    Status setCursorPosition(uint8_t col, uint8_t row);
+    
+    /**
+     * Clears display. The instruction execution takes 1.08 ms.
+     * @param wait Waits instruction execution in this function if true.
+     * @returns
+     *     SUCCESS on the device was successfully cleared.
+     *     others on error.
+     */
+    Status clear(bool wait = true);
+    
+    /**
+     * Puts a string from the position (0,0). The characters exceeds the display
+     * range will be ignored.
+     * @param str Pointer to a string to be shown.
+     * @param wrap Automatically move to the second line if true.
+     * @returns
+     *     SUCCESS on the device put the gievn string successfully.
+     *     others on error.
+     */
+    Status puts(const char *str, bool wrap = true);
+    
+private:
+    /**
+     * Holds a pointer to an I2C object.
+     */
+    I2C *i2c;
+    
+};
+
+#endif   // __ST7072I_H__