Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.

Dependencies:   Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo

Committer:
shimniok
Date:
Wed Jun 20 14:57:48 2012 +0000
Revision:
0:826c6171fc1b
Updated documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:826c6171fc1b 1 #include "SerialGraphicLCD.h"
shimniok 0:826c6171fc1b 2
shimniok 0:826c6171fc1b 3 #define XSIZE 6
shimniok 0:826c6171fc1b 4 #define YSIZE 9
shimniok 0:826c6171fc1b 5
shimniok 0:826c6171fc1b 6 SerialGraphicLCD::SerialGraphicLCD(PinName tx, PinName rx):
shimniok 0:826c6171fc1b 7 Serial(tx, rx), _firmware(SFE_FW)
shimniok 0:826c6171fc1b 8 {
shimniok 0:826c6171fc1b 9 baud(115200); // default baud rate
shimniok 0:826c6171fc1b 10 resolution(LCD_128x64); // default resolution
shimniok 0:826c6171fc1b 11 }
shimniok 0:826c6171fc1b 12
shimniok 0:826c6171fc1b 13 SerialGraphicLCD::SerialGraphicLCD(PinName tx, PinName rx, int firmware):
shimniok 0:826c6171fc1b 14 Serial(tx, rx), _firmware(firmware)
shimniok 0:826c6171fc1b 15 {
shimniok 0:826c6171fc1b 16 baud(115200); // default baud rate
shimniok 0:826c6171fc1b 17 resolution(LCD_128x64); // default resolution
shimniok 0:826c6171fc1b 18 }
shimniok 0:826c6171fc1b 19
shimniok 0:826c6171fc1b 20 void SerialGraphicLCD::clear() {
shimniok 0:826c6171fc1b 21 putc(0x7c);
shimniok 0:826c6171fc1b 22 putc(0x00);
shimniok 0:826c6171fc1b 23 }
shimniok 0:826c6171fc1b 24
shimniok 0:826c6171fc1b 25 void SerialGraphicLCD::pos(int col, int row) {
shimniok 0:826c6171fc1b 26 if (_firmware == SD_FW)
shimniok 0:826c6171fc1b 27 posXY(XSIZE*col, (YSIZE*row));
shimniok 0:826c6171fc1b 28 else if (_firmware == SFE_FW)
shimniok 0:826c6171fc1b 29 posXY(XSIZE*col, _yMax-(YSIZE*row)-1);
shimniok 0:826c6171fc1b 30 }
shimniok 0:826c6171fc1b 31
shimniok 0:826c6171fc1b 32 void SerialGraphicLCD::posXY(int x, int y) {
shimniok 0:826c6171fc1b 33 putc(0x7c);
shimniok 0:826c6171fc1b 34 putc(0x18);
shimniok 0:826c6171fc1b 35 putc(x);
shimniok 0:826c6171fc1b 36 putc(0x7c);
shimniok 0:826c6171fc1b 37 putc(0x19);
shimniok 0:826c6171fc1b 38 putc(y);
shimniok 0:826c6171fc1b 39 }
shimniok 0:826c6171fc1b 40
shimniok 0:826c6171fc1b 41 void SerialGraphicLCD::pixel(int x, int y, bool set) {
shimniok 0:826c6171fc1b 42 putc(0x7c);
shimniok 0:826c6171fc1b 43 putc(0x10);
shimniok 0:826c6171fc1b 44 putc(x);
shimniok 0:826c6171fc1b 45 putc(y);
shimniok 0:826c6171fc1b 46 putc((set) ? 0x01 : 0x00);
shimniok 0:826c6171fc1b 47 }
shimniok 0:826c6171fc1b 48
shimniok 0:826c6171fc1b 49 void SerialGraphicLCD::line(int x1, int y1, int x2, int y2, bool set) {
shimniok 0:826c6171fc1b 50 putc(0x7c);
shimniok 0:826c6171fc1b 51 putc(0x0c);
shimniok 0:826c6171fc1b 52 putc(x1);
shimniok 0:826c6171fc1b 53 putc(y1);
shimniok 0:826c6171fc1b 54 putc(x2);
shimniok 0:826c6171fc1b 55 putc(y2);
shimniok 0:826c6171fc1b 56 putc((set) ? 0x01 : 0x00);
shimniok 0:826c6171fc1b 57 }
shimniok 0:826c6171fc1b 58
shimniok 0:826c6171fc1b 59 void SerialGraphicLCD::circle(int x, int y, int r, bool set) {
shimniok 0:826c6171fc1b 60 putc(0x7c);
shimniok 0:826c6171fc1b 61 putc(0x03);
shimniok 0:826c6171fc1b 62 putc(x);
shimniok 0:826c6171fc1b 63 putc(y);
shimniok 0:826c6171fc1b 64 putc(r);
shimniok 0:826c6171fc1b 65 putc((set) ? 0x01 : 0x00);
shimniok 0:826c6171fc1b 66 }
shimniok 0:826c6171fc1b 67
shimniok 0:826c6171fc1b 68 // Unfortunately, the datasheet for the stock firmware is incorrect;
shimniok 0:826c6171fc1b 69 // the box command does not take a 5th parameter for draw/erase like the others
shimniok 0:826c6171fc1b 70 // However, it does in the sd firmware
shimniok 0:826c6171fc1b 71 void SerialGraphicLCD::rect(int x1, int y1, int x2, int y2) {
shimniok 0:826c6171fc1b 72 putc(0x7c);
shimniok 0:826c6171fc1b 73 putc(0x0f);
shimniok 0:826c6171fc1b 74 putc(x1);
shimniok 0:826c6171fc1b 75 putc(y1);
shimniok 0:826c6171fc1b 76 putc(x2);
shimniok 0:826c6171fc1b 77 putc(y2);
shimniok 0:826c6171fc1b 78 if (_firmware == SD_FW)
shimniok 0:826c6171fc1b 79 putc(0x01);
shimniok 0:826c6171fc1b 80 }
shimniok 0:826c6171fc1b 81
shimniok 0:826c6171fc1b 82 void SerialGraphicLCD::rect(int x1, int y1, int x2, int y2, bool set) {
shimniok 0:826c6171fc1b 83 putc(0x7c);
shimniok 0:826c6171fc1b 84 putc(0x0f);
shimniok 0:826c6171fc1b 85 putc(x1);
shimniok 0:826c6171fc1b 86 putc(y1);
shimniok 0:826c6171fc1b 87 putc(x2);
shimniok 0:826c6171fc1b 88 putc(y2);
shimniok 0:826c6171fc1b 89 if (_firmware == SD_FW)
shimniok 0:826c6171fc1b 90 putc((set) ? 0x01 : 0x00);
shimniok 0:826c6171fc1b 91 }
shimniok 0:826c6171fc1b 92
shimniok 0:826c6171fc1b 93 void SerialGraphicLCD::rectFill(int x1, int y1, int x2, int y2, char fillByte) {
shimniok 0:826c6171fc1b 94 if (_firmware == SD_FW) {
shimniok 0:826c6171fc1b 95
shimniok 0:826c6171fc1b 96 // Bugs in firmware; if y2-y1 == 2, nothing drawn; if y2-y1 == 3, fill is 4 tall
shimniok 0:826c6171fc1b 97 // if ((y2 - y1) > 3) {
shimniok 0:826c6171fc1b 98 putc(0x7c);
shimniok 0:826c6171fc1b 99 putc(0x12);
shimniok 0:826c6171fc1b 100 putc(x1);
shimniok 0:826c6171fc1b 101 putc(y1);
shimniok 0:826c6171fc1b 102 putc(x2+1); // bug in firmware, off-by-one on x2
shimniok 0:826c6171fc1b 103 putc(y2);
shimniok 0:826c6171fc1b 104 putc(fillByte);
shimniok 0:826c6171fc1b 105 // } else {
shimniok 0:826c6171fc1b 106 // for (int y=y1; y <= y2; y++)
shimniok 0:826c6171fc1b 107 // line(x1, y, x2, y, fillByte == FILL);
shimniok 0:826c6171fc1b 108 // }
shimniok 0:826c6171fc1b 109 }
shimniok 0:826c6171fc1b 110 }
shimniok 0:826c6171fc1b 111
shimniok 0:826c6171fc1b 112 void SerialGraphicLCD::erase(int x1, int y1, int x2, int y2) {
shimniok 0:826c6171fc1b 113 putc(0x7c);
shimniok 0:826c6171fc1b 114 putc(0x05);
shimniok 0:826c6171fc1b 115 putc(x1);
shimniok 0:826c6171fc1b 116 putc(y1);
shimniok 0:826c6171fc1b 117 putc(x2);
shimniok 0:826c6171fc1b 118 putc(y2);
shimniok 0:826c6171fc1b 119 }
shimniok 0:826c6171fc1b 120
shimniok 0:826c6171fc1b 121 void SerialGraphicLCD::backlight(int i) {
shimniok 0:826c6171fc1b 122 if (i >= 0 && i <= 100) {
shimniok 0:826c6171fc1b 123 putc(0x7c);
shimniok 0:826c6171fc1b 124 putc(0x02);
shimniok 0:826c6171fc1b 125 putc(i);
shimniok 0:826c6171fc1b 126 }
shimniok 0:826c6171fc1b 127 }
shimniok 0:826c6171fc1b 128
shimniok 0:826c6171fc1b 129 void SerialGraphicLCD::reverseMode() {
shimniok 0:826c6171fc1b 130 putc(0x7c);
shimniok 0:826c6171fc1b 131 putc(0x12);
shimniok 0:826c6171fc1b 132 }
shimniok 0:826c6171fc1b 133
shimniok 0:826c6171fc1b 134 void SerialGraphicLCD::resolution(int type) {
shimniok 0:826c6171fc1b 135 switch (type) {
shimniok 0:826c6171fc1b 136 case LCD_128x64 :
shimniok 0:826c6171fc1b 137 resolution(128, 64);
shimniok 0:826c6171fc1b 138 break;
shimniok 0:826c6171fc1b 139 case LCD_160x128 :
shimniok 0:826c6171fc1b 140 resolution(160, 128);
shimniok 0:826c6171fc1b 141 break;
shimniok 0:826c6171fc1b 142 }
shimniok 0:826c6171fc1b 143 }
shimniok 0:826c6171fc1b 144
shimniok 0:826c6171fc1b 145 void SerialGraphicLCD::resolution(int x, int y) {
shimniok 0:826c6171fc1b 146 _xMax = x;
shimniok 0:826c6171fc1b 147 _yMax = y;
shimniok 0:826c6171fc1b 148 }
shimniok 0:826c6171fc1b 149
shimniok 0:826c6171fc1b 150
shimniok 0:826c6171fc1b 151 void SerialGraphicLCD::lcdbaud(int b) {
shimniok 0:826c6171fc1b 152 if (b > 0 && b < 7) {
shimniok 0:826c6171fc1b 153 putc(0x7c);
shimniok 0:826c6171fc1b 154 putc(0x07);
shimniok 0:826c6171fc1b 155 putc(b+'0');
shimniok 0:826c6171fc1b 156 }
shimniok 0:826c6171fc1b 157 }