Index: lcd.c
===================================================================
--- lcd.c	(revision 1344)
+++ lcd.c	(revision 1345)
@@ -1,24 +1,42 @@
-/*
+/**
+ * Copyright (c) 2007 Colony Project
+ * 
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ **/
 
-lcd-ar2.c - implementation for lcd functions
 
-Author: CMU Robotics Club, Colony Project
-
-This uses SPI.
-
-lcd_init MUST be called before anything can be used.
-
-*/
-
+/**
+ * @file lcd.c
+ * @brief LCD
+ *
+ * Implementation of functions for using the LCD.
+ *
+ * @author Colony Project, CMU Robotics Club
+ **/
 #include <avr/io.h>
 #include <lcd.h>
 #include <time.h>
 
-#define LCD_RS    PB4 // Command/Data
-#define LCD_RSTB  PE2 // reset line
-#define LCD_CS    PB0
-
-//////lcd defines
+//LCD defines
 #define RST _BV(4)  // pd4 (GPIO)
 #define SCE _BV(0)  // pb0 (~SS)
 #define D_C _BV(5)  // pd5 (GPIO?)
@@ -31,201 +49,15 @@
 #define LCDRESETPORT PORTD
 #define LCDRESETDDR DDRD
 
-// Internal Function Prototypes
-// inialize OLED and SPI
-void OLED_init(void);
-
-// reset Controller
-void Reset_SSD1339(void);
-
-// write command or data
-void write_c(unsigned char out_command);
-void write_d(unsigned char out_data);
-
-// these write data to the OLED on 8 bit data bus,  depends on MCU
-void LCD_out(unsigned char cmd);
-
-// these functions set / clear pins for OLED control lines.  they accecpt a 0 or 1 
-void DC(char stat);
-void RES(char stat);
-void CS(char stat);
-
 void lcd_putbyte(unsigned char b);
 
