Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: VariousSG BouncingBalls BounceBall House ... more
SMARTGPU.cpp
- Committer:
- emmanuelchio
- Date:
- 2011-09-12
- Revision:
- 0:32fe54a88167
- Child:
- 1:96ed067e95a6
File content as of revision 0:32fe54a88167:
#include "mbed.h"
#include "SMARTGPU.h"
Serial SGSERIALPORT(TXpin, RXpin); //create the serial port which is connected to SMARTGPU serial port
DigitalOut Rst(resetPin);
// SMART GPU DEFAULT BAUD RATE: 9600bps
// Mbed default configured port for serial communication with SMARTGPU is p13 for TX, and p14 for RX, Resetpin =p15
SMARTGPU::SMARTGPU(){
init();
}
/********** high level commands, for the user! */
void SMARTGPU::init(){ //configure the mbed for SMARTGPU board
SGSERIALPORT.baud(9600);
Rst=1; //set the pin to 3.3v to avoid reset
}
void SMARTGPU::reset(){ //Reset the SMARTGPU board
Rst=0; //set the pin to GND to reset
wait_ms(500);
Rst=1; //set the pin to 3.3v to end reset
wait_ms(500);
}
unsigned char SMARTGPU::start(){ //Init the SMARTGPU
wait_ms(500);
SGSERIALPORT.putc('U');
wait_ms(1000);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::erase(){ //Erase the SMARTGPU screen
SGSERIALPORT.putc('E');
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::sleep(unsigned char mode){ //Send SMARTGPU to sleep mode
SGSERIALPORT.putc('Z');
SGSERIALPORT.putc(mode);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::orientation(unsigned char side){ //Change display orientation
SGSERIALPORT.putc('O');
SGSERIALPORT.putc(side);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::bright(unsigned char val){ //Change display brightness
SGSERIALPORT.putc('V');
SGSERIALPORT.putc(val);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::baudChange(unsigned long val){ //Change baud rate of arduino and SMARTGPU board
unsigned char aux;
switch(val){
case 9600:
aux=0;
break;
case 19200:
aux=1;
break;
case 57600:
aux=2;
break;
case 115200:
aux=3;
break;
case 256000:
aux=4;
break;
case 500000:
aux=5;
break;
case 1000000:
aux=6;
break;
case 2000000:
aux=7;
break;
default:
return 'F';
break;
}
SGSERIALPORT.putc('X');
SGSERIALPORT.putc(aux);
aux=SGSERIALPORT.getc();
if(aux=='O'){
wait_ms(150);
SGSERIALPORT.baud(val);
wait_ms(200);
return SGSERIALPORT.getc();
}else{
return aux;
}
}
unsigned char SMARTGPU::digitalOut(unsigned char number, unsigned char val ){ //Set Digital out pins to a logic value
SGSERIALPORT.putc('D');
SGSERIALPORT.putc(number);
SGSERIALPORT.putc(val);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::setScreenBackground(int colour){ //Change the default screen background colour for erase function
SGSERIALPORT.putc('B');
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::setTextBackground(int colour){ //Set the default text background colour for letters and strings
SGSERIALPORT.putc('A');
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::memoryRead(int x1, int y1, int x2, int y2, char buffer[]){ //Read the internal memory of the SMARTGPU, This command returns 24bit pixels (3 bytes)
unsigned int i,j,k=0;
SGSERIALPORT.putc('M');
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
//receive all the pixels
for(j=0;j<=(y2-y1);j++){
for(i=0;i<=(x2-x1);i++){
buffer[k]=SGSERIALPORT.getc(); //Red
k++;
buffer[k]=SGSERIALPORT.getc(); //Green
k++;
buffer[k]=SGSERIALPORT.getc(); //Blue
k++;
}
}
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::putPixel(int x, int y, int colour){ //Draw a pixel on the screen
SGSERIALPORT.putc('P');
SGSERIALPORT.putc(x>>8);
SGSERIALPORT.putc(x);
SGSERIALPORT.putc(y>>8);
SGSERIALPORT.putc(y);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::drawLine(int x1, int y1, int x2, int y2, int colour){ //Draw a line on the screen
SGSERIALPORT.putc('L');
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::drawRectangle(int x1, int y1, int x2, int y2, int colour, unsigned char fill){ //Draw a rectangle on the screen
SGSERIALPORT.putc('R');
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
SGSERIALPORT.putc(fill);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int colour, unsigned char fill){ //Draw a triangle on the screen
SGSERIALPORT.putc('T');
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
SGSERIALPORT.putc(x3>>8);
SGSERIALPORT.putc(x3);
SGSERIALPORT.putc(y3>>8);
SGSERIALPORT.putc(y3);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
SGSERIALPORT.putc(fill);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::drawCircle(int x, int y, int radius, int colour, unsigned char fill){ //Draw a circle on the screen
SGSERIALPORT.putc('C');
SGSERIALPORT.putc(x>>8);
SGSERIALPORT.putc(x);
SGSERIALPORT.putc(y>>8);
SGSERIALPORT.putc(y);
SGSERIALPORT.putc(radius>>8);
SGSERIALPORT.putc(radius);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
SGSERIALPORT.putc(fill);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::putLetter(int x, int y, int colour, unsigned char font, unsigned char fill, unsigned char letter){ //Draw a letter on the screen
SGSERIALPORT.putc('W');
SGSERIALPORT.putc(x>>8);
SGSERIALPORT.putc(x);
SGSERIALPORT.putc(y>>8);
SGSERIALPORT.putc(y);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
SGSERIALPORT.putc(font);
SGSERIALPORT.putc(fill);
SGSERIALPORT.putc(letter);
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::string(int x1, int y1, int x2, int y2, int colour, unsigned char font, unsigned char fill, char text[]){ //Draw a string on the screen
int counter=0;
SGSERIALPORT.putc('S');
SGSERIALPORT.putc('N'); //not SD
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
SGSERIALPORT.putc(font);
SGSERIALPORT.putc(fill);
while(1){
SGSERIALPORT.putc(text[counter]);
if(text[counter]==0x00){
break;
}
counter++;
}
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::stringSD(int x1, int y1, int x2, int y2, int colour, unsigned char font, unsigned char fill, int BS, int BR, char name[]){ //Draw a String from a text file contained on the micro SD card on the screen
unsigned char counter=0;
SGSERIALPORT.putc('S');
SGSERIALPORT.putc('S'); //from SD
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
SGSERIALPORT.putc(colour>>8);
SGSERIALPORT.putc(colour);
SGSERIALPORT.putc(font);
SGSERIALPORT.putc(fill);
SGSERIALPORT.putc(BS>>8);
SGSERIALPORT.putc(BS);
SGSERIALPORT.putc(BR>>8);
SGSERIALPORT.putc(BR);
while(1){
SGSERIALPORT.putc(name[counter]);
if(name[counter]==0x00){
break;
}
counter++;
}
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::icon(int x1, int y1, int x2, int y2, char icon[]){ //Send and image or icon pixel by pixel to SMARTGPU, 16bit(2 bytes) each pixel RGB565
unsigned int i,j,k=0;
SGSERIALPORT.putc('I');
SGSERIALPORT.putc('N'); //not SD
SGSERIALPORT.putc(x1>>8);
SGSERIALPORT.putc(x1);
SGSERIALPORT.putc(y1>>8);
SGSERIALPORT.putc(y1);
SGSERIALPORT.putc(x2>>8);
SGSERIALPORT.putc(x2);
SGSERIALPORT.putc(y2>>8);
SGSERIALPORT.putc(y2);
//Send icon buffer pixel by pixel
for(j=0;j<=(y2-y1);j++){
for(i=0;i<=(x2-x1);i++){
SGSERIALPORT.putc(icon[k]);
k++;
}
}
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::imageSD(int x, int y, char name[]){ //Draw an Image contained on the micro SD card on the screen, top left corner coordinates
unsigned char counter=0;
SGSERIALPORT.putc('I');
SGSERIALPORT.putc('S'); //from SD
SGSERIALPORT.putc(x>>8);
SGSERIALPORT.putc(x);
SGSERIALPORT.putc(y>>8);
SGSERIALPORT.putc(y);
while(1){
SGSERIALPORT.putc(name[counter]);
if(name[counter]==0x00){
break;
}
counter++;
}
return SGSERIALPORT.getc();
}
unsigned char SMARTGPU::touchScreen(int buffer[]){ //Ask for a touch on the screen, if return=1, touch coordinates are stored on the buffer[]
SGSERIALPORT.putc('G');
buffer[0]=SGSERIALPORT.getc();
buffer[0]=buffer[0]<<8;
buffer[0]|=SGSERIALPORT.getc();
buffer[1]=SGSERIALPORT.getc();
buffer[1]=buffer[1]<<8;
buffer[1]|=SGSERIALPORT.getc();
SGSERIALPORT.getc();
if(buffer[0]<0x0200){
return 1;
}else{
return 0;
}
}
unsigned char SMARTGPU::touchIcon(char buffer[]){ //Ask for a touch on the icons of the screen, if return=1, icon name is stored on the buffer[]
SGSERIALPORT.putc('G');
buffer[0]=SGSERIALPORT.getc();
buffer[1]=SGSERIALPORT.getc();
buffer[2]=SGSERIALPORT.getc();
buffer[3]=SGSERIALPORT.getc();
SGSERIALPORT.getc();
if(!(buffer[0]<0x02) & (buffer[0]!=0x4E)){
return 1;
}else{
return 0;
}
}