Basic library for SHARP LCD LS027B4DH01/LS027B7DH01

Dependents:   AkiSpiLcd_demo AkiSpiLcd_demo2 LCDRAM AkiSpiLcd_example

Files at this revision

API Documentation at this revision

Comitter:
k4zuki
Date:
Sun Sep 07 14:42:13 2014 +0000
Parent:
8:c99c1c99b34c
Child:
10:eed99ef09e63
Commit message:
.

Changed in this revision

AkiSpiLcd.cpp Show annotated file Show diff for this revision Revisions of this file
AkiSpiLcd.h Show annotated file Show diff for this revision Revisions of this file
--- a/AkiSpiLcd.cpp	Thu Aug 28 17:26:10 2014 +0000
+++ b/AkiSpiLcd.cpp	Sun Sep 07 14:42:13 2014 +0000
@@ -4,31 +4,32 @@
 
 #include "mbed.h"
 #include "AkiSpiLcd.h"
+#include "Ser23K256.h"
 
 AkiSpiLcd::AkiSpiLcd(PinName mosi, PinName miso, PinName sck, PinName csl, PinName csr)
     :_spi(mosi, miso, sck), _csl(csl), _csr(csr)
 {
-    _ram= new Ser23K256::Ser23K256(_spi,csr);
+//    Ser23K256 _ram(_spi,csr);
     _csl=0;
     _csr=1;
     _spi.format(8,0);
-    _spi.frequency(2000000);
+    _spi.frequency(1000000);
     comflag = modeflag = clearflag = 0;
 
     int data=0;
-    for(int i=0;i<240;i++){
+    for(int i=0; i<240; i++) {
         data=( ( i & 0x01 ) << 7 )|
-            ( ( i & 0x02 ) << 5 )|
-            ( ( i & 0x04 ) << 3 )|
-            ( ( i & 0x08 ) << 1 )|
-            ( ( i & 0x10 ) >> 1 )|
-            ( ( i & 0x20 ) >> 3 )|
-            ( ( i & 0x40 ) >> 5 )|
-            ( ( i & 0x80 ) >> 7 );
+             ( ( i & 0x02 ) << 5 )|
+             ( ( i & 0x04 ) << 3 )|
+             ( ( i & 0x08 ) << 1 )|
+             ( ( i & 0x10 ) >> 1 )|
+             ( ( i & 0x20 ) >> 3 )|
+             ( ( i & 0x40 ) >> 5 )|
+             ( ( i & 0x80 ) >> 7 );
 
-        _ram->write(RAMLINE_BASE+i,(uint8_t)data);
+        ram_write(RAMLINE_BASE+i,(uint8_t)data);
     }
-        _ram->write(RAMMODE_BASE,0x00000000);
+    ram_write(RAMMODE_BASE,0x00000000,4);
 
 }
 
@@ -155,19 +156,20 @@
 }
 */
 
