Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.
Dependents: N3310LCD_Demo FRDM_N3110LCD
Revision 1:51961974fe55, committed 2013-03-10
- Comitter:
- SomeRandomBloke
- Date:
- Sun Mar 10 18:29:09 2013 +0000
- Parent:
- 0:7efa6655d94b
- Child:
- 2:e6c002c680a6
- Commit message:
- reformatted code
Changed in this revision
--- a/Joystick.cpp Sun Mar 10 18:15:25 2013 +0000 +++ b/Joystick.cpp Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -34,18 +34,17 @@ // values correspond to use of a 3.3V supply for the LCD shield. const int Joystick::adcKeyVal[NUM_KEYS] = {50, // LEFT - 200, // CENTER DEPRESSED - 400, // DOWN - 600, // UP - 800 // RIGHT - // 1024 CENTER NOT DEPRESSED - }; - + 200, // CENTER DEPRESSED + 400, // DOWN + 600, // UP + 800 // RIGHT + // 1024 CENTER NOT DEPRESSED + }; + Joystick::Joystick(PinName jstick) : joystick(jstick) { // reset button arrays - for (int i = 0; i < NUM_KEYS; i++) - { + for (int i = 0; i < NUM_KEYS; i++) { buttonCount[i] = 0; buttonStatus[i] = 0; buttonFlag[i] = 0; @@ -55,76 +54,64 @@ int Joystick::getKeyState(int i) { int retval = 0; - - if (i < NUM_KEYS) - { + + if (i < NUM_KEYS) { retval = buttonFlag[i]; } - + return retval; } void Joystick::resetKeyState(int i) { - if (i < NUM_KEYS) - { + if (i < NUM_KEYS) { buttonFlag[i] = 0; } } void Joystick::updateADCKey() { - // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important + // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important // that the LCD shield is powered from a 3.3V supply in order for the 'right' joystick // key to function correctly. - + int adcKeyIn = joystick * 1024; // scale this up so we can use int int keyIn = getKey(adcKeyIn); pc.printf("%d \n",adcKeyIn ); - - for (int i = 0; i < NUM_KEYS; i++) - { - if (keyIn == i) //one key is pressed - { - if (buttonCount[i] < DEBOUNCE_MAX) - { + + for (int i = 0; i < NUM_KEYS; i++) { + if (keyIn == i) { //one key is pressed + if (buttonCount[i] < DEBOUNCE_MAX) { buttonCount[i]++; - if (buttonCount[i] > DEBOUNCE_ON) - { - if (buttonStatus[i] == 0) - { + if (buttonCount[i] > DEBOUNCE_ON) { + if (buttonStatus[i] == 0) { buttonFlag[i] = 1; buttonStatus[i] = 1; //button debounced to 'pressed' status } } } - } - else // no button pressed - { - if (buttonCount[i] > 0) - { - buttonFlag[i] = 0; + } else { // no button pressed + if (buttonCount[i] > 0) { + buttonFlag[i] = 0; buttonCount[i]--; - if (buttonCount[i] < DEBOUNCE_OFF) - { + if (buttonCount[i] < DEBOUNCE_OFF) { buttonStatus[i] = 0; //button debounced to 'released' status } } } } } - + // Convert ADC value to key number int Joystick::getKey(int input) { int k; - - for (k = 0; k < NUM_KEYS; k++) - { + + for (k = 0; k < NUM_KEYS; k++) { if (input < adcKeyVal[k]) return k; } - + if (k >= NUM_KEYS) k = -1; // No valid key pressed - + return k; }
--- a/Joystick.h Sun Mar 10 18:15:25 2013 +0000 +++ b/Joystick.h Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -34,22 +34,22 @@ { public: Joystick(PinName jstick); - + int getKeyState(int i); void resetKeyState(int i); void updateADCKey(); // call this to initiate joystick read - + private: // data int buttonCount[NUM_KEYS]; // debounce counters int buttonStatus[NUM_KEYS]; // button status - pressed/released int buttonFlag[NUM_KEYS]; // button on flags for user program - + static const int adcKeyVal[NUM_KEYS]; - + // I/O AnalogIn joystick; - + // functions int getKey(int input); };
--- a/N3310Fonts.h Sun Mar 10 18:15:25 2013 +0000 +++ b/N3310Fonts.h Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -30,8 +30,7 @@ // 1 pixel space at left and bottom // index = ASCII - 32 -unsigned char font6_8[][6] = -{ +unsigned char font6_8[][6] = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // ! { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // " @@ -127,11 +126,10 @@ }; -//******* VERY LARGE FONTS ********** +//******* VERY LARGE FONTS ********** //used here for displaying numbers 0 - 9 and '+', '-', '.' -unsigned char big_number[13][3][16] = -{ +unsigned char big_number[13][3][16] = { 0,128,192,224,224,96,224,224, //'0' 192,128,0,0,0,0,0,0 ,
--- a/N3310LCD.cpp Sun Mar 10 18:15:25 2013 +0000 +++ b/N3310LCD.cpp Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -28,10 +28,10 @@ static unsigned char lcd_buffer[LCDROWMAX][LCDCOLMAX]; -N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck, - PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) : - lcdPort(mosi, miso, sck), - ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on) +N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck, + PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) : + lcdPort(mosi, miso, sck), + ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on) { } @@ -40,29 +40,28 @@ // use default SPI format lcdPort.format(8,0); lcdPort.frequency(1000000); - + // lcd reset wait_ms(1); rstWire = 0; wait_ms(1); rstWire = 1; - - write(0x21, CMD); - write(0xc8, CMD); - write(0x06, CMD); - write(0x13, CMD); - write(0x20, CMD); - cls(); + + write(0x21, CMD); + write(0xc8, CMD); + write(0x06, CMD); + write(0x13, CMD); + write(0x20, CMD); + cls(); write(0x0c, CMD); } void N3310LCD::cls() { - write(0x0c, CMD); - write(0x80, CMD); + write(0x0c, CMD); + write(0x80, CMD); - for (int i = 0; i < 504; i++) - { + for (int i = 0; i < 504; i++) { write(0, DATA); } } @@ -77,14 +76,14 @@ { // bring CS low for write ceWire = 0; - + if (CMD == req_type) dcWire = 0; else // DATA dcWire = 1; - + lcdPort.write(data); - + // write finished ceWire = 1; } @@ -92,75 +91,71 @@ void N3310LCD::locate(BYTE xPos, BYTE yPos) { write(0x40 | yPos, CMD); // column - write(0x80 | xPos, CMD); // row + write(0x80 | xPos, CMD); // row } void N3310LCD::drawBitmap(BYTE xPos, BYTE yPos, BYTE* bitmap, BYTE bmpXSize, BYTE bmpYSize) { BYTE row; - + if (0 == bmpYSize % 8) - row = bmpYSize/8; + row = bmpYSize/8; else row = bmpYSize/8 + 1; - - for (BYTE n = 0; n < row; n++) - { + + for (BYTE n = 0; n < row; n++) { locate(xPos, yPos); - for(BYTE i = 0; i < bmpXSize; i++) - { + for(BYTE i = 0; i < bmpXSize; i++) { write(bitmap[i + (n * bmpXSize)], DATA); } - yPos++; + yPos++; } } /* - * Name : clearBitmap + * Name : clearBitmap * Description : Clear an area of the screen, usually to blank out a * previously drawn image or part of image. * Argument(s) : x, y - Position on screen, x 0-83, y 1-6 * size_x,size_y - Size of the image in pixels, * size_y is multiple of 8 - * Return value : none + * Return value : none */ void N3310LCD::clearBitmap( unsigned char x,unsigned char y, - unsigned char size_x,unsigned char size_y) - { + unsigned char size_x,unsigned char size_y) +{ unsigned int i,n; unsigned char row; - + row = (size_y % 8 == 0 ) ? size_y / 8 : size_y / 8 + 1; // if (size_y % 8==0) -// row=size_y/8; +// row=size_y/8; // else // row=size_y/8+1; - - for (n=0;n<row;n++) { + + for (n=0; n<row; n++) { locate(x,y); for(i=0; i<size_x; i++) { write( 0x00, DATA ); } - y++; - } + y++; + } } void N3310LCD::writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode) { locate(xPos, yPos); - - while (*string) - { + + while (*string) { writeChar(*string++, mode); } } - + void N3310LCD::writeStringBig(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode) { - while (*string) - { + while (*string) { writeCharBig(xPos, yPos, *string , mode); - + if('.' == *string++) xPos += 5; else @@ -171,12 +166,11 @@ void N3310LCD::writeChar(BYTE ch, eDisplayMode mode) { BYTE sendByte; - + unsigned char* pFont = (unsigned char*)font6_8; ch -= 32; - for (BYTE line = 0; line < 6; line++) - { + for (BYTE line = 0; line < 6; line++) { sendByte = *(pFont + ch*6 + line); write((mode == NORMAL)? sendByte: (sendByte ^ 0xff) , DATA); } @@ -185,9 +179,9 @@ void N3310LCD::writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode) { BYTE sendByte; - + unsigned char* pFont = (unsigned char *) big_number; - + if('.' == ch) ch = 10; else if ('+' == ch) @@ -196,13 +190,11 @@ ch = 12; else ch = ch & 0x0f; - - for(BYTE i = 0; i < 3; i++) - { + + for(BYTE i = 0; i < 3; i++) { locate(xPos, yPos + i); - - for(BYTE j = 0; j < 16; j++) - { + + for(BYTE j = 0; j < 16; j++) { sendByte = *(pFont + ch*48 + i*16 + j); write((mode == NORMAL)? sendByte : (sendByte^0xff), DATA); } @@ -216,10 +208,11 @@ * c - colour, either PIXEL_ON, PIXEL_OFF or PIXEL_XOR * Return value : none */ -void N3310LCD::setPixel( unsigned char x, unsigned char y, unsigned char c ) { -unsigned char value; -unsigned char row; - +void N3310LCD::setPixel( unsigned char x, unsigned char y, unsigned char c ) +{ + unsigned char value; + unsigned char row; + // if( x < 0 || x >= LCDCOLMAX || y < 0 || y >= LCDPIXELROWMAX ) return; if( x >= LCDCOLMAX || y >= LCDPIXELROWMAX ) return; @@ -250,7 +243,8 @@ * Return value : none */ void N3310LCD::drawLine(unsigned char x1, unsigned char y1, - unsigned char x2, unsigned char y2, unsigned char c) { + unsigned char x2, unsigned char y2, unsigned char c) +{ int dx, dy, stepx, stepy, fraction; /* Calculate differential form */ @@ -326,7 +320,8 @@ * Return value : none */ void N3310LCD::drawRectangle(unsigned char x1, unsigned char y1, - unsigned char x2, unsigned char y2, unsigned char c){ + unsigned char x2, unsigned char y2, unsigned char c) +{ drawLine( x1, y1, x2, y1, c ); drawLine( x1, y1, x1, y2, c ); drawLine( x1, y2, x2, y2, c ); @@ -344,7 +339,8 @@ * Return value : none */ void N3310LCD::drawFilledRectangle(unsigned char x1, unsigned char y1, - unsigned char x2, unsigned char y2, unsigned char c) { + unsigned char x2, unsigned char y2, unsigned char c) +{ for(int i=y1; i <= y2; i++ ) { drawLine( x1, i, x2, i, c ); } @@ -353,22 +349,23 @@ /* * Name : drawCircle - * Description : Draw a circle using Bresenham's algorithm. + * Description : Draw a circle using Bresenham's algorithm. * Some small circles will look like squares!! * Argument(s) : xc, yc - Centre of circle * r - Radius * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR - * Return value : None + * Return value : None */ void N3310LCD::drawCircle(unsigned char xc, unsigned char yc, - unsigned char r, unsigned char c) { + unsigned char r, unsigned char c) +{ int x=0; int y=r; int p=3-(2*r); - setPixel( (uint8_t)(xc+x),(uint8_t)(yc-y), c); + setPixel( (uint8_t)(xc+x),(uint8_t)(yc-y), c); - for(x=0;x<=y;x++) { + for(x=0; x<=y; x++) { if (p<0) { y=y; p=(p+(4*x)+6);
--- a/N3310LCD.h Sun Mar 10 18:15:25 2013 +0000 +++ b/N3310LCD.h Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -39,35 +39,35 @@ class N3310LCD { public: - N3310LCD(PinName mosi, PinName miso, PinName sck, + N3310LCD(PinName mosi, PinName miso, PinName sck, PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on); - + void init(); void cls(); void backlight(eBacklight state); - void write(BYTE data, eRequestType req_type); + void write(BYTE data, eRequestType req_type); void locate(BYTE xPos, BYTE yPos); - + void drawBitmap(BYTE xPos, BYTE yPos, BYTE* bitmap, BYTE bmpXSize, BYTE bmpYSize); void clearBitmap(BYTE xPos,BYTE yPos, BYTE size_x, BYTE size_y); - void writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode); + void writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode); void writeStringBig(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode); void writeChar(BYTE ch, eDisplayMode mode); void writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode); - - void setPixel( BYTE x, BYTE y, BYTE c ); + + void setPixel( BYTE x, BYTE y, BYTE c ); void drawLine(BYTE x1, BYTE y1, BYTE x2, BYTE y2, BYTE c); void drawRectangle(BYTE x1, BYTE y1,BYTE x2, BYTE y2, BYTE c); void drawFilledRectangle(BYTE x1, BYTE y1, BYTE x2, BYTE y2, BYTE c); void drawCircle(BYTE xc, BYTE yc, BYTE r, BYTE c); - + private: // I/O SPI lcdPort; // does SPI MOSI, MISO and SCK DigitalOut ceWire; // does SPI CE DigitalOut dcWire; // does 3310 DAT_CMD DigitalOut rstWire; // does 3310 LCD_RST - DigitalOut blWire; // does 3310 BL_ON (backlight) + DigitalOut blWire; // does 3310 BL_ON (backlight) }; #endif \ No newline at end of file
--- a/N3310LCDDefs.h Sun Mar 10 18:15:25 2013 +0000 +++ b/N3310LCDDefs.h Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--- a/N3310SPIConfig.h Sun Mar 10 18:15:25 2013 +0000 +++ b/N3310SPIConfig.h Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -38,11 +38,11 @@ static const PinName LCD_RST; // LCD reset static const PinName DAT_CMD; // indicates if the SPI write is command or date static const PinName BL_ON; // Back Light On - + static const PinName AD0; // analog in for joystick }; -// NOTE pins have been chosen not to conflict with any I2C usage. +// NOTE pins have been chosen not to conflict with any I2C usage. // MOSI = p5, MISO = p6, SCK = p7 is also an option const PinName N3310SPIPort::MOSI = PTD2; const PinName N3310SPIPort::MISO = PTD3; // not used for 3310 @@ -70,7 +70,7 @@ * p10: CE * p9: LCD_RST * p8: DAT_CMD -* +* * Connector J1: * p7: BL_ON *