1 | #include "ArduinoRobot.h"
|
---|
2 | #include "Wire.h"
|
---|
3 |
|
---|
4 | #define BUFFPIXEL 20
|
---|
5 |
|
---|
6 | bool cmp(char* str1, char* str2, uint8_t len);
|
---|
7 | uint16_t read16(Fat16& f);
|
---|
8 | uint32_t read32(Fat16& f);
|
---|
9 | //uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
|
---|
10 |
|
---|
11 | void RobotControl::beginTFT(uint16_t foreGround, uint16_t backGround){
|
---|
12 | //TFT initialization
|
---|
13 | Arduino_LCD::initB();
|
---|
14 | Arduino_LCD::fillScreen(backGround);
|
---|
15 | Arduino_LCD::setTextColor(foreGround);
|
---|
16 | Arduino_LCD::setTextSize(1);
|
---|
17 | this->foreGround=foreGround;
|
---|
18 | this->backGround=backGround;
|
---|
19 | }
|
---|
20 | void RobotControl::_enableLCD(){
|
---|
21 | DDRB = DDRB & 0xEF; //pinMode(CS_SD,INPUT);
|
---|
22 | DDRB = DDRB | 0x20; //pinMode(CS_LCD,OUTPUT);
|
---|
23 | }
|
---|
24 | /*void RobotControl::_setErase(uint8_t posX, uint8_t posY){
|
---|
25 | Arduino_LCD::setCursor(posX,posY);
|
---|
26 | Arduino_LCD::setTextColor(backGround);
|
---|
27 | Arduino_LCD::setTextSize(1);
|
---|
28 | }
|
---|
29 | void RobotControl::_setWrite(uint8_t posX, uint8_t posY){
|
---|
30 | Arduino_LCD::setCursor(posX,posY);
|
---|
31 | Arduino_LCD::setTextColor(foreGround);
|
---|
32 | Arduino_LCD::setTextSize(1);
|
---|
33 | }*/
|
---|
34 | /*
|
---|
35 | void RobotControl::text(int value, uint8_t posX, uint8_t posY, bool EW){
|
---|
36 | if(EW)
|
---|
37 | _setWrite(posX,posY);
|
---|
38 | else
|
---|
39 | _setErase(posX,posY);
|
---|
40 | Arduino_LCD::print(value);
|
---|
41 | }
|
---|
42 | void RobotControl::text(long value, uint8_t posX, uint8_t posY, bool EW){
|
---|
43 | if(EW)
|
---|
44 | _setWrite(posX,posY);
|
---|
45 | else
|
---|
46 | _setErase(posX,posY);
|
---|
47 | Arduino_LCD::print(value);
|
---|
48 | }
|
---|
49 | void RobotControl::text(char* value, uint8_t posX, uint8_t posY, bool EW){
|
---|
50 | if(EW)
|
---|
51 | _setWrite(posX,posY);
|
---|
52 | else
|
---|
53 | _setErase(posX,posY);
|
---|
54 | Arduino_LCD::print(value);
|
---|
55 | }
|
---|
56 | void RobotControl::text(char value, uint8_t posX, uint8_t posY, bool EW){
|
---|
57 | if(EW)
|
---|
58 | _setWrite(posX,posY);
|
---|
59 | else
|
---|
60 | _setErase(posX,posY);
|
---|
61 | Arduino_LCD::print(value);
|
---|
62 | }
|
---|
63 | */
|
---|
64 |
|
---|
65 | void RobotControl::debugPrint(long value, uint8_t x, uint8_t y){
|
---|
66 | static long oldVal=0;
|
---|
67 | Arduino_LCD::stroke(backGround);
|
---|
68 | text(oldVal,x,y);
|
---|
69 | Arduino_LCD::stroke(foreGround);
|
---|
70 | text(value,x,y);
|
---|
71 | oldVal=value;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void RobotControl::clearScreen(){
|
---|
75 | Arduino_LCD::fillScreen(backGround);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void RobotControl::drawBMP(char* filename, uint8_t x, uint8_t y){
|
---|
79 | /*for(int j=0;j<NUM_EEPROM_BMP;j++){
|
---|
80 | Serial.println(_eeprom_bmp[j].name);
|
---|
81 | Serial.print(" ");
|
---|
82 | Serial.print(_eeprom_bmp[j].address);
|
---|
83 | Serial.print(" ");
|
---|
84 | Serial.print(_eeprom_bmp[j].width);
|
---|
85 | Serial.print(" ");
|
---|
86 | Serial.println(_eeprom_bmp[j].height);
|
---|
87 | }
|
---|
88 | Serial.println();*/
|
---|
89 | if(_isEEPROM_BMP_Allocated){
|
---|
90 | for(int i=0;i<NUM_EEPROM_BMP;i++){
|
---|
91 | if(cmp(_eeprom_bmp[i].name,filename,7)){
|
---|
92 | /*Serial.println(_eeprom_bmp[i].name);
|
---|
93 | Serial.print(" ");
|
---|
94 | Serial.print(_eeprom_bmp[i].address);
|
---|
95 | Serial.print(" ");
|
---|
96 | Serial.print(_eeprom_bmp[i].width);
|
---|
97 | Serial.print(" ");
|
---|
98 | Serial.println(_eeprom_bmp[i].height);*/
|
---|
99 | _drawBMP(_eeprom_bmp[i].address,x,y,_eeprom_bmp[i].width,_eeprom_bmp[i].height);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }else{
|
---|
104 | _drawBMP(filename,x,y);//goes to SD
|
---|
105 | }
|
---|
106 | }
|
---|
107 | bool cmp(char* str1, char* str2, uint8_t len){
|
---|
108 | for(uint8_t i=0;i<len;i++){
|
---|
109 | if(str1[i]==' ')break;
|
---|
110 | if(str1[i]!=str2[i])return false;
|
---|
111 | }
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void RobotControl::_drawBMP(uint32_t iconOffset, uint8_t x, uint8_t y, uint8_t width, uint8_t height){
|
---|
116 | uint8_t screenWidth=Arduino_LCD::width();
|
---|
117 | uint8_t screenHeight=Arduino_LCD::height();
|
---|
118 | if((x >= screenWidth) || (y >= screenHeight)) return;
|
---|
119 |
|
---|
120 | // Crop area to be loaded
|
---|
121 | if((x+width-1) >= screenWidth) width = screenWidth - x;
|
---|
122 | if((y+height-1) >= screenHeight) height = screenHeight - y;
|
---|
123 |
|
---|
124 | // Set TFT address window to clipped image bounds
|
---|
125 | Arduino_LCD::setAddrWindow(x, y, x+width-1, y+height-1);
|
---|
126 |
|
---|
127 | // launch the reading command
|
---|
128 | _drawBMP_EEPROM(iconOffset, width, height);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // Draw BMP from SD card through the filename
|
---|
132 | void RobotControl::_drawBMP(char* filename, uint8_t posX, uint8_t posY){
|
---|
133 | uint8_t bmpWidth, bmpHeight; // W+H in pixels
|
---|
134 | uint8_t bmpDepth; // Bit depth (currently must be 24)
|
---|
135 | uint32_t bmpImageoffset; // Start of image data in file
|
---|
136 | uint32_t rowSize; // Not always = bmpWidth; may have padding
|
---|
137 | uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
|
---|
138 | uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
|
---|
139 | bool goodBmp = false; // Set to true on valid header parse
|
---|
140 | bool flip = true; // BMP is stored bottom-to-top
|
---|
141 | uint8_t w, h, row, col;
|
---|
142 | uint8_t r, g, b;
|
---|
143 | uint32_t pos = 0;
|
---|
144 |
|
---|
145 | // Open requested file on SD card
|
---|
146 | if ((file.open(filename,O_READ)) == NULL) {
|
---|
147 | return;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Parse BMP header
|
---|
151 | if(read16(file) == 0x4D42) { // BMP signature
|
---|
152 | read32(file);//uint32_t aux = read32(file);
|
---|
153 | (void)read32(file); // Read & ignore creator bytes
|
---|
154 | bmpImageoffset = read32(file); // Start of image data
|
---|
155 |
|
---|
156 | // Read DIB header
|
---|
157 | (void)read32(file);//aux = read32(file);
|
---|
158 | bmpWidth = read32(file);
|
---|
159 | bmpHeight = read32(file);
|
---|
160 |
|
---|
161 | if(read16(file) == 1) { // # planes -- must be '1'
|
---|
162 | bmpDepth = read16(file); // bits per pixel
|
---|
163 | if((bmpDepth == 24) && (read32(file) == 0)) { // 0 = uncompressed
|
---|
164 | goodBmp = true; // Supported BMP format -- proceed!
|
---|
165 |
|
---|
166 | // BMP rows are padded (if needed) to 4-byte boundary
|
---|
167 | rowSize = (bmpWidth * 3 + 3) & ~3;
|
---|
168 |
|
---|
169 | // If bmpHeight is negative, image is in top-down order.
|
---|
170 | // This is not canon but has been observed in the wild.
|
---|
171 | if(bmpHeight < 0) {
|
---|
172 | bmpHeight = -bmpHeight;
|
---|
173 | flip = false;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Crop area to be loaded
|
---|
177 | w = bmpWidth;
|
---|
178 | h = bmpHeight;
|
---|
179 |
|
---|
180 | // Start drawing
|
---|
181 | //_enableLCD();
|
---|
182 | Arduino_LCD::setAddrWindow(posX, posY, posX+bmpWidth-1, posY+bmpHeight-1);
|
---|
183 |
|
---|
184 | for (row=0; row<h; row++) { // For each scanline...
|
---|
185 | if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
---|
186 | pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
|
---|
187 | else // Bitmap is stored top-to-bottom
|
---|
188 | pos = bmpImageoffset + row * rowSize;
|
---|
189 |
|
---|
190 | if(file.curPosition() != pos) { // Need seek?
|
---|
191 | //_enableSD();
|
---|
192 | file.seekSet(pos);
|
---|
193 | buffidx = sizeof(sdbuffer); // Force buffer reload
|
---|
194 | //_enableLCD();
|
---|
195 | }
|
---|
196 | for (col=0; col<w; col++) { // For each pixel...
|
---|
197 | // Time to read more pixel data?
|
---|
198 | if (buffidx >= sizeof(sdbuffer)) { // Indeed
|
---|
199 | //_enableSD();
|
---|
200 | file.read(sdbuffer, sizeof(sdbuffer));
|
---|
201 | buffidx = 0; // Set index to beginning
|
---|
202 | //_enableLCD();
|
---|
203 | }
|
---|
204 | // Convert pixel from BMP to TFT format, push to display
|
---|
205 | b = sdbuffer[buffidx++];
|
---|
206 | g = sdbuffer[buffidx++];
|
---|
207 | r = sdbuffer[buffidx++];
|
---|
208 |
|
---|
209 | int color = Arduino_LCD::Color565(r,g,b);
|
---|
210 |
|
---|
211 | Arduino_LCD::pushColor(color);
|
---|
212 | } // end pixel
|
---|
213 | } // end scanline
|
---|
214 | //_enableSD();
|
---|
215 | } // end goodBmp*/
|
---|
216 | }
|
---|
217 | }
|
---|
218 | file.close();
|
---|
219 | //_enableLCD();
|
---|
220 | }
|
---|
221 | uint16_t read16(Fat16& f) {
|
---|
222 | uint16_t result;
|
---|
223 | f.read(&result,sizeof(result));
|
---|
224 | return result;
|
---|
225 | }
|
---|
226 | uint32_t read32(Fat16& f) {
|
---|
227 | uint32_t result;
|
---|
228 | f.read(&result,sizeof(result));
|
---|
229 | return result;
|
---|
230 | }
|
---|
231 | /*
|
---|
232 | uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
|
---|
233 | return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
|
---|
234 | }*/
|
---|
235 |
|
---|
236 |
|
---|
237 | void RobotControl::_drawBMP_EEPROM(uint16_t address, uint8_t width, uint8_t height){
|
---|
238 | uint16_t u16retVal = 0;
|
---|
239 | EEPROM_I2C::_beginTransmission(address);
|
---|
240 | EEPROM_I2C::_endTransmission();
|
---|
241 | /*Wire.beginTransmission(DEVICEADDRESS);
|
---|
242 | Wire.write( (address >> 8) & 0xFF );
|
---|
243 | Wire.write( (address >> 0) & 0xFF );
|
---|
244 | Wire.endTransmission();*/
|
---|
245 |
|
---|
246 | long s = width * height ;
|
---|
247 | for(long j = 0; j < (long) s >> 4; j++) { // divided by 32, times 2
|
---|
248 | Wire.requestFrom(DEVICEADDRESS, 32);
|
---|
249 | for(int i = 0; i < 32; i+=2) {
|
---|
250 | u16retVal = Wire.read();
|
---|
251 | u16retVal = (u16retVal << 8) + Wire.read();
|
---|
252 | Arduino_LCD::pushColor(u16retVal);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|
257 | void RobotControl::beginBMPFromEEPROM(){
|
---|
258 | _eeprom_bmp=(EEPROM_BMP*)malloc(NUM_EEPROM_BMP*sizeof(EEPROM_BMP));
|
---|
259 | EEPROM_I2C::_beginTransmission(0);
|
---|
260 | EEPROM_I2C::_endTransmission();
|
---|
261 |
|
---|
262 | for(uint8_t j=0;j<NUM_EEPROM_BMP;j++){
|
---|
263 | Wire.requestFrom(DEVICEADDRESS, sizeof(EEPROM_BMP));
|
---|
264 | for(uint8_t i=0;i<8;i++){
|
---|
265 | _eeprom_bmp[j].name[i]=Wire.read();//name
|
---|
266 | }
|
---|
267 | _eeprom_bmp[j].width=Wire.read();//width
|
---|
268 | _eeprom_bmp[j].height=Wire.read();//height
|
---|
269 |
|
---|
270 | _eeprom_bmp[j].address=Wire.read();
|
---|
271 | _eeprom_bmp[j].address=_eeprom_bmp[j].address + (Wire.read() << 8);//address
|
---|
272 | }
|
---|
273 | _isEEPROM_BMP_Allocated=true;
|
---|
274 |
|
---|
275 | }
|
---|
276 | void RobotControl::endBMPFromEEPROM(){
|
---|
277 | free(_eeprom_bmp);
|
---|
278 | _isEEPROM_BMP_Allocated=false;
|
---|
279 | }
|
---|