-    /** Reads single line (400 bits = 50 bytes) from a screen
-    */
-void AkiSpiLcd::ramReadSingle(int line, uint8_t* buffer, int screen){
+/** Reads single line (400 bits = 50 bytes) from a screen
+*/
+void AkiSpiLcd::ramReadSingle(int line, uint8_t* buffer, int screen)
+{
     screen &=1;
-    if(screen==0){
+    if(screen==SCREEN0) {
         screen=SCREEN0_BASE;
-    }else{
+    } else {
         screen=SCREEN1_BASE;
     }
 //    uint8_t buffer[50];
     line*=50;
     int address=screen+line;
-    _ram->read(address,buffer,50);
+    ram_read(address,buffer,50);
     /*
     _csr=0; //select VRAM
     _spi.write(0x03);
@@ -181,19 +183,20 @@
 //    return buffer;
 }
 
-    /** Reads multi lines(400 x N bits = 50 x N bytes) from a screen
-    */
-void AkiSpiLcd::ramReadMulti(int line, int length, uint8_t* buffer, int screen){
+/** Reads multi lines(400 x N bits = 50 x N bytes) from a screen
+*/
+void AkiSpiLcd::ramReadMulti(int line, int length, uint8_t* buffer, int screen)
+{
     screen &=1;
-    if(screen==0){
+    if(screen==SCREEN0) {
         screen=SCREEN0_BASE;
-    }else{
+    } else {
         screen=SCREEN1_BASE;
     }
     line*=50;
     int address=screen+line;
 //    for(int j=0;j<length;j++){
-    _ram->read(address,buffer,50*length);
+    ram_read(address,buffer,50*length);
 //    }
     /*
     _csr=0; //select VRAM
@@ -209,43 +212,45 @@
     */
 //    return buffer;
 }
-    
-    /** Writes single line (400 bits = 50 bytes) into a screen
-    */
-void AkiSpiLcd::ramWriteSingle(int line, uint8_t* data, int screen){
+
+/** Writes single line (400 bits = 50 bytes) into a screen
+*/
+void AkiSpiLcd::ramWriteSingle(int line, uint8_t* data, int screen)
+{
     screen &=1;
-    if(screen==0){
+    if(screen==SCREEN0) {
         screen=SCREEN0_BASE;
-    }else{
+    } else {
         screen=SCREEN1_BASE;
     }
     line*=50;
     int address=screen+line;
-    _ram->write(address,data,50);
-    
+    ram_write(address,data,50);
+
     _csr=0; //select VRAM
     _spi.write(0x02);
     _spi.write(address>>8);
     _spi.write(address&0xff);
-    for(int i=0;i<50;i++){
+    for(int i=0; i<50; i++) {
         _spi.write(*(data+i));
     }
     _csr=1;
 }
 
-    /** Writes multi lines(400 x N bits = 50 x N bytes) into a screen
-    */
-void AkiSpiLcd::ramWriteMulti(int line, int length, uint8_t* data, int screen){
+/** Writes multi lines(400 x N bits = 50 x N bytes) into a screen
+*/
+void AkiSpiLcd::ramWriteMulti(int line, int length, uint8_t* data, int screen)
+{
     screen &=1;
-    if(screen==0){
+    if(screen==SCREEN0) {
         screen=SCREEN0_BASE;
-    }else{
+    } else {
         screen=SCREEN1_BASE;
     }
     line*=50;
-    
+
     int address=screen+line;
-    _ram->write(address,data,50*length);
+    ram_write(address,data,50*length);
     /*
     _csr=0; //select VRAM
     _spi.write(0x02);
@@ -260,13 +265,14 @@
     */
 }
 
-    /** copies whole data in screen into LCD
-    */
-void AkiSpiLcd::ram2lcd(int startline, int length, int screen){
+/** copies whole data in screen into LCD
+*/
+void AkiSpiLcd::ram2lcd(int startline, int length, int screen)
+{
     screen &=1;
-    if(screen==0){
+    if(screen==SCREEN0) {
         screen=SCREEN0_BASE;
-    }else{
+    } else {
         screen=SCREEN1_BASE;
     }
 //    screen<<=7;
@@ -279,13 +285,13 @@
     _spi.write(address>>8);
     _spi.write(address&0xff);
     */
-    for(int j=0;j<240;j++){
-        _ram->write(RAMMODE_BASE,(modeflag << 7) | (comflag << 6) | (clearflag << 5));
-        _csl=1;
-        _ram->read(RAMMODE_BASE);
-        _ram->read(RAMLINE_BASE+startline);
-        _ram->read(address+50*j,dummy,50);
-        _ram->read(RAMMODE_BASE+2,dummy,2);
+    ram_write(RAMMODE_BASE,(modeflag << 7) | (comflag << 6) | (clearflag << 5));
+    _csl=1;
+    for(int j=0; j<length; j++) {
+        ram_read(RAMMODE_BASE);
+        ram_read(RAMLINE_BASE+startline);
+        ram_read(address+50*j,dummy,50);
+        ram_read(RAMMODE_BASE+2,dummy,2);
         wait_us(5);
         _csl=0;
         if(comflag == 0) {
@@ -296,3 +302,75 @@
     }
     _csl=0;
 }
+
+uint8_t AkiSpiLcd::ram_read(int address)
+{
+    ram_prepareCommand(READ, address);
+    int result = _spi.write(0);
+    ram_deselect();
+    return (uint8_t) result;
+}
+
+void AkiSpiLcd::ram_read(int address, uint8_t * buffer, int count)
+{
+    ram_writeStatus(SEQUENTIAL_MODE);
+    ram_prepareCommand(READ, address);
+    for (int i = 0; i < count; i++) {
+        buffer[i] = _spi.write(0);
+    }
+    ram_deselect();
+    ram_writeStatus(BYTE_MODE);
+}
+
+void AkiSpiLcd::ram_write(int address, uint8_t byte)
+{
+    ram_prepareCommand(WRITE, address);
+    _spi.write(byte);
+    ram_deselect();
+}
+
+void AkiSpiLcd::ram_write(int address, uint8_t * buffer, int count)
+{
+    ram_writeStatus(SEQUENTIAL_MODE);
+    ram_prepareCommand(WRITE, address);
+    for (int i = 0; i < count; i++) {
+        _spi.write(buffer[i]);
+    }
+    ram_deselect();
+    ram_writeStatus(BYTE_MODE);
+}
+
+uint8_t AkiSpiLcd::ram_readStatus()
+{
+    ram_select();
+    _spi.write(READ_STATUS);
+    uint8_t result = (uint8_t) _spi.write(0);
+    ram_deselect();
+    return result;
+}
+
+void AkiSpiLcd::ram_writeStatus(uint8_t status)
+{
+    ram_select();
+    _spi.write(WRITE_STATUS);
+    _spi.write(status);
+    ram_deselect();
+}
+
+void AkiSpiLcd::ram_prepareCommand(uint8_t command, int address)
+{
+    ram_select();
+    _spi.write(command);
+    _spi.write(address >> 8);
+    _spi.write(address & 0xFF);
+}
+
+void AkiSpiLcd::ram_select()
+{
+    _csr=0;
+}
+
+void AkiSpiLcd::ram_deselect()
+{
+    _csr=1;
+}
--- a/AkiSpiLcd.h	Thu Aug 28 17:26:10 2014 +0000
+++ b/AkiSpiLcd.h	Sun Sep 07 14:42:13 2014 +0000
@@ -6,7 +6,7 @@
 #define __AKISPILCD_H
 
 #include "mbed.h"
-#include "Ser23K256.h"
+//#include "Ser23K256.h"
 /** AkiSpiLcd
  * mbed library for SHARP LCD LS027B4DH01
  *
@@ -38,6 +38,8 @@
 #define RAMMODE_BASE 0x6100
 #define SCREEN0_BASE 0x0000
 #define SCREEN1_BASE 0x3000
+#define SCREEN0 0
+#define SCREEN1 1
  
 class AkiSpiLcd
 {
@@ -97,13 +99,42 @@
 //    */
 //    void dispOn(bool disp);
 
+/** read a byte from SRAM
+* @param address    The address to read from
+* @return the uint8_tacter at that address
+*/
+    uint8_t ram_read(int address);
+/** read multiple bytes from SRAM into a buffer
+* @param address    The SRAM address to read from
+* @param buffer     The buffer to read into (must be big enough!)
+* @param count      The number of bytes to read
+*/
+    void ram_read(int address, uint8_t * buffer, int count);
+/** write a byte to SRAM
+* @param address    The address SRAM to write to
+* @param byte       The byte to write there
+*/
+    void ram_write(int address, uint8_t byte);
+    /** write multiple bytes to SRAM from a buffer
+* @param address    The SRAM address write to
+* @param buffer     The buffer to write from
+* @param count      The number of bytes to write
+*/
+    void ram_write(int address, uint8_t * buffer, int count);
+
 private:
-    Ser23K256::Ser23K256 *_ram;
+//    Ser23K256 _ram;
     int comflag;
     int modeflag;
     int clearflag;
     SPI _spi;
     DigitalOut _csl;
     DigitalOut _csr;
+
+    uint8_t ram_readStatus();
+    void ram_writeStatus(uint8_t status);
+    void ram_prepareCommand(uint8_t command, int address);
+    void ram_select();
+    void ram_deselect();
 };
 #endif