main_160x128_11-12-08.c

00001 /*
00002     graphic LCD backpack test code
00003         
00004         set up for the big LCD, 160x128
00005         
00006         10/16/08, everything works. Pixel, line, circle, set x, set y, print_char, clear_screen. All good.
00007 */
00008 
00009 /*
00010 Stuff from Nate to be incorporated...
00011 
00012 In IOINIT:
00013 
00014     //Init timer 2
00015     //8,000,000 / 8 = 1,000,000
00016     TCCR2B = (1<<CS21); //Set Prescaler to 8. CS21=1
00017 
00018 
00019 //General short delays
00020 void delay_ms(uint16_t x)
00021 {
00022     for (; x > 0 ; x--)
00023     {
00024         delay_us(250);
00025         delay_us(250);
00026         delay_us(250);
00027         delay_us(250);
00028     }
00029 }
00030 
00031 //General short delays
00032 void delay_us(uint8_t x)
00033 {
00034     TIFR2 = 0x01; //Clear any interrupt flags on Timer2
00035     
00036     TCNT2 = 256 - x; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click
00037 
00038     while( (TIFR2 & (1<<TOV2)) == 0);
00039 }
00040 */
00041 
00042 #include <avr/io.h>
00043 #include "rprintf.h"
00044 #include <math.h>
00045 #include <avr/interrupt.h>
00046 
00047 #define FOSC 16000000// Clock Speed
00048 #define BAUD 57600
00049 #define MYUBRR FOSC/16/BAUD-1
00050 
00051 #define WR 0        //PC0
00052 #define RD 1        //PC1
00053 #define CE 2        //PC2
00054 #define CD 3        //PC3
00055 #define HALT 4        //PC4
00056 #define RST 5        //PC5
00057 
00058 #define BL_EN 2        //PB2
00059 
00060 #define X_ENDPOINT 159
00061 #define Y_ENDPOINT 127
00062 
00063 //#define        CS20 0
00064 //#define        CS21 1
00065 //#define        CS22 2
00066 
00067 
00068 //Define functions
00069 //======================
00070 void ioinit(void);      //Initializes IO
00071 void delay_ms(uint16_t x); //General purpose delay
00072 void delay(void);
00073 void delay_us(uint8_t x);
00074 void USART_Init( unsigned int ubrr);
00075 void put_char(char byte);
00076 void print_num(short num);
00077 int rnd(float number);
00078 
00079 void set_data(char data);
00080 char read(char D_S);//reads data (D_S = 1) or status (D_S = anything else)
00081 void write(char D_C, char byte);//writes data or command
00082 void display_init(void);
00083 
00084 
00085 void clear_screen(void);
00086 
00087 void print_char(char S_R, char txt);
00088 void del_char(char endpoint);
00089 void pixel(char S_R, char x, char y);
00090 void line(char S_R, char x1, char y1, char x2, char y2);
00091 void circle(char S_R, int x, int y, int r);
00092 void demo(void);
00093 
00094 void erase_block(char x1, char y1, char x2, char y2);
00095 void box(char x1, char y1, char x2, char y2);
00096 
00097 //======================
00098 char x_offset = 0;
00099 char y_offset = 127;
00100 
00101 unsigned char RX_array[256];
00102 volatile unsigned short RX_in = 0;
00103 unsigned short RX_read = 0;
00104 
00105 static char logo[30] = {0x01,0xC0,0x03,0x80,0x03,0x80,0x01,0xD0,
00106                                                 0x01,0xF8,0x0C,0xF8,0x18,0xF8,0x1F,0xF8,
00107                                                 0x1F,0xF8,0x1F,0xF0,0x1F,0xE0,0x1F,0xC0,
00108                                                 0x1C,0x00,0x18,0x00,0x10,0x00};
00109 
00110 //Jacked from Sinister 7 code
00111 static char text_array[475] = {0x00,0x00,0x00,0x00,0x00,/*space*/
00112                               0x00,0xF6,0xF6,0x00,0x00,/*!*/
00113                               0x00,0xE0,0x00,0xE0,0x00,/*"*/
00114                               0x28,0xFE,0x28,0xFE,0x28,/*#*/
00115                               0x00,0x64,0xD6,0x54,0x08,/*$*/
00116                               0xC2,0xCC,0x10,0x26,0xC6,/*%*/
00117                               0x4C,0xB2,0x92,0x6C,0x0A,/*&*/
00118                               0x00,0x00,0xE0,0x00,0x00,/*'*/
00119                               0x00,0x38,0x44,0x82,0x00,/*(*/
00120                               0x00,0x82,0x44,0x38,0x00,/*)*/
00121                               0x88,0x50,0xF8,0x50,0x88,/***/
00122                               0x08,0x08,0x3E,0x08,0x08,/*+*/
00123                               0x00,0x00,0x05,0x06,0x00,/*,*/
00124                               0x08,0x08,0x08,0x08,0x08,/*-*/
00125                               0x00,0x00,0x06,0x06,0x00,/*.*/
00126                               0x02,0x0C,0x10,0x60,0x80,/*/*/
00127                               0x7C,0x8A,0x92,0xA2,0x7C,/*0*/
00128                               0x00,0x42,0xFE,0x02,0x00,/*1*/
00129                               0x42,0x86,0x8A,0x92,0x62,/*2*/
00130                               0x44,0x82,0x92,0x92,0x6C,/*3*/
00131                               0x10,0x30,0x50,0xFE,0x10,/*4*/
00132                               0xE4,0xA2,0xA2,0xA2,0x9C,/*5*/
00133                               0x3C,0x52,0x92,0x92,0x0C,/*6*/
00134                               0x80,0x86,0x98,0xE0,0x80,/*7*/
00135                               0x6C,0x92,0x92,0x92,0x6C,/*8*/
00136                               0x60,0x92,0x92,0x94,0x78,/*9*/
00137                               0x00,0x00,0x36,0x36,0x00,/*:*/
00138                               0x00,0x00,0x35,0x36,0x00,/*;*/
00139                               0x10,0x28,0x44,0x82,0x00,/*<*/
00140                               0x28,0x28,0x28,0x28,0x28,/*=*/
00141                               0x00,0x82,0x44,0x28,0x10,/*>*/
00142                               0x40,0x80,0x8A,0x90,0x60,/*?*/
00143                               0x7C,0x82,0xBA,0xBA,0x62,/*@*/
00144                               0x3E,0x48,0x88,0x48,0x3E,/*A*/
00145                               0xFE,0x92,0x92,0x92,0x6C,/*B*/
00146                               0x7C,0x82,0x82,0x82,0x44,/*C*/
00147                               0xFE,0x82,0x82,0x82,0x7C,/*D*/
00148                               0xFE,0x92,0x92,0x92,0x82,/*E*/
00149                               0xFE,0x90,0x90,0x90,0x80,/*F*/
00150                               0x7C,0x82,0x82,0x8A,0x4E,/*G*/
00151                               0xFE,0x10,0x10,0x10,0xFE,/*H*/
00152                               0x82,0x82,0xFE,0x82,0x82,/*I*/
00153                               0x84,0x82,0xFC,0x80,0x80,/*J*/
00154                               0xFE,0x10,0x28,0x44,0x82,/*K*/
00155                               0xFE,0x02,0x02,0x02,0x02,/*L*/
00156                               0xFE,0x40,0x20,0x40,0xFE,/*M*/
00157                               0xFE,0x60,0x10,0x0C,0xFE,/*N*/
00158                               0x7C,0x82,0x82,0x82,0x7C,/*O*/
00159                               0xFE,0x90,0x90,0x90,0x60,/*P*/
00160                               0x7C,0x82,0x82,0x86,0x7E,/*Q*/
00161                               0xFE,0x90,0x98,0x94,0x62,/*R*/
00162                               0x64,0x92,0x92,0x92,0x4C,/*S*/
00163                               0x80,0x80,0xFE,0x80,0x80,/*T*/
00164                               0xFC,0x02,0x02,0x02,0xFC,/*U*/
00165                               0xF8,0x04,0x02,0x04,0xF8,/*V*/
00166                               0xFC,0x02,0x0C,0x02,0xFC,/*W*/
00167                               0xC6,0x28,0x10,0x28,0xC6,/*X*/
00168                               0xC0,0x20,0x1E,0x20,0xC0,/*Y*/
00169                               0x86,0x8A,0x92,0xA2,0xC2,/*Z*/
00170                               0x00,0x00,0xFE,0x82,0x00,/*[*/
00171                               0x80,0x60,0x10,0x0C,0x02,/*this should be / */
00172                               0x80,0x60,0x10,0x0C,0x02,/*]*/
00173                               0x20,0x40,0x80,0x40,0x20,/*^*/
00174                               0x01,0x01,0x01,0x01,0x01,/*_*/
00175                               0x80,0x40,0x20,0x00,0x00,/*`*/
00176                               0x04,0x2A,0x2A,0x2A,0x1E,/*a*/
00177                               0xFE,0x12,0x22,0x22,0x1C,/*b*/
00178                               0x1C,0x22,0x22,0x22,0x14,/*c*/
00179                               0x1C,0x22,0x22,0x12,0xFE,/*d*/
00180                               0x1C,0x2A,0x2A,0x2A,0x18,/*e*/
00181                               0x10,0x7E,0x90,0x80,0x40,/*f*/
00182                               0x18,0x25,0x25,0x25,0x1E,/*g*/
00183                               0xFE,0x10,0x10,0x10,0x0E,/*h*/
00184                               0x00,0x12,0x5E,0x02,0x00,/*i*/
00185                               0x02,0x01,0x01,0x11,0x5E,/*j*/
00186                               0xFE,0x08,0x08,0x14,0x22,/*k*/
00187                               0x00,0x82,0xFE,0x02,0x00,/*l*/
00188                               0x3E,0x20,0x1C,0x20,0x1E,/*m*/
00189                               0x3E,0x20,0x20,0x20,0x1E,/*n*/
00190                               0x1C,0x22,0x22,0x22,0x1C,/*o*/
00191                               0x3F,0x24,0x24,0x24,0x18,/*p*/
00192                               0x18,0x24,0x24,0x3F,0x01,/*q*/
00193                               0x3E,0x10,0x20,0x20,0x10,/*r*/
00194                               0x12,0x2A,0x2A,0x2A,0x04,/*s*/
00195                               0x00,0x10,0x3C,0x12,0x04,/*t*/
00196                               0x3C,0x02,0x02,0x02,0x3E,/*u*/
00197                               0x30,0x0C,0x02,0x0C,0x30,/*v*/
00198                               0x38,0x06,0x18,0x06,0x38,/*w*/
00199                               0x22,0x14,0x08,0x14,0x22,/*x*/
00200                               0x38,0x05,0x05,0x05,0x3E,/*y*/
00201                               0x22,0x26,0x2A,0x32,0x22,/*z*/
00202                               0x00,0x10,0x6C,0x82,0x82,/*{*/
00203                               //0x00,0x00,0xFF,0x00,0x00,/*|*/
00204                               0x04,0x02,0xFF,0x02,0x04,/*|, arrow*/
00205                               0x82,0x82,0x6C,0x10,0x00,/*}*/
00206                               0x08,0x10,0x18,0x08,0x10};/*~*/
00207                                                           
00208                                                           
00209 
00210 ISR (SIG_USART_RECV)                                                                        //USART Receive Interrupt
00211 {
00212         cli();//Disable Interrupts
00213         RX_array[RX_in] = UDR0;
00214         
00215         RX_in++;
00216         
00217         if (RX_in >= 256) RX_in = 0;
00218         
00219         sei();//Enable Interrupts
00220         
00221 }
00222 
00223 
00224 int main (void)
00225 {
00226         char x, y, a;
00227         //short a, b;
00228         
00229         //char a = 0;
00230     ioinit(); //Setup IO pins and defaults
00231         USART_Init( MYUBRR);
00232         rprintf_devopen(put_char); /* init rrprintf */
00233         
00234         //reset the display
00235         delay_ms(1);
00236         PORTC |= (1<<RST);
00237         
00238         //initialize the display
00239         display_init();
00240 
00241         clear_screen();
00242         
00243         //Backlight on
00244         PORTB &= (~(1<<BL_EN));
00245         
00246         while(1)
00247         {
00248                 if(RX_in != RX_read)
00249                 {
00250                         x = RX_array[RX_read];
00251                         RX_read++;
00252                         if(RX_read >= 256) RX_read = 0;
00253                         
00254                         //Backspace===================================================
00255                         if(x == 8) del_char(0);
00256                         
00257                         //Special commands
00258                         else if (x == 124)
00259                         {        
00260                                 //make sure the next byte is there
00261                                 while(RX_in == RX_read);
00262                                 
00263                                 //0, clear screen======================================================
00264                                 if(RX_array[RX_read] == 0)
00265                                 {
00266                                         clear_screen();
00267                                         RX_read++;
00268                                         if(RX_read >= 256) RX_read = 0;
00269                                 }
00270                                 
00271                                 
00272                                 //Backlight on/off
00273                                 else if(RX_array[RX_read] == 2)
00274                                 {
00275                                         y = PINB;
00276                                         if (y & (1<<BL_EN)) PORTB &= (~(1<<BL_EN));
00277                                         else PORTB |= (1<<BL_EN);
00278                                         RX_read++;
00279                                 }
00280                                 
00281                                 //demo mode
00282                                 else if(RX_array[RX_read] == 4)
00283                                 {
00284                                         RX_in = 0, RX_read = 0;
00285                                         demo();
00286                                         clear_screen();
00287                                         RX_in = 0;
00288                                 }
00289                                 
00290                                 else
00291                                 {                                
00292                                         //set x or y=========================================================
00293                                         if((RX_array[RX_read] == 24) | (RX_array[RX_read] == 25))
00294                                         {
00295                                                 RX_read++;
00296                                                 if(RX_read >= 256) RX_read = 0;
00297                                                 while(RX_in == RX_read);//wait for byte
00298                                                 if (RX_array[RX_read-1] == 24) x_offset = RX_array[RX_read];
00299                                                 else if (RX_array[RX_read-1] == 25) y_offset = RX_array[RX_read];
00300                                                 
00301                                                 RX_read++;
00302                                                 if(RX_read >= 256) RX_read = 0;
00303                                                 
00304                                                 if (x_offset > 159) x_offset = 159;
00305                                                 if (y_offset > 127) y_offset = 127;
00306 
00307                                         }
00308 
00309                                         //set pixel=========================================================
00310                                         if (RX_array[RX_read] == 16)
00311                                         {
00312                                                 //need 3 bytes
00313                                                 for (y = 0; y < 3; y++)
00314                                                 {
00315                                                         RX_read++;
00316                                                         if(RX_read >= 256) RX_read = 0;
00317                                                         while(RX_in == RX_read);//wait for byte
00318                                                 }
00319                                                 
00320                                                 pixel(RX_array[RX_read], RX_array[RX_read-2], RX_array[RX_read-1]);
00321                                                 
00322                                                 RX_read++;
00323                                                 if(RX_read >= 256) RX_read = 0;
00324 
00325                                         }
00326 
00327                                         //<ctrl>c, circle======================================================
00328                                         if(RX_array[RX_read] == 3)
00329                                         {
00330                                                 //need 4 bytes
00331                                                 for (y = 0; y < 4; y++)
00332                                                 {
00333                                                         RX_read++;
00334                                                         if(RX_read >= 256) RX_read = 0;
00335                                                         while(RX_in == RX_read);//wait for byte
00336                                                 }
00337                                                 
00338                                                 circle(RX_array[RX_read], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1]);
00339                                                 
00340                                                 RX_read++;
00341                                                 if(RX_read >= 256) RX_read = 0;
00342                                         }
00343                                         
00344                                         
00345                                         //<ctrl>e, erase block======================================================
00346                                         if(RX_array[RX_read] == 5)
00347                                         {
00348                                                 //need 4 bytes
00349                                                 for (y = 0; y < 4; y++)
00350                                                 {
00351                                                         RX_read++;
00352                                                         if(RX_read >= 256) RX_read = 0;
00353                                                         while(RX_in == RX_read);//wait for byte
00354                                                 }
00355                                                 
00356                                                 erase_block(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]);
00357                                                 
00358                                                 RX_read++;
00359                                                 if(RX_read >= 256) RX_read = 0;
00360                                         }
00361                                         
00362                                         
00363                                         //<ctrl>o, box, running out of meaningful letters======================================================
00364                                         if(RX_array[RX_read] == 15)
00365                                         {
00366                                                 //need 4 bytes
00367                                                 for (y = 0; y < 4; y++)
00368                                                 {
00369                                                         RX_read++;
00370                                                         if(RX_read >= 256) RX_read = 0;
00371                                                         while(RX_in == RX_read);//wait for byte
00372                                                 }
00373                                                 
00374                                                 box(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]);
00375                                                 
00376                                                 RX_read++;
00377                                                 if(RX_read >= 256) RX_read = 0;
00378                                         }
00379 
00380 
00381                                         //<ctrl>L, line========================================================
00382                                         else if (RX_array[RX_read] == 12)
00383                                         {
00384                                                 //need 5 bytes
00385                                                 for (y = 0; y < 5; y++)
00386                                                 {
00387                                                         RX_read++;
00388                                                         if(RX_read >= 256) RX_read = 0;
00389                                                         while(RX_in == RX_read);//wait for byte
00390                                                 }
00391                                                 
00392                                                 line(RX_array[RX_read], RX_array[RX_read-4], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read+-1]);
00393                                                 RX_read++;
00394                                                 if(RX_read >= 256) RX_read = 0;
00395                                         }
00396                                         
00397                                         
00398                                 }
00399         
00400                         }
00401                         
00402                         //print character to the screen===============================================
00403                         else
00404                         {
00405                                 del_char(1);
00406                                 print_char(1, x);
00407                         }
00408                 }
00409                 
00410         }
00411         
00412         
00413 
00414 }
00415 
00416 void ioinit (void)
00417 {
00418         
00419     //1 = output, 0 = input
00420         
00421         /*
00422         WR        //PC0
00423         RD        //PC1
00424         CE        //PC2
00425         C_D //PC3
00426         HALT        //PC4
00427         RST        //PC5
00428         */
00429 
00430         PORTB |= (1<<BL_EN);//Backlight off
00431         DDRB |= (1<<BL_EN);//set PB2 as output
00432         
00433         //set these in the read/write functions instead of here
00434         //DDRB = 0b00000011; //PB0 and PB1 are outs
00435         //DDRD = 0b11111100; //PD2-PD7 are also outs.  Ports B and D are the data bus.
00436 
00437         PORTC = ((1<<WR) | (1<<RD) | (1<<CE) | (1<<CD) | (1<<HALT));
00438         PORTC &= (~(1<<RST));//set the reset line low at power up
00439         DDRC = ((1<<WR) | (1<<RD) | (1<<CE) | (1<<CD) | (1<<HALT) | (1<<RST));
00440         
00441         //Init timer 2
00442     //8,000,000 / 8 = 1,000,000
00443     TCCR2B = (1<<CS21); //Set Prescaler to 8. CS21=1
00444         //TCCR2 = ((1<<CS20) | (1<<CS22) | (1<<CS22));
00445         
00446 }
00447 
00448 //General short delays
00449 void delay_ms(uint16_t x)
00450 {
00451         for (; x > 0 ; x--)
00452     {
00453         delay_us(250);
00454         delay_us(250);
00455         delay_us(250);
00456         delay_us(250);
00457     }
00458         
00459 }
00460 
00461 //General short delays
00462 void delay_us(uint8_t x)
00463 {
00464         char temp;
00465         
00466         if (x == 0) temp = 1;
00467         else temp = x;
00468     //TIFR = 0x01; //Clear any interrupt flags on Timer2
00469         TIFR2 |= 0x01;
00470     
00471     TCNT2 = 256 - temp; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click
00472 
00473     //while( (TIFR & (1<<TOV2)) == 0);
00474         while(!(TIFR2 & 0x01));
00475         
00476         if (x == 0) return;//this is for display timing
00477         
00478         
00479         //The prescaler doesn't allow for a setting of 16, just 8 or 32. So, we do this twice.
00480         TIFR2 |= 0x01;
00481     
00482     TCNT2 = 256 - temp; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click
00483 
00484     //while( (TIFR & (1<<TOV2)) == 0);
00485         while(!(TIFR2 & 0x01));
00486         
00487 }
00488 
00489 void USART_Init( unsigned int ubrr)
00490 {
00491         // Set baud rate 
00492         UBRR0H = (unsigned char)(ubrr>>8);
00493         UBRR0L = (unsigned char)ubrr;
00494         // Enable receiver and transmitter 
00495         //UCSRB = (1<<RXEN)|(1<<TXEN);
00496         UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);        //Enable Interrupts on receive character
00497         // Set frame format: 8data, 2stop bit 
00498         //UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
00499         UCSR0C = (1<<UMSEL00)|(1<<UCSZ00)|(1<<UCSZ01);
00500         sei();
00501 }
00502 
00503 void put_char(char byte)
00504 {
00505         /* Wait for empty transmit buffer */
00506         while ( !( UCSR0A & (1<<UDRE0)) );
00507         /* Put data into buffer, sends the data */
00508         UDR0 = byte;
00509 }
00510 
00511 //delay for display timing
00512 void delay(void)
00513 {
00514         char y;
00515         
00516         for(y = 0; y < 20; y++)
00517         {
00518                 asm volatile ("nop");
00519                 
00520         }
00521         
00522         /*
00523         asm volatile ("nop");
00524         asm volatile ("nop");
00525         asm volatile ("nop");
00526         asm volatile ("nop");
00527         asm volatile ("nop");
00528         */
00529         
00530 }
00531 
00532 //set data port
00533 void set_data(char data)
00534 {
00535         //PORTB
00536         //DB0 = PB0
00537         //DB1 = PB1
00538         
00539         PORTB &= 0xFC;
00540         
00541         //PORTD
00542         //DB2 = PD2
00543         //DB3 = PD3
00544         //DB4 = PD4
00545         //DB5 = PD5
00546         //DB6 = PD6
00547         //DB7 = PD7
00548         
00549         PORTD &= 0x03;
00550         
00551         PORTB |= (data & 0x03);
00552         PORTD |= (data & 0xFC);
00553 
00554 }
00555 
00556 
00557 //Reads data or status
00558 //for data D_S = 1, for status D_S = 0
00559 //returns the value of the data bus
00560 char read(char D_S)
00561 {
00562         char data1 = 0, data2 = 0;
00563         
00564         DDRB &= 0xFC;//PB0 and PB1 inputs
00565         DDRD &= 0x02;//everything but PD1 as input
00566         
00567         PORTC &= ~((1 << RD) | (1 << CE));//CD high for status
00568         if (D_S == 1) PORTC &= ~(1 << CD);//CD down for data
00569         
00570         delay_us(0);
00571         
00572         data1 = PINB;
00573         data1 &= 0x03;
00574         
00575         data2 = PIND;
00576         data2 &= 0xFC;
00577         
00578         data1 |= data2;
00579         
00580         PORTC |= ((1 << CD) | (1 << RD) | (1 << CE));//all up
00581         
00582         delay_us(0);
00583         
00584         return data1;
00585 
00586 }
00587 
00588 
00589 //Writes data (D_C = 1) or command (D_C = anything else)
00590 void write(char D_C, char byte)
00591 {
00592         DDRB |= 0x03; //PB0 and PB1 are outs
00593         DDRD |= 0xFC; //PD2-PD7 are also outs.  Ports B and D are the data bus
00594         
00595         set_data(byte);
00596         
00597         if (D_C == 1) PORTC &= ~((1 << WR) | (1 << CE) | (1 << CD));//down
00598         else PORTC &= ~((1 << WR) | (1 << CE));//down
00599         
00600         delay_us(0);
00601         PORTC |= ((1 << CD) | (1 << WR) | (1 << CE));//all up
00602         delay();
00603         DDRB &= 0xFC;//PB0 and PB1 inputs
00604         DDRD &= 0x02;//everything but PD1 as input
00605         
00606         delay_us(0);
00607 
00608 }
00609 
00610 
00611 
00612 void display_init(void)
00613 {
00614         //set graphics home address to 0
00615         while(!(read(0) & 3));//read status
00616         write(1, 0);
00617         while(!(read(0) & 3));//read status
00618         write(1, 0);
00619         while(!(read(0) & 3));//read status
00620         write(0, 0x42);
00621 
00622         //set graphics area
00623         while(!(read(0) & 3));//read status
00624         write(1, 20);//20 bytes, 160/8
00625         while(!(read(0) & 3));//read status
00626         write(1, 0);
00627         while(!(read(0) & 3));//read status
00628         write(0, 0x43);
00629         
00630         //set mode
00631         while(!(read(0) & 3));//read status
00632         write(0, 0x80);//Or, with internal character generator
00633         
00634         //set display mode
00635         while(!(read(0) & 3));//read status
00636         write(0, 0x98);//Graphics on
00637 
00638 }
00639 
00640 
00641 
00642 
00643 
00644 void clear_screen(void)
00645 {
00646         int x;
00647         
00648         //set address pointer to 0, start of graphics
00649         while(!(read(0) & 3));//read status
00650         write(1, 0);
00651         while(!(read(0) & 3));//read status
00652         write(1, 0);
00653         while(!(read(0) & 3));//read status
00654         write(0, 0x24);
00655         
00656         for(x = 0; x < 0xA00; x++)
00657         {
00658                 while(!(read(0) & 3));//read status
00659                 write(1, 0);
00660                 while(!(read(0) & 3));//read status
00661                 write(0, 0xC0);
00662         }
00663         
00664         x_offset = 0;
00665         y_offset = 127;
00666 }
00667 
00668 /*
00669 void set_cursor(char x_spot, char y_spot)
00670 {
00671         //set address pointer to 0, start of graphics
00672         while(!(read(0) & 3));//read status
00673         write(1, x_spot);
00674         while(!(read(0) & 3));//read status
00675         write(1, y_spot);
00676         while(!(read(0) & 3));//read status
00677         write(0, 0x21);
00678 
00679 }
00680 */
00681 
00682 void pixel(char S_R, char x, char y)
00683 {
00684         short address = 0;
00685         char byte = 0;
00686                 
00687         //don't try to print something outside of our range
00688         if (x > 159) return;
00689         if (y > 127) return;
00690         
00691         address = ((127-y) * 20) + (x / 8);
00692         
00693         //set address pointer
00694         while(!(read(0) & 3));//read status
00695         byte = (char)(address & 0xFF);
00696 
00697         write(1, byte);//20 bytes, 160/8
00698         
00699         while(!(read(0) & 3));//read status
00700         byte = (char)((address & 0xFF00) >> 8);
00701 
00702         write(1, byte);
00703         
00704         while(!(read(0) & 3));//read status
00705         write(0, 0x24);
00706         
00707         byte = ~(x % 8);
00708 
00709         byte |= 0xF8;
00710         if (S_R == 0) byte &= 0xF7;
00711         
00712         //set-reset bit
00713         while(!(read(0) & 3));//read status
00714         write(0, byte);
00715         
00716 }
00717 
00718 
00719 
00720 void line(char S_R, char x1, char y1, char x2, char y2)
00721 {
00722         float m, q;
00723         int x_dif, y_dif;
00724         int a, b, c;
00725         
00726         if ((x1 > X_ENDPOINT) | (x2 > X_ENDPOINT)) return;
00727         //if ((x1 < 0) | (x2 < 0)) return;
00728         if ((y1 > Y_ENDPOINT) | (y2 > Y_ENDPOINT)) return;
00729         //if ((y1 < 0) | (y2 < 0)) return;
00730         
00731         x_dif = x2 - x1;
00732         y_dif = y2 - y1;
00733         if (y_dif < 0) y_dif *= (-1);
00734         
00735 
00736         m = (float)(y2 - y1) / (float)(x2 - x1);
00737         
00738         b = y1-(m*x1);
00739         
00740         if(x_dif >= y_dif)
00741         {
00742                 for (a = x1; a <= x2; a++)
00743                 {
00744                         pixel(S_R, (char)a, (char)((m*a)+b));
00745                         //delay_ms(25);
00746                 }
00747         }
00748         
00749         else
00750         {
00751                 if (y2 > y1)
00752                 {
00753                         for (a = y1; a <= y2; a++)
00754                         {
00755                                 if (x_dif == 0) c = x1;
00756                                 else
00757                                 {
00758                                         q = (((float)(a-b))/m);
00759                                         c = rnd(q);
00760                                 }
00761                                 
00762                                 pixel(S_R, (char)c, (char)a);
00763                                 //delay_ms(25);
00764                         }
00765                 }
00766                 
00767                 else if (y1 > y2)
00768                 {
00769                         for (a = y1; a >= y2; a--)
00770                         {
00771                                 if (x_dif == 0) c = x1;
00772                                 else 
00773                                 {
00774                                         q = (((float)(a-b))/m);
00775                                         c = rnd(q);
00776                                 }
00777                         
00778                                 pixel(S_R, (char)c, (char)a);
00779                                 //delay_ms(25);
00780                         }
00781                 }
00782         }
00783                 
00784 }
00785 
00786 
00787 
00788 void circle(char S_R, int x, int y, int r)
00789 {
00790         int x1 = 0, x2 = 0;
00791         int x_line = 0, y_line = 0;
00792         int temp_y;
00793         int temp_x;
00794         //float x_float, y_float, r_float;
00795         
00796         x1 = x - r;
00797         x2 = x + r;
00798         
00799         for (temp_x = x1; temp_x <= x2; temp_x++)
00800         {
00801                 temp_y = ((sqrt((r*r) - ((temp_x - x)*(temp_x - x)))) - y);
00802                 
00803                 temp_y *= (-1);
00804                 
00805                 /*
00806                 print_num((short)temp_x);
00807                 put_char(9);
00808                 print_num((short)temp_y);
00809                 put_char(10);
00810                 put_char(13);
00811                 */
00812                 if (temp_x > x1)
00813                 {
00814                         line(S_R, (char)x_line, (char)y_line, (char)temp_x, (char)temp_y);
00815                         line(S_R, (char)x_line, (char)(2*y - y_line), (char)temp_x, (char)(2*y - temp_y));
00816                 }
00817                         
00818                 else 
00819                 {
00820                         pixel(S_R, (char)temp_x, (char)temp_y);
00821                         pixel(S_R, (char)temp_x, (char)(y + y - temp_y));
00822                 }
00823                 
00824                 x_line = temp_x;
00825                 y_line = temp_y;
00826                 
00827                 //
00828                 
00829                 
00830         }
00831         
00832 
00833 }
00834 
00835 
00836 int rnd(float number)
00837 {
00838         int a;
00839         float b;
00840         
00841         a = number / 1;
00842         b = number - a;
00843         
00844         if (b >= 0.5) a++;
00845         
00846         return a;
00847 
00848 }
00849 
00850 
00851 void print_char(char S_R, char txt)
00852 {
00853     short text_array_offset = (txt - 32)*5, j;
00854     char x, k;
00855         
00856         
00857     for (j = text_array_offset; j < text_array_offset+5; j++)
00858     {
00859                 
00860                 
00861                 k = text_array[j];
00862                 
00863                 for (x = 0; x < 8; x++)
00864                 {
00865                         if(k & 0x80) pixel(S_R, x_offset, y_offset - x);
00866                         k <<= 1;
00867                 }
00868                         
00869                 //if (j == text_array_offset+5) x_offset++;//blank byte for letter spacing
00870 
00871                         
00872                 x_offset++;
00873                 
00874     }
00875         
00876         x_offset++;
00877         
00878     if ((x_offset + 6) > 159)
00879         {
00880                 x_offset = 0;
00881                 if (y_offset <= 7) y_offset = 127;
00882                 else y_offset -= 8;
00883                 
00884         }
00885         
00886 }
00887 
00888 
00889 
00890 void print_num(short num)
00891 {
00892         short a;
00893         char b, c;
00894         a = num;
00895         
00896         //print hex
00897         for (c = 0; c < 4; c++)
00898         {
00899                 b = (char)((a & 0xF000) >> 12);
00900                 if (b < 10) put_char(b+48);
00901                 else put_char(b+55);
00902                 a <<= 4;
00903         }
00904         
00905         //decimal
00906         /*
00907         b = a/100;
00908         putchr(b+48);
00909         a -= b*100;
00910         b = a/10;
00911         putchr(b+48);
00912         a -= b*10;
00913         putchr(a+48);
00914         */
00915         
00916 }
00917 
00918 
00919 void demo(void)
00920 {
00921         char x, y, temp;
00922         int q = 0;
00923         
00924         while(1)
00925         {        
00926                 x_offset = 0;
00927                 y_offset = 127;
00928         
00929                 for (y = 0; y < 5; y++)
00930                 {
00931                         for (x = 32; x < 123; x++)
00932                         {        
00933                                 del_char(1);
00934                                 print_char(1, x);
00935                                 if (RX_in > 0) return;
00936                         }
00937                 }
00938                 
00939                 clear_screen();
00940                 
00941                 for (y = 0; y < 5; y++)
00942                 {
00943                         for (x = 32; x < 123; x++)
00944                         {
00945                                 //x_offset += 4;
00946                                 y_offset -= 6;
00947                                 if (y_offset <= 8) y_offset = 127;
00948                                 del_char(1);
00949                                 print_char(1, x);
00950                                 if (RX_in > 0) return;
00951                         }
00952                 }
00953                 
00954                 clear_screen();
00955                 
00956                 //draw circles================================
00957                 for (x = 5; x < 120; x += 5)
00958                 {
00959                         circle(1,80,64,x);
00960                         if (RX_in > 0) return;
00961                 }
00962                 
00963                 
00964                 //draw lines===================================
00965                 y = Y_ENDPOINT;
00966                 
00967                 for (x = 0; x < X_ENDPOINT; x += 20)
00968                 {
00969                         line(1,0,y,x,0);
00970                         y -= 16;
00971                 }
00972                 
00973                 y = 0;
00974                 
00975                 for (x = 0; x < X_ENDPOINT; x += 20)
00976                 {
00977                         line(1,x,0,X_ENDPOINT,y);
00978                         y += 16;
00979                 }
00980                 
00981                 y = Y_ENDPOINT;
00982                 
00983                 for (x = 0; x < X_ENDPOINT; x += 20)
00984                 {
00985                         line(1,x,Y_ENDPOINT,X_ENDPOINT,y);
00986                         y -= 16;
00987                 }
00988                 
00989                 y = 0;
00990                 
00991                 for (x = 0; x < X_ENDPOINT; x += 20)
00992                 {
00993                         line(1,0,y,x,Y_ENDPOINT);
00994                         y += 16;
00995                 }
00996                 
00997                 
00998                 //erase circles================================
00999                 for (x = 5; x < 120; x += 5)
01000                 {
01001                         circle(0,80,64,x);
01002                         if (RX_in > 0) return;
01003                 }
01004 
01005                 //erase lines===================================
01006                 y = Y_ENDPOINT;
01007                 
01008                 for (x = 0; x < X_ENDPOINT; x += 20)
01009                 {
01010                         line(0,0,y,x,0);
01011                         y -= 16;
01012                 }
01013                 
01014                 y = 0;
01015                 
01016                 for (x = 0; x < X_ENDPOINT; x += 20)
01017                 {
01018                         line(0,x,0,X_ENDPOINT,y);
01019                         y += 16;
01020                 }
01021                 
01022                 y = Y_ENDPOINT;
01023                 
01024                 for (x = 0; x < X_ENDPOINT; x += 20)
01025                 {
01026                         line(0,x,Y_ENDPOINT,X_ENDPOINT,y);
01027                         y -= 16;
01028                 }
01029                 
01030                 y = 0;
01031                 
01032                 for (x = 0; x < X_ENDPOINT; x += 20)
01033                 {
01034                         line(0,0,y,x,Y_ENDPOINT);
01035                         y += 16;
01036                 }
01037                 
01038                 if (RX_in > 0) return;
01039                 
01040                 //Boxes=================================================================
01041                 y = 111;
01042                 for (x = 0; x <= 140; x += 10)
01043                 {
01044                         erase_block(x, y, x+16, y+16);
01045                         box(x, y, x+16, y+16);
01046                         y -= 7;
01047                 }
01048                 
01049                 
01050                 //x = 110;
01051                 y = 28;
01052                 //Logo=================================================================
01053                 q = 0;
01054                 while(q < 30)
01055                 {
01056                         temp = logo[q];
01057                         for (x = 140; x < 148; x++)
01058                         {
01059                                 if (temp & 0x80) pixel(1,x,y);
01060                                 
01061                                 temp <<= 1;
01062                         }
01063                         q++;
01064                         temp = logo[q];
01065                         for (x = 148; x < 156; x++)
01066                         {
01067                                 if (temp & 0x80) pixel(1,x,y);
01068                                 
01069                                 temp <<= 1;
01070                         }
01071                         y--;
01072                         q++;
01073                 }        
01074                 
01075                 delay_ms(3000);
01076                 clear_screen();
01077                 
01078         }
01079         
01080 
01081 }
01082 
01083 //Deletes a character. Endpoint == 0 for a backwards delete,
01084 //Endpoint != 0 to erase spot for a new character write
01085 void del_char(char endpoint)
01086 {
01087         char a, y;
01088         
01089         if (endpoint == 0)//Backwards delete
01090         {
01091                 if (x_offset <= 5)
01092                 {
01093                         //x_offset = 150;
01094                         //if (y_offset >= 120) y_offset = 7;
01095                         //else y_offset += 8;
01096                         
01097                         x_offset += 152;
01098                         y_offset += 8;
01099                         
01100                         if (y_offset > 127) y_offset -= 128;
01101                 }
01102                 
01103                 else x_offset -= 6;
01104         }
01105         /*        
01106         for (a = 0; a < 6; a++)
01107         {                                        
01108                 for (y = 0; y < 8; y++)
01109                 {
01110                         pixel(0, x_offset + a, y_offset - y);
01111                         
01112                 }
01113         }
01114         */
01115         
01116         for (a = x_offset; a < x_offset + 6; a++)
01117         {                                        
01118                 for (y = y_offset - 7; y <= y_offset; y++)
01119                 {
01120                         pixel(0, a, y);
01121                         //put_char('L');
01122                 }
01123         }
01124                 
01125 }
01126 
01127 
01128 void erase_block(char x1, char y1, char x2, char y2)
01129 {
01130         static char temp_x = 0, temp_y = 0;
01131         
01132         for (temp_y = y2; temp_y >= y1; temp_y--)
01133         {
01134                 for (temp_x = x1; temp_x <= x2; temp_x++)
01135                 {
01136                         pixel(0, temp_x, temp_y);
01137                         
01138                         //rprintf("%d ",temp_x);
01139                         //rprintf("%d \r\n",temp_y);
01140                         //delay_ms(1000);
01141                         
01142                 }
01143         }        
01144         
01145         
01146 
01147 }
01148 
01149 void box(char x1, char y1, char x2, char y2)
01150 {
01151         //static char temp_x = 0, temp_y = 0;
01152         
01153         line(1, x1, y1, x1, y2);
01154         line(1, x1, y1, x2, y1);
01155         line(1, x2, y1, x2, y2);
01156         line(1, x1, y2, x2, y2);
01157 
01158 }
01159 
01160 
01161 
01162 
01163 
Generated on Sun May 8 08:41:17 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3