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