-#include "RrdGlcd.h"
-
-RrdGlcd::RrdGlcd(PinName mosi, PinName sclk, Pin cs) {
- this->spi= new mbed::SPI(mosi, NC, sclk);
- //chip select
- this->cs= cs;
- this->cs.set(0);
-
- initDisplay();
-}
-
-RrdGlcd::~RrdGlcd() {
- delete this->spi;
-}
-
-void RrdGlcd::setFrequency(int freq) {
- this->spi->frequency(freq);
-}
-
-void RrdGlcd::writeInstruction(unsigned int command) {
- unsigned int hn, ln;
- hn = command & 0xF0;
- ln = (command & 0x0F) << 4;
-
- this->cs.set(1); // chip select
- //delay_ns(60); // setup time
-
- this->spi->write(0xF8); // sync, rw=0, rs=0
- this->spi->write(hn); // hi nibble
- this->spi->write(ln); // lo nibble
-
- this->cs.set(0);
-
- wait_us(80);
-}
-
-void RrdGlcd::writeRAM(unsigned int data) {
- unsigned int hn, ln;
- hn = data & 0xF0;
- ln = (data & 0x0F) << 4;
-
- this->cs.set(1); // chip select
- //delay_ns(60); // setup time
-
- this->spi->write(0xFA); // sync, rw=0, rs=1
- this->spi->write(hn); // hi nibble
- this->spi->write(ln); // lo nibble
-
- this->cs.set(0);
-
- wait_us(80);
-}
-
-void RrdGlcd::initDisplay() {
- wait_ms(40); // wait 40ms
- writeInstruction(FUNCTION_SET | DATA_LENGTH_DL); // 8 bits interface, RE=0
- writeInstruction(DISPLAY_CONTROL | DISPLAY_ON_D ); // display on
- writeInstruction(DISPLAY_CLEAR); // clear display
- wait_ms(2); // wait 2ms for clear
- writeInstruction(ENTRY_MODE_SET | INCREASE_DECREASE_ID); // move cursor right
- writeInstruction(RETURN_HOME);
- setGraphicsMode();
-}
-
-//************************************************************************************************
-//public methodes
-void RrdGlcd::setGraphicsMode() {
- writeInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL);
- writeInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE); //RE=1 (Extended funtion set)
- writeInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE | GRAPHIC_ON_G);
-}
-
-void RrdGlcd::setTextMode() {
- writeInstruction(FUNCTION_SET | DATA_LENGTH_DL); // RE=0 (Basic funtion set)
-}
-
-void RrdGlcd::clearScreen() {
- writeInstruction(FUNCTION_SET | DATA_LENGTH_DL); // RE=0 (Basic funtion set)
- writeInstruction(DISPLAY_CLEAR);
-}
-
-void RrdGlcd::returnHome() {
- writeInstruction(FUNCTION_SET | DATA_LENGTH_DL); //RE=0 (Basic funtion set)
- writeInstruction(RETURN_HOME);
-}
-
-void RrdGlcd::standby() {
- writeInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE); //RE=1 (Extended funtion set)
- writeInstruction(STANDBY);
-}
-
-//Basic text functions
-void RrdGlcd::displayString(int Row,int Column,const char *ptr,int length) {
- int i=0;
-
- switch (Row) {
- case 0:
- Column|=0x80;
- break;
- case 1:
- Column|=0x90;
- break;
- case 2:
- Column|=0x88;
- break;
- case 3:
- Column|=0x98;
- break;
- default:
- Column=0x80;
- break;
- }
-
- if (Column%2!=0) {
- Column-=1;
- i=1;
- }
- writeInstruction((unsigned int)Column);
-
- if (i==1) {
- writeRAM(' ');
- }
- for (i=0; i<length; i++) {
- writeRAM((unsigned int)ptr[i]);
- }
-}
-
-void RrdGlcd::displayChar(int Row,int Column,int inpChr) {
- int i=0;
-
- switch (Row) {
- case 0:
- Column|=0x80; // SET_DDRAM_ADDRESS
- break;
- case 1:
- Column|=0x90;
- break;
- case 2:
- Column|=0x88;
- break;
- case 3:
- Column|=0x98;
- break;
- default:
- Column=0x80;
- break;
- }
-
- if (Column%2!=0) {
- Column-=1;
- i=1;
- }
- writeInstruction((unsigned int)Column);
-
- if (i==1) {
- writeRAM(' ');
- }
- writeRAM((unsigned int)inpChr);
-}
-
-// Graphic functions
-void RrdGlcd::fillGDRAM(unsigned char *bitmap) {
- unsigned char i, j, k ;
-
- for ( i = 0 ; i < 2 ; i++ ) {
- for ( j = 0 ; j < 32 ; j++ ) {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ;
- if ( i == 0 ) {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS) ;
- } else {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ;
- }
- for ( k = 0 ; k < 16 ; k++ ) {
- writeRAM( *bitmap++ ) ;
- }
- }
- }
-}
-
-void RrdGlcd::fillGDRAM_Turned(unsigned char *bitmap) {
- int i, j, k, m, offset_row, mask ;
- unsigned char data;
-
- for ( i = 0 ; i < 2 ; i++ ) { //upper and lower page
- for ( j = 0 ; j < 32 ; j++ ) { //32 lines per page
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ;
- if ( i == 0 ) {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS) ;
- } else {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ;
- }
- mask=1<<(j%8); // extract bitnumber
- //printf("mask: %d\r\n",mask);
- for ( k = 0 ; k < 16 ; k++ ) { //16 bytes per line
- offset_row=((i*32+j)/8)*128 + k*8; //y coordinate/8 = row 0-7 * 128 = byte offset, read 8 bytes
- data=0;
- for (m = 0 ; m < 8 ; m++) { // read 8 bytes from source
-
- if ((bitmap[offset_row+m] & mask)) { //pixel = 1
- data|=(128>>m);
- }
- }
- writeRAM(data) ;
- }
- }
- }
-}
-
-void RrdGlcd::clearGDRAM() {
- unsigned char i, j, k ;
-
- for ( i = 0 ; i < 2 ; i++ ) {
- for ( j = 0 ; j < 32 ; j++ ) {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ;
- if ( i == 0 ) {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS) ;
- } else {
- writeInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ;
- }
- for ( k = 0 ; k < 16 ; k++ ) {
- writeRAM(0);
- }
- }
- }
-}
+#include "RrdGlcd.h"
+
+static const uint8_t font5x7[] = {
+ // each of the 5 bytes is an X bit the first byte being X==0, the last being X==5
+ // bit0 is y==0, bit7 is y==7
+ 0x00,0x00,0x00,0x00,0x00, // ascii 0
+ 0x3e,0x5b,0x4f,0x5b,0x3e, // ascii 1
+ 0x3e,0x6b,0x4f,0x6b,0x3e, // ascii 2 .. etc
+ 0x1c,0x3e,0x7c,0x3e,0x1c,
+ 0x18,0x3c,0x7e,0x3c,0x18,
+ 0x1c,0x57,0x7d,0x57,0x1c,
+ 0x1c,0x5e,0x7f,0x5e,0x1c,
+ 0x00,0x18,0x3c,0x18,0x00,
+ 0xff,0xe7,0xc3,0xe7,0xff,
+ 0x00,0x18,0x24,0x18,0x00,
+ 0xff,0xe7,0xdb,0xe7,0xff,
+ 0x30,0x48,0x3a,0x06,0x0e,
+ 0x26,0x29,0x79,0x29,0x26,
+ 0x40,0x7f,0x05,0x05,0x07,
+ 0x40,0x7f,0x05,0x25,0x3f,
+ 0x5a,0x3c,0xe7,0x3c,0x5a,
+ 0x7f,0x3e,0x1c,0x1c,0x08,
+ 0x08,0x1c,0x1c,0x3e,0x7f,
+ 0x14,0x22,0x7f,0x22,0x14,
+ 0x5f,0x5f,0x00,0x5f,0x5f,
+ 0x06,0x09,0x7f,0x01,0x7f,
+ 0x00,0x66,0x89,0x95,0x6a,
+ 0x60,0x60,0x60,0x60,0x60,
+ 0x94,0xa2,0xff,0xa2,0x94,
+ 0x08,0x04,0x7e,0x04,0x08,
+ 0x10,0x20,0x7e,0x20,0x10,
+ 0x08,0x08,0x2a,0x1c,0x08,
+ 0x08,0x1c,0x2a,0x08,0x08,
+ 0x1e,0x10,0x10,0x10,0x10,
+ 0x0c,0x1e,0x0c,0x1e,0x0c,
+ 0x30,0x38,0x3e,0x38,0x30,
+ 0x06,0x0e,0x3e,0x0e,0x06,
+ 0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x5f,0x00,0x00,
+ 0x00,0x07,0x00,0x07,0x00,
+ 0x14,0x7f,0x14,0x7f,0x14,
+ 0x24,0x2a,0x7f,0x2a,0x12,
+ 0x23,0x13,0x08,0x64,0x62,
+ 0x36,0x49,0x56,0x20,0x50,
+ 0x00,0x08,0x07,0x03,0x00,
+ 0x00,0x1c,0x22,0x41,0x00,
+ 0x00,0x41,0x22,0x1c,0x00,
+ 0x2a,0x1c,0x7f,0x1c,0x2a,
+ 0x08,0x08,0x3e,0x08,0x08,
+ 0x00,0x80,0x70,0x30,0x00,
+ 0x08,0x08,0x08,0x08,0x08,
+ 0x00,0x00,0x60,0x60,0x00,
+ 0x20,0x10,0x08,0x04,0x02,
+ 0x3e,0x51,0x49,0x45,0x3e,
+ 0x00,0x42,0x7f,0x40,0x00,
+ 0x72,0x49,0x49,0x49,0x46,
+ 0x21,0x41,0x49,0x4d,0x33,
+ 0x18,0x14,0x12,0x7f,0x10,
+ 0x27,0x45,0x45,0x45,0x39,
+ 0x3c,0x4a,0x49,0x49,0x31,
+ 0x41,0x21,0x11,0x09,0x07,
+ 0x36,0x49,0x49,0x49,0x36,
+ 0x46,0x49,0x49,0x29,0x1e,
+ 0x00,0x00,0x14,0x00,0x00,
+ 0x00,0x40,0x34,0x00,0x00,
+ 0x00,0x08,0x14,0x22,0x41,
+ 0x14,0x14,0x14,0x14,0x14,
+ 0x00,0x41,0x22,0x14,0x08,
+ 0x02,0x01,0x59,0x09,0x06,
+ 0x3e,0x41,0x5d,0x59,0x4e,
+ 0x7c,0x12,0x11,0x12,0x7c,
+ 0x7f,0x49,0x49,0x49,0x36,
+ 0x3e,0x41,0x41,0x41,0x22,
+ 0x7f,0x41,0x41,0x41,0x3e,
+ 0x7f,0x49,0x49,0x49,0x41,
+ 0x7f,0x09,0x09,0x09,0x01,
+ 0x3e,0x41,0x41,0x51,0x73,
+ 0x7f,0x08,0x08,0x08,0x7f,
+ 0x00,0x41,0x7f,0x41,0x00,
+ 0x20,0x40,0x41,0x3f,0x01,
+ 0x7f,0x08,0x14,0x22,0x41,
+ 0x7f,0x40,0x40,0x40,0x40,
+ 0x7f,0x02,0x1c,0x02,0x7f,
+ 0x7f,0x04,0x08,0x10,0x7f,
+ 0x3e,0x41,0x41,0x41,0x3e,
+ 0x7f,0x09,0x09,0x09,0x06,
+ 0x3e,0x41,0x51,0x21,0x5e,
+ 0x7f,0x09,0x19,0x29,0x46,
+ 0x26,0x49,0x49,0x49,0x32,
+ 0x03,0x01,0x7f,0x01,0x03,
+ 0x3f,0x40,0x40,0x40,0x3f,
+ 0x1f,0x20,0x40,0x20,0x1f,
+ 0x3f,0x40,0x38,0x40,0x3f,
+ 0x63,0x14,0x08,0x14,0x63,
+ 0x03,0x04,0x78,0x04,0x03,
+ 0x61,0x59,0x49,0x4d,0x43,
+ 0x00,0x7f,0x41,0x41,0x41,
+ 0x02,0x04,0x08,0x10,0x20,
+ 0x00,0x41,0x41,0x41,0x7f,
+ 0x04,0x02,0x01,0x02,0x04,
+ 0x40,0x40,0x40,0x40,0x40,
+ 0x00,0x03,0x07,0x08,0x00,
+ 0x20,0x54,0x54,0x78,0x40,
+ 0x7f,0x28,0x44,0x44,0x38,
+ 0x38,0x44,0x44,0x44,0x28,
+ 0x38,0x44,0x44,0x28,0x7f,
+ 0x38,0x54,0x54,0x54,0x18,
+ 0x00,0x08,0x7e,0x09,0x02,
+ 0x18,0xa4,0xa4,0x9c,0x78,
+ 0x7f,0x08,0x04,0x04,0x78,
+ 0x00,0x44,0x7d,0x40,0x00,
+ 0x20,0x40,0x40,0x3d,0x00,
+ 0x7f,0x10,0x28,0x44,0x00,
+ 0x00,0x41,0x7f,0x40,0x00,
+ 0x7c,0x04,0x78,0x04,0x78,
+ 0x7c,0x08,0x04,0x04,0x78,
+ 0x38,0x44,0x44,0x44,0x38,
+ 0xfc,0x18,0x24,0x24,0x18,
+ 0x18,0x24,0x24,0x18,0xfc,
+ 0x7c,0x08,0x04,0x04,0x08,
+ 0x48,0x54,0x54,0x54,0x24,
+ 0x04,0x04,0x3f,0x44,0x24,
+ 0x3c,0x40,0x40,0x20,0x7c,
+ 0x1c,0x20,0x40,0x20,0x1c,
+ 0x3c,0x40,0x30,0x40,0x3c,
+ 0x44,0x28,0x10,0x28,0x44,
+ 0x4c,0x90,0x90,0x90,0x7c,
+ 0x44,0x64,0x54,0x4c,0x44,
+ 0x00,0x08,0x36,0x41,0x00,
+ 0x00,0x00,0x77,0x00,0x00,
+ 0x00,0x41,0x36,0x08,0x00,
+ 0x02,0x01,0x02,0x04,0x02,
+ 0x3c,0x26,0x23,0x26,0x3c,
+ 0x1e,0xa1,0xa1,0x61,0x12,
+ 0x3a,0x40,0x40,0x20,0x7a,
+ 0x38,0x54,0x54,0x55,0x59,
+ 0x21,0x55,0x55,0x79,0x41,
+ 0x21,0x54,0x54,0x78,0x41,
+ 0x21,0x55,0x54,0x78,0x40,
+ 0x20,0x54,0x55,0x79,0x40,
+ 0x0c,0x1e,0x52,0x72,0x12,
+ 0x39,0x55,0x55,0x55,0x59,
+ 0x39,0x54,0x54,0x54,0x59,
+ 0x39,0x55,0x54,0x54,0x58,
+ 0x00,0x00,0x45,0x7c,0x41,
+ 0x00,0x02,0x45,0x7d,0x42,
+ 0x00,0x01,0x45,0x7c,0x40,
+ 0xf0,0x29,0x24,0x29,0xf0,
+ 0xf0,0x28,0x25,0x28,0xf0,
+ 0x7c,0x54,0x55,0x45,0x00,
+ 0x20,0x54,0x54,0x7c,0x54,
+ 0x7c,0x0a,0x09,0x7f,0x49,
+ 0x32,0x49,0x49,0x49,0x32,
+ 0x32,0x48,0x48,0x48,0x32,
+ 0x32,0x4a,0x48,0x48,0x30,
+ 0x3a,0x41,0x41,0x21,0x7a,
+ 0x3a,0x42,0x40,0x20,0x78,
+ 0x00,0x9d,0xa0,0xa0,0x7d,
+ 0x39,0x44,0x44,0x44,0x39,
+ 0x3d,0x40,0x40,0x40,0x3d,
+ 0x3c,0x24,0xff,0x24,0x24,
+ 0x48,0x7e,0x49,0x43,0x66,
+ 0x2b,0x2f,0xfc,0x2f,0x2b,
+ 0xff,0x09,0x29,0xf6,0x20,
+ 0xc0,0x88,0x7e,0x09,0x03,
+ 0x20,0x54,0x54,0x79,0x41,
+ 0x00,0x00,0x44,0x7d,0x41,
+ 0x30,0x48,0x48,0x4a,0x32,
+ 0x38,0x40,0x40,0x22,0x7a,
+ 0x00,0x7a,0x0a,0x0a,0x72,
+ 0x7d,0x0d,0x19,0x31,0x7d,
+ 0x26,0x29,0x29,0x2f,0x28,
+ 0x26,0x29,0x29,0x29,0x26,
+ 0x30,0x48,0x4d,0x40,0x20,
+ 0x38,0x08,0x08,0x08,0x08,
+ 0x08,0x08,0x08,0x08,0x38,
+ 0x2f,0x10,0xc8,0xac,0xba,
+ 0x2f,0x10,0x28,0x34,0xfa,
+ 0x00,0x00,0x7b,0x00,0x00,
+ 0x08,0x14,0x2a,0x14,0x22,
+ 0x22,0x14,0x2a,0x14,0x08,
+ 0xaa,0x00,0x55,0x00,0xaa,
+ 0xaa,0x55,0xaa,0x55,0xaa,
+ 0x55,0xaa,0x55,0xaa,0x55,
+ 0x00,0x00,0x00,0xff,0x00,
+ 0x10,0x10,0x10,0xff,0x00,
+ 0x14,0x14,0x14,0xff,0x00,
+ 0x10,0x10,0xff,0x00,0xff,
+ 0x10,0x10,0xf0,0x10,0xf0,
+ 0x14,0x14,0x14,0xfc,0x00,
+ 0x14,0x14,0xf7,0x00,0xff,
+ 0x00,0x00,0xff,0x00,0xff,
+ 0x14,0x14,0xf4,0x04,0xfc,
+ 0x14,0x14,0x17,0x10,0x1f,
+ 0x10,0x10,0x1f,0x10,0x1f,
+ 0x14,0x14,0x14,0x1f,0x00,
+ 0x10,0x10,0x10,0xf0,0x00,
+ 0x00,0x00,0x00,0x1f,0x10,
+ 0x10,0x10,0x10,0x1f,0x10,
+ 0x10,0x10,0x10,0xf0,0x10,
+ 0x00,0x00,0x00,0xff,0x10,
+ 0x10,0x10,0x10,0x10,0x10,
+ 0x10,0x10,0x10,0xff,0x10,
+ 0x00,0x00,0x00,0xff,0x14,
+ 0x00,0x00,0xff,0x00,0xff,
+ 0x00,0x00,0x1f,0x10,0x17,
+ 0x00,0x00,0xfc,0x04,0xf4,
+ 0x14,0x14,0x17,0x10,0x17,
+ 0x14,0x14,0xf4,0x04,0xf4,
+ 0x00,0x00,0xff,0x00,0xf7,
+ 0x14,0x14,0x14,0x14,0x14,
+ 0x14,0x14,0xf7,0x00,0xf7,
+ 0x14,0x14,0x14,0x17,0x14,
+ 0x10,0x10,0x1f,0x10,0x1f,
+ 0x14,0x14,0x14,0xf4,0x14,
+ 0x10,0x10,0xf0,0x10,0xf0,
+ 0x00,0x00,0x1f,0x10,0x1f,
+ 0x00,0x00,0x00,0x1f,0x14,
+ 0x00,0x00,0x00,0xfc,0x14,
+ 0x00,0x00,0xf0,0x10,0xf0,
+ 0x10,0x10,0xff,0x10,0xff,
+ 0x14,0x14,0x14,0xff,0x14,
+ 0x10,0x10,0x10,0x1f,0x00,
+ 0x00,0x00,0x00,0xf0,0x10,
+ 0xff,0xff,0xff,0xff,0xff,
+ 0xf0,0xf0,0xf0,0xf0,0xf0,
+ 0xff,0xff,0xff,0x00,0x00,
+ 0x00,0x00,0x00,0xff,0xff,
+ 0x0f,0x0f,0x0f,0x0f,0x0f,
+ 0x38,0x44,0x44,0x38,0x44,
+ 0x7c,0x2a,0x2a,0x3e,0x14,
+ 0x7e,0x02,0x02,0x06,0x06,
+ 0x02,0x7e,0x02,0x7e,0x02,
+ 0x63,0x55,0x49,0x41,0x63,
+ 0x38,0x44,0x44,0x3c,0x04,
+ 0x40,0x7e,0x20,0x1e,0x20,
+ 0x06,0x02,0x7e,0x02,0x02,
+ 0x99,0xa5,0xe7,0xa5,0x99,
+ 0x1c,0x2a,0x49,0x2a,0x1c,
+ 0x4c,0x72,0x01,0x72,0x4c,
+ 0x30,0x4a,0x4d,0x4d,0x30,
+ 0x30,0x48,0x78,0x48,0x30,
+ 0xbc,0x62,0x5a,0x46,0x3d,
+ 0x3e,0x49,0x49,0x49,0x00,
+ 0x7e,0x01,0x01,0x01,0x7e,
+ 0x2a,0x2a,0x2a,0x2a,0x2a,
+ 0x44,0x44,0x5f,0x44,0x44,
+ 0x40,0x51,0x4a,0x44,0x40,
+ 0x40,0x44,0x4a,0x51,0x40,
+ 0x00,0x00,0xff,0x01,0x03,
+ 0xe0,0x80,0xff,0x00,0x00,
+ 0x08,0x08,0x6b,0x6b,0x08,
+ 0x36,0x12,0x36,0x24,0x36,
+ 0x06,0x0f,0x09,0x0f,0x06,
+ 0x00,0x01,0x02,0x01,0x40,
+ 0x00,0x00,0x10,0x10,0x00,
+ 0x30,0x40,0xff,0x01,0x01,
+ 0x00,0x1f,0x01,0x01,0x1e,
+ 0x00,0x19,0x1d,0x17,0x12,
+ 0x00,0x3c,0x3c,0x3c,0x3c
+};
+
+#define ST7920_CS() {cs.set(1);wait_us(10);}
+#define ST7920_NCS() {cs.set(0);wait_us(10);}
+#define ST7920_WRITE_BYTE(a) {this->spi->write((a)&0xf0);this->spi->write((a)<<4);wait_us(10);}
+#define ST7920_WRITE_BYTES(p,l) {uint8_t i;for(i=0;i<l;i++){this->spi->write(*p&0xf0);this->spi->write(*p<<4);p++;} wait_us(10); }
+#define ST7920_SET_CMD() {this->spi->write(0xf8);wait_us(10);}
+#define ST7920_SET_DAT() {this->spi->write(0xfa);wait_us(10);}
+#define PAGE_HEIGHT 32 //512 byte framebuffer
+#define WIDTH 128
+#define HEIGHT 64
+
+RrdGlcd::RrdGlcd(PinName mosi, PinName sclk, Pin cs) {
+ this->spi= new mbed::SPI(mosi, NC, sclk);
+ //chip select
+ this->cs= cs;
+ this->cs.set(0);
+ inited= false;
+}
+
+RrdGlcd::~RrdGlcd() {
+ delete this->spi;
+}
+
+void RrdGlcd::setFrequency(int freq) {
+ this->spi->frequency(freq);
+}
+
+void RrdGlcd::initDisplay() {
+ // wait_ms(40); // wait 40ms
+ // writeInstruction(FUNCTION_SET | DATA_LENGTH_DL); // 8 bits interface, RE=0
+ // writeInstruction(DISPLAY_CONTROL | DISPLAY_ON_D ); // display on
+ // writeInstruction(DISPLAY_CLEAR); // clear display
+ // wait_ms(2); // wait 2ms for clear
+ // writeInstruction(ENTRY_MODE_SET | INCREASE_DECREASE_ID); // move cursor right
+ // writeInstruction(RETURN_HOME);
+ // setGraphicsMode();
+ // wait_ms(100);
+
+ ST7920_CS();
+ wait_ms(90); //initial delay for boot up
+ ST7920_SET_CMD();
+ ST7920_WRITE_BYTE(0x08); //display off, cursor+blink off
+ ST7920_WRITE_BYTE(0x01); //clear CGRAM ram
+ wait_ms(10); //delay for cgram clear
+ ST7920_WRITE_BYTE(0x3E); //extended mode + gdram active
+ for(int y=0;y<HEIGHT/2;y++) //clear GDRAM
+ {
+ ST7920_WRITE_BYTE(0x80|y); //set y
+ ST7920_WRITE_BYTE(0x80); //set x = 0
+ ST7920_SET_DAT();
+ for(int i=0;i<2*WIDTH/8;i++) //2x width clears both segments
+ ST7920_WRITE_BYTE(0);
+ ST7920_SET_CMD();
+ }
+ ST7920_WRITE_BYTE(0x0C); //display on, cursor+blink off
+ ST7920_NCS();
+ wait_ms(100);
+ memset(fb, 0, sizeof(fb));
+ inited= true;
+}
+
+void RrdGlcd::clearScreen() {
+ memset(fb, 0, sizeof(fb));
+}
+
+// render into local screenbuffer
+void RrdGlcd::displayString(int row, int col, const char *ptr, int length) {
+ for (int i = 0; i < length; ++i) {
+ displayChar(row, col, ptr[i]);
+ col+=1;
+ }
+}
+
+static void renderChar(uint8_t *fb, char c, int ox, int oy) {
+ int i= c*5;
+ for(int y=0;y<8;y++) {
+ for(int x=0;x<5;x++) {
+ int b= font5x7[i+x];
+ if((b & (1<<(y))) != 0){
+ fb[(y+oy)*16 + (x+ox)/8] |= (1<<(8-(x+ox)%8-1));
+ }else{
+ fb[(y+oy)*16 + (x+ox)/8] &= ~(1<<(8-(x+ox)%8-1));
+ }
+ }
+ }
+}
+
+void RrdGlcd::displayChar(int row, int col, char c) {
+ renderChar(this->fb, c, col*6, row*8);
+}
+
+// static void draw(uint8_t *fb) {
+// int ox= 0;
+// int oy= 0;
+// for(int c=' ';c<='z';c++) {
+// int i= c*5;
+// for(int y=0;y<8;y++) {
+// for(int x=0;x<5;x++) {
+// int b= font5x7[i+x];
+// if((b & (1<<(y))) != 0){
+// fb[(y+oy)*16 + (x+ox)/8] |= (1<<(8-(x+ox)%8-1));
+// }
+// }
+// }
+// ox += 6;
+// if(ox >= 20*6){
+// ox= 0;
+// oy+=8;
+// }
+// }
+// }
+
+
+// static void draw(uint8_t *fb) {
+// for (int i = 0; i < 1024; ++i) {
+// *fb++ = 0x92;
+// }
+// }
+
+void RrdGlcd::refresh() {
+ if(!inited) return;
+ fillGDRAM(this->fb);
+}
+
+// Graphic functions
+void RrdGlcd::fillGDRAM(const uint8_t *bitmap) {
+ unsigned char i, y;
+ for ( i = 0 ; i < 2 ; i++ ) {
+ ST7920_CS();
+ for ( y = 0 ; y < PAGE_HEIGHT ; y++ ) {
+ ST7920_SET_CMD();
+ ST7920_WRITE_BYTE(0x80 | y);
+ if ( i == 0 ) {
+ ST7920_WRITE_BYTE(0x80);
+ } else {
+ ST7920_WRITE_BYTE(0x80 | 0x08);
+ }
+ ST7920_SET_DAT();
+ ST7920_WRITE_BYTES(bitmap, WIDTH/8);
+ }
+ ST7920_NCS();
+ }
+}
+
+// void RrdGlcd::fillGDRAM_Turned(unsigned char *bitmap) {
+// int i, j, k, m, offset_row, mask ;
+// unsigned char data;
+
+// for ( i = 0 ; i < 2 ; i++ ) { //upper and lower page
+// for ( j = 0 ; j < 32 ; j++ ) { //32 lines per page
+// writeInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ;
+// if ( i == 0 ) {
+// writeInstruction(SET_GRAPHIC_RAM_ADDRESS) ;
+// } else {
+// writeInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ;
+// }
+// mask=1<<(j%8); // extract bitnumber
+// //printf("mask: %d\r\n",mask);
+// for ( k = 0 ; k < 16 ; k++ ) { //16 bytes per line
+// offset_row=((i*32+j)/8)*128 + k*8; //y coordinate/8 = row 0-7 * 128 = byte offset, read 8 bytes
+// data=0;
+// for (m = 0 ; m < 8 ; m++) { // read 8 bytes from source
+
+// if ((bitmap[offset_row+m] & mask)) { //pixel = 1
+// data|=(128>>m);
+// }
+// }
+// writeRAM(data) ;
+// }
+// }
+// }
+// }