-void crazycircle (void) {
-/*  int i;
-  // draw 100 random circles
-  for(i = 0;i < 100;i++){
-      write_c(0x86);    // draw circle command
-      write_d(rand() % 130);
-      write_d(rand() % 130);
-      write_d(rand() % 64);
-      write_d(rand());
-      write_d(rand());
-      write_d(rand());
-      write_d(rand());
 
-      delay_ms(10);
-  }
-*/
-}
-
-void OLEDtest (void) {
-  //int   i = 0;
-
-  // Initialize
-  //Initialize();
-  OLED_init();
-
-  delay_ms(120);
-
-  write_c(0x8e);    // clear window command
-  write_d(0);
-  write_d(0);
-  write_d(130);
-  write_d(130);
-
-  delay_ms(100);
-  
-  write_c(0x92);    // fill enable command
-  write_d(0x01); 
-  
-  delay_ms(10);
-
-  crazycircle();
-/*
-  // write directly to ram,  this fills up bottom 1/3 of display with color pattern
-  write_c(0x5c);
-  for (i = 0; i < 2000; i++){
-  write_c(0x5c);  
-   write_d(i);
-   write_d(i);
-   write_d(i);
-  }
-  */
-} 
-
-/**********************************************************
-                      Initialize
-**********************************************************/
-
-void OLED_init(void)
-{
-  // Setup SPI here
-  SPCR = 0x5D; //  enable SPI, master, SPI mode 3
-  DDRB |= 0x06; // enable MOSI and SCK as outputs
-
-  LCD_out(0);
-  DC(0);
-  CS(0);
-  Reset_SSD1339();
-  write_c(0xa0); // Set Re-map / Color Depth
-  write_d(0x34);//0xb4); // 262K 8bit R->G->B
-  write_c(0xa1); // Set display start line
-  write_d(0x00); // 00h start
-  //write_c(0xa2); // Set display offset
-  //write_d(0x80); // 80h start
-  write_c(0xA6); // Normal display
-  write_c(0xad); // Set Master Configuration
-  write_d(0x8e); // DC-DC off & external VcomH voltage & external pre-charge voltage
-  write_c(0xb0); // Power saving mode
-  write_d(0x05);
-  write_c(0xb1); // Set pre & dis_charge
-  write_d(0x11); // pre=1h dis=1h
-  write_c(0xb3); // clock & frequency
-  write_d(0xf0); // clock=Divser+1 frequency=fh
-  write_c(0xbb); // Set pre-charge voltage of color A B C
-  write_d(0xff); // color A  was 1c
-  write_d(0xff); // color B  was 1c
-  write_d(0xff); // color C  was 1c
-  write_c(0xbe); // Set VcomH
-  write_d(0x1f); //
-  write_c(0xc1); // Set contrast current for A B C
-  write_d(0xCa); // Color A was AA
-  write_d(0xD4); // Color B was B4
-  write_d(0xF8); // Color C was C8
-  write_c(0xc7); // Set master contrast
-  write_d(0x0f); // no change
-  write_c(0xca); // Duty
-  write_d(0x7f); // 127+1
-  write_c(0xaf); // Display on
-}
-
-void Reset_SSD1339(void)
-{
-  RES(0);
-  delay_ms(100);
-  RES(1);
-}
-void write_c(unsigned char out_command)
-{
-  DC(0);CS(0);
-  //delay_ms(1);
-  
-  LCD_out(out_command);
- // delay_ms(1);
-  
-  DC(1);
-  // potentially add NOP
-  CS(1);
-
-}
-
-void write_d(unsigned char out_data)
-{
-  DC(1);CS(0);
- // delay_ms(1);
- 
-  LCD_out(out_data);
- // delay_ms(1);
-  DC(1);
-  //potentially add NOP
-  CS(1);
-}
-
-// these functions set / clear pins for LCD control lines.  they accecpt a 0 or 1 
-void DC(char stat)
-{
-	DDRB |= _BV(LCD_RS);				// RS is P0.30, set to output
-	if (stat)	
-		PORTB |= _BV(LCD_RS);
-	else
-		PORTB &= ~(_BV(LCD_RS));
-}
-
-void RES(char stat)
-{
-	DDRE |= _BV(LCD_RSTB);				// RSTB is P0.7, set to output
-	if (stat)	
-		PORTE |= _BV(LCD_RSTB);
-	else
-		PORTE &= ~(_BV(LCD_RSTB));
-}
-
-void CS(char stat)
-{
-	DDRB |= _BV(LCD_CS);				// RSTB is P0.7, set to output
-	if (stat)	
-		PORTB |= _BV(LCD_CS);
-	else
-		PORTB &= ~(_BV(LCD_CS));
-}
-
-// send to the LCD
-void LCD_out(unsigned char cmd)
-{
-	SPDR = cmd;
-    // could also do this via interrupts
-	loop_until_bit_is_set(SPSR, SPIF);
-}
-
-
 //**************************************Old shit below!*****************
 /*
-FontLookup - a lookup table for all characters
+  FontLookup - a lookup table for all characters
 */
 static const unsigned char FontLookup [][5] =
