main_160x128_11-14-08.c

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