-{
+  {
     { 0x00, 0x00, 0x00, 0x00, 0x00 },  // sp
     { 0x00, 0x00, 0x5f, 0x00, 0x00 },   // !
     { 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
@@ -322,7 +154,7 @@
     { 0x41, 0x41, 0x36, 0x08, 0x00 },   // }
     { 0x02, 0x01, 0x01, 0x02, 0x01 },   // ~
     { 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // del
-};
+  };
 
 
 /**
@@ -338,33 +170,32 @@
  * Initializes the LCD. Must be called before any other
  * LCD functions.
  **/
-void lcd_init(void)
-{
-	LCDDDR |= (SCE | SDI | SCK);
-	LCDRESETDDR |= (RST|D_C);
+void lcd_init(void) {
+  LCDDDR |= (SCE | SDI | SCK);
+  LCDRESETDDR |= (RST|D_C);
 
-	LCDPORT &= ~( SCE | SDI | SCK);
-	LCDRESETPORT &=~(D_C);
+  LCDPORT &= ~( SCE | SDI | SCK);
+  LCDRESETPORT &=~(D_C);
   
-	SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
-	SPSR |= 0x01;       // a continuation of the above
+  SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
+  SPSR |= 0x01;       // a continuation of the above
 
-	LCDRESETPORT |= RST;
-	delay_ms(10);
-	LCDRESETPORT &= (~RST);
-	delay_ms(100);
-	LCDRESETPORT |= RST;
+  LCDRESETPORT |= RST;
+  delay_ms(10);
+  LCDRESETPORT &= (~RST);
+  delay_ms(100);
+  LCDRESETPORT |= RST;
 	
-    lcd_putbyte( 0x21 );  // LCD Extended Commands.
-    lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
-    lcd_putbyte( 0x06 );  // Set Temp coefficent.
-    lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
-    lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
-    lcd_putbyte( 0x0C );  // LCD in normal mode.
+  lcd_putbyte( 0x21 );  // LCD Extended Commands.
+  lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
+  lcd_putbyte( 0x06 );  // Set Temp coefficent.
+  lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
+  lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
+  lcd_putbyte( 0x0C );  // LCD in normal mode.
 	
-	LCDRESETPORT |= D_C;		//put it in init instead of main
+  LCDRESETPORT |= D_C;		//put it in init instead of main
 	
-	lcd_clear_screen();
+  lcd_clear_screen();
 }
 
 /**
@@ -373,11 +204,11 @@
  * @see lcd_init
  **/
 void lcd_clear_screen( void ) {
-	int i;
-	for (i = 0; i < 504; i++)
-			lcd_putbyte(0x0);
+  int i;
+  for (i = 0; i < 504; i++)
+    lcd_putbyte(0x0);
 	
-	lcd_gotoxy(0,0);
+  lcd_gotoxy(0,0);
 }
 
 /**
@@ -388,37 +219,35 @@
  *
  * @see lcd_init
  **/
-void lcd_putc(char c)
-{
-	int i;
+void lcd_putc(char c) {
+  int i;
 	
-	for (i = 0; i < 5; i++)
-		lcd_putbyte(FontLookup[c-32][i]);
-	lcd_putbyte(0);
+  for (i = 0; i < 5; i++)
+    lcd_putbyte(FontLookup[c-32][i]);
+  lcd_putbyte(0);
 }
 
 /*
-print an entire string to the lcd
+  print an entire string to the lcd
 */
-void lcd_puts(char *s)
-{
-	char *t = s;
-	while (*t != 0)
-	{
-		lcd_putc(*t);
-		t++;
-	}
+void lcd_puts(char *s) {
+  char *t = s;
+  while (*t != 0) {
+    lcd_putc(*t);
+    t++;
+  }
 }
 
 /*
-go to coordinate x, y
-y: vertically - 1 char
-x: horizontally - 1 pixel
+  go to coordinate x, y
+  y: vertically - 1 char
+  x: horizontally - 1 pixel
 
-multiply x by 6 if want to move 1 entire character
+  multiply x by 6 if want to move 1 entire character
 
-origin (0,0) is at top left corner of lcd screen
+  origin (0,0) is at top left corner of lcd screen
 */
+
 /**
  * Move the current cursor position to the one specified.
  * lcd_init must be called before this function may be used.
@@ -428,8 +257,7 @@
  *
  * @see lcd_init
  **/
-void lcd_gotoxy(int x, int y)
-{
+void lcd_gotoxy(int x, int y) {
   LCDRESETPORT &= ~(D_C);
   lcd_putbyte(0x40 | (y & 0x07));
   lcd_putbyte(0x80 | (x & 0x7f));
@@ -437,10 +265,11 @@
 }
 
 /*
-prints an int to the lcd
+  prints an int to the lcd
 
-code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
+  code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
 */
+
 /**
  * Print an integer to the LCD screen.
  * lcd_init must be called before this function may be used.
@@ -450,28 +279,30 @@
  * @see lcd_init
  **/
 void lcd_puti(int value ) {
-	unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; 
+  unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; 
 
-        /* convert int to ascii  */ 
-        if(value<0) { lcd_putc('-'); value=-value; }    
-        do { position--; *(lcd_data+position)=(value%radix)+'0'; value/=radix;  } while(value); 
+  /* convert int to ascii  */ 
+  if(value<0) { 
+    lcd_putc('-'); 
+    value=-value; 
+  }    
+  do { 
+    position--; 
+    *(lcd_data+position)=(value%radix)+'0'; 
+    value/=radix;  
+  } while(value); 
 
     
-        /* start displaying the number */
-        for(;position<=(sizeof(lcd_data)-1);position++)
-          {
-            
-            lcd_putc(lcd_data[position]);
-          }
+  /* start displaying the number */
+  for(;position<=(sizeof(lcd_data)-1);position++)
+    lcd_putc(lcd_data[position]);
 
-	return;
+  return;
 }
 
 /** @} **/ //end defgroup
 
-void lcd_putbyte(unsigned char b)
-{
-	SPDR = b;
-	while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */
+void lcd_putbyte(unsigned char b) {
+  SPDR = b;
+  while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */
 }
-
