![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mathematica-like environment on the mbed using USB keyboard input, VGA output, and a thermal printer.
Dependencies: mbed Thermal 4DGL-uLCD-SE USBHost_Modified uVGAIII
main.cpp@2:d97e71edb2b3, 2018-12-13 (annotated)
- Committer:
- zrussell3
- Date:
- Thu Dec 13 03:05:28 2018 +0000
- Revision:
- 2:d97e71edb2b3
- Parent:
- 0:824466ffa6da
- Child:
- 3:2b6951038d5b
Printer testing, uLCD connected
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zrussell3 | 0:824466ffa6da | 1 | #include "mbed.h" |
zrussell3 | 0:824466ffa6da | 2 | #include "Thermal.h" |
zrussell3 | 0:824466ffa6da | 3 | #include "USBHostKeyboard.h" |
zrussell3 | 0:824466ffa6da | 4 | #include "uVGAIII.h" |
zrussell3 | 0:824466ffa6da | 5 | |
zrussell3 | 2:d97e71edb2b3 | 6 | //#include "stdafx.h" |
zrussell3 | 2:d97e71edb2b3 | 7 | #include <iostream> |
zrussell3 | 2:d97e71edb2b3 | 8 | #include <cstdlib> |
zrussell3 | 2:d97e71edb2b3 | 9 | #include <ctime> |
zrussell3 | 2:d97e71edb2b3 | 10 | |
zrussell3 | 0:824466ffa6da | 11 | #define SIZE_X 480 |
zrussell3 | 0:824466ffa6da | 12 | #define SIZE_Y 800 |
zrussell3 | 0:824466ffa6da | 13 | |
zrussell3 | 0:824466ffa6da | 14 | // Debugging LEDs |
zrussell3 | 0:824466ffa6da | 15 | DigitalOut led(LED1); |
zrussell3 | 0:824466ffa6da | 16 | DigitalOut led2(LED2); |
zrussell3 | 0:824466ffa6da | 17 | DigitalOut led3(LED3); |
zrussell3 | 0:824466ffa6da | 18 | DigitalOut led4(LED4); |
zrussell3 | 0:824466ffa6da | 19 | |
zrussell3 | 0:824466ffa6da | 20 | uVGAIII ecran(p9,p10,p11); // serial tx, serial rx, reset pin; |
zrussell3 | 2:d97e71edb2b3 | 21 | uLCD_4DGL uLCD(p13,p14,p12); // serial tx, serial rx, reset pin; |
zrussell3 | 0:824466ffa6da | 22 | Thermal printer(p28, p27, 19200); |
zrussell3 | 0:824466ffa6da | 23 | |
zrussell3 | 0:824466ffa6da | 24 | int verticalCursor = 2; |
zrussell3 | 0:824466ffa6da | 25 | int horizontalCursor = 0; |
zrussell3 | 0:824466ffa6da | 26 | |
zrussell3 | 0:824466ffa6da | 27 | char currentLine[48]; |
zrussell3 | 0:824466ffa6da | 28 | int currentLineIndex = 0; |
zrussell3 | 0:824466ffa6da | 29 | |
zrussell3 | 2:d97e71edb2b3 | 30 | unsigned char bitmap[5000]; |
zrussell3 | 2:d97e71edb2b3 | 31 | char temp[8]; |
zrussell3 | 2:d97e71edb2b3 | 32 | |
zrussell3 | 2:d97e71edb2b3 | 33 | char getPixel(int x, int y, int bk) { |
zrussell3 | 2:d97e71edb2b3 | 34 | int r = ecran.read_pixel(x, y); |
zrussell3 | 2:d97e71edb2b3 | 35 | if (r == bk) //same as background |
zrussell3 | 2:d97e71edb2b3 | 36 | return '0'; |
zrussell3 | 2:d97e71edb2b3 | 37 | else |
zrussell3 | 2:d97e71edb2b3 | 38 | return '1'; |
zrussell3 | 2:d97e71edb2b3 | 39 | } |
zrussell3 | 2:d97e71edb2b3 | 40 | |
zrussell3 | 2:d97e71edb2b3 | 41 | void makeBitmap() { |
zrussell3 | 2:d97e71edb2b3 | 42 | int bk = ecran.read_pixel(799,479); |
zrussell3 | 2:d97e71edb2b3 | 43 | for (int i = 0; i < 32; ++i) { |
zrussell3 | 2:d97e71edb2b3 | 44 | for (int j = 0; j < 8; ++j) { //get next 8 bits and put them in temp array |
zrussell3 | 2:d97e71edb2b3 | 45 | temp[j] = getPixel(1+((i%2)*8+j),1+(i/2),bk); // wtf |
zrussell3 | 2:d97e71edb2b3 | 46 | //temp[j] = ecran.read_pixel(); |
zrussell3 | 2:d97e71edb2b3 | 47 | } |
zrussell3 | 2:d97e71edb2b3 | 48 | |
zrussell3 | 2:d97e71edb2b3 | 49 | //need to convert to 0b format |
zrussell3 | 2:d97e71edb2b3 | 50 | char * end; |
zrussell3 | 2:d97e71edb2b3 | 51 | long int value = strtol(temp, &end, 2); |
zrussell3 | 2:d97e71edb2b3 | 52 | bitmap[i] = value; |
zrussell3 | 2:d97e71edb2b3 | 53 | led3 = !led3; |
zrussell3 | 2:d97e71edb2b3 | 54 | } |
zrussell3 | 2:d97e71edb2b3 | 55 | printer.printBitmap(16,16,bitmap); |
zrussell3 | 2:d97e71edb2b3 | 56 | printer.feed(2); |
zrussell3 | 2:d97e71edb2b3 | 57 | } |
zrussell3 | 2:d97e71edb2b3 | 58 | |
zrussell3 | 2:d97e71edb2b3 | 59 | /* |
zrussell3 | 0:824466ffa6da | 60 | // hardcode plot x^2 |
zrussell3 | 0:824466ffa6da | 61 | void plot(void const *) |
zrussell3 | 0:824466ffa6da | 62 | { |
zrussell3 | 0:824466ffa6da | 63 | int xOffset = 600; |
zrussell3 | 0:824466ffa6da | 64 | int yOffset = 260; |
zrussell3 | 0:824466ffa6da | 65 | int pastX = 0; |
zrussell3 | 0:824466ffa6da | 66 | int pastY = 0; |
zrussell3 | 0:824466ffa6da | 67 | int currentX = 0; |
zrussell3 | 0:824466ffa6da | 68 | int currentY = 0; |
zrussell3 | 0:824466ffa6da | 69 | |
zrussell3 | 0:824466ffa6da | 70 | for(double i = -20; i < 20; i++) { |
zrussell3 | 0:824466ffa6da | 71 | //evaluate(); |
zrussell3 | 0:824466ffa6da | 72 | currentX = i*3 + xOffset; |
zrussell3 | 0:824466ffa6da | 73 | currentY = yOffset-(i*i); |
zrussell3 | 0:824466ffa6da | 74 | if(pastX == 0){ |
zrussell3 | 0:824466ffa6da | 75 | pastX = currentX; |
zrussell3 | 0:824466ffa6da | 76 | pastY = currentY; |
zrussell3 | 0:824466ffa6da | 77 | continue; |
zrussell3 | 0:824466ffa6da | 78 | } |
zrussell3 | 2:d97e71edb2b3 | 79 | ecran.line(pastX, pastY, currentX, currentY, RED); |
zrussell3 | 0:824466ffa6da | 80 | //ecran.put_pixel(i*2 + xOffset, yOffset-(i*i),RED); |
zrussell3 | 0:824466ffa6da | 81 | pastX = currentX; |
zrussell3 | 0:824466ffa6da | 82 | pastY = currentY; |
zrussell3 | 0:824466ffa6da | 83 | } |
zrussell3 | 0:824466ffa6da | 84 | while(1) { |
zrussell3 | 0:824466ffa6da | 85 | Thread::wait(200); |
zrussell3 | 0:824466ffa6da | 86 | } |
zrussell3 | 2:d97e71edb2b3 | 87 | }*/ |
zrussell3 | 2:d97e71edb2b3 | 88 | |
zrussell3 | 2:d97e71edb2b3 | 89 | |
zrussell3 | 0:824466ffa6da | 90 | |
zrussell3 | 0:824466ffa6da | 91 | void onKeyCode(uint8_t key, uint8_t modifier) { |
zrussell3 | 0:824466ffa6da | 92 | |
zrussell3 | 0:824466ffa6da | 93 | led3 = 1; |
zrussell3 | 0:824466ffa6da | 94 | |
zrussell3 | 0:824466ffa6da | 95 | if(key == 0) { |
zrussell3 | 0:824466ffa6da | 96 | led3 = 0; |
zrussell3 | 0:824466ffa6da | 97 | return; |
zrussell3 | 0:824466ffa6da | 98 | } |
zrussell3 | 0:824466ffa6da | 99 | |
zrussell3 | 0:824466ffa6da | 100 | // Handle newline |
zrussell3 | 0:824466ffa6da | 101 | if(key == 0x0A) { |
zrussell3 | 0:824466ffa6da | 102 | |
zrussell3 | 0:824466ffa6da | 103 | // Check if going off end of screen |
zrussell3 | 0:824466ffa6da | 104 | if(verticalCursor > 37) { |
zrussell3 | 0:824466ffa6da | 105 | ecran.filled_rectangle(0, 0 , 399, 480, DGREY); |
zrussell3 | 0:824466ffa6da | 106 | verticalCursor = 0; |
zrussell3 | 0:824466ffa6da | 107 | } else { |
zrussell3 | 0:824466ffa6da | 108 | // Move cursor to newline |
zrussell3 | 0:824466ffa6da | 109 | verticalCursor+=2; |
zrussell3 | 0:824466ffa6da | 110 | } |
zrussell3 | 0:824466ffa6da | 111 | |
zrussell3 | 0:824466ffa6da | 112 | horizontalCursor = 0; |
zrussell3 | 0:824466ffa6da | 113 | |
zrussell3 | 0:824466ffa6da | 114 | ecran.move_cursor(verticalCursor, horizontalCursor); |
zrussell3 | 0:824466ffa6da | 115 | |
zrussell3 | 0:824466ffa6da | 116 | ecran.puts(currentLine); |
zrussell3 | 2:d97e71edb2b3 | 117 | |
zrussell3 | 0:824466ffa6da | 118 | printer.printf(currentLine); |
zrussell3 | 0:824466ffa6da | 119 | printer.feed(2); |
zrussell3 | 0:824466ffa6da | 120 | |
zrussell3 | 0:824466ffa6da | 121 | for(size_t i = 0; i < 48; i++) { |
zrussell3 | 0:824466ffa6da | 122 | currentLine[i] = NULL; |
zrussell3 | 0:824466ffa6da | 123 | } |
zrussell3 | 0:824466ffa6da | 124 | |
zrussell3 | 0:824466ffa6da | 125 | currentLineIndex = 0; |
zrussell3 | 0:824466ffa6da | 126 | |
zrussell3 | 0:824466ffa6da | 127 | led3 = 0; |
zrussell3 | 0:824466ffa6da | 128 | return; |
zrussell3 | 0:824466ffa6da | 129 | } |
zrussell3 | 0:824466ffa6da | 130 | |
zrussell3 | 0:824466ffa6da | 131 | if(key == 0x08) |
zrussell3 | 0:824466ffa6da | 132 | { |
zrussell3 | 0:824466ffa6da | 133 | if(currentLineIndex != 0) |
zrussell3 | 0:824466ffa6da | 134 | { |
zrussell3 | 0:824466ffa6da | 135 | currentLineIndex--; |
zrussell3 | 0:824466ffa6da | 136 | currentLine[currentLineIndex] = NULL; |
zrussell3 | 0:824466ffa6da | 137 | } |
zrussell3 | 0:824466ffa6da | 138 | led3 = 0; |
zrussell3 | 0:824466ffa6da | 139 | return; |
zrussell3 | 0:824466ffa6da | 140 | } |
zrussell3 | 0:824466ffa6da | 141 | |
zrussell3 | 0:824466ffa6da | 142 | // Move the cursor 1 character over |
zrussell3 | 0:824466ffa6da | 143 | //ecran.move_cursor(verticalCursor, horizontalCursor++); |
zrussell3 | 0:824466ffa6da | 144 | |
zrussell3 | 0:824466ffa6da | 145 | // Append character to curret line string |
zrussell3 | 0:824466ffa6da | 146 | currentLine[currentLineIndex] = (char)key; |
zrussell3 | 0:824466ffa6da | 147 | |
zrussell3 | 0:824466ffa6da | 148 | if(currentLineIndex < 47) { |
zrussell3 | 0:824466ffa6da | 149 | currentLineIndex++; |
zrussell3 | 0:824466ffa6da | 150 | } |
zrussell3 | 0:824466ffa6da | 151 | |
zrussell3 | 0:824466ffa6da | 152 | led3 = 0; |
zrussell3 | 0:824466ffa6da | 153 | } |
zrussell3 | 0:824466ffa6da | 154 | |
zrussell3 | 0:824466ffa6da | 155 | void keyboard_task(void const *) { |
zrussell3 | 0:824466ffa6da | 156 | |
zrussell3 | 0:824466ffa6da | 157 | USBHostKeyboard keyboard; |
zrussell3 | 0:824466ffa6da | 158 | |
zrussell3 | 0:824466ffa6da | 159 | while(1) { |
zrussell3 | 0:824466ffa6da | 160 | // try to connect a USB keyboard |
zrussell3 | 0:824466ffa6da | 161 | while(!keyboard.connect()) { |
zrussell3 | 0:824466ffa6da | 162 | Thread::wait(1); |
zrussell3 | 0:824466ffa6da | 163 | } |
zrussell3 | 0:824466ffa6da | 164 | |
zrussell3 | 0:824466ffa6da | 165 | if(keyboard.connected()) led2 = 1; |
zrussell3 | 0:824466ffa6da | 166 | |
zrussell3 | 0:824466ffa6da | 167 | // When connected, attach handler called on keyboard event |
zrussell3 | 0:824466ffa6da | 168 | keyboard.attach(onKeyCode); |
zrussell3 | 0:824466ffa6da | 169 | |
zrussell3 | 0:824466ffa6da | 170 | // Wait until the keyboard is disconnected |
zrussell3 | 0:824466ffa6da | 171 | while(keyboard.connected()) |
zrussell3 | 0:824466ffa6da | 172 | Thread::wait(1); |
zrussell3 | 0:824466ffa6da | 173 | |
zrussell3 | 0:824466ffa6da | 174 | led2 = 0; |
zrussell3 | 0:824466ffa6da | 175 | } |
zrussell3 | 0:824466ffa6da | 176 | } |
zrussell3 | 0:824466ffa6da | 177 | |
zrussell3 | 2:d97e71edb2b3 | 178 | int main() { |
zrussell3 | 2:d97e71edb2b3 | 179 | led = 1; |
zrussell3 | 0:824466ffa6da | 180 | // Set up display |
zrussell3 | 0:824466ffa6da | 181 | ecran.baudrate(300000); |
zrussell3 | 0:824466ffa6da | 182 | ecran.screen_mode(LANDSCAPE); |
zrussell3 | 0:824466ffa6da | 183 | ecran.graphics_parameters(RESOLUTION, 2); |
zrussell3 | 0:824466ffa6da | 184 | ecran.touch_status(); |
zrussell3 | 2:d97e71edb2b3 | 185 | ecran.background_color(DGREY); |
zrussell3 | 0:824466ffa6da | 186 | ecran.cls(); |
zrussell3 | 0:824466ffa6da | 187 | |
zrussell3 | 0:824466ffa6da | 188 | ecran.move_cursor(0, 0); |
zrussell3 | 0:824466ffa6da | 189 | ecran.char_width('d'); |
zrussell3 | 0:824466ffa6da | 190 | ecran.char_height('d'); |
zrussell3 | 0:824466ffa6da | 191 | ecran.text_fgd_color(WHITE); |
zrussell3 | 0:824466ffa6da | 192 | ecran.text_bgd_color(DGREY); |
zrussell3 | 0:824466ffa6da | 193 | ecran.puts("Booting up..."); |
zrussell3 | 0:824466ffa6da | 194 | |
zrussell3 | 0:824466ffa6da | 195 | ecran.line(400, 0 , 400, 480, RED); |
zrussell3 | 2:d97e71edb2b3 | 196 | ecran.filled_rectangle(400,0,405,480,RED); |
zrussell3 | 0:824466ffa6da | 197 | |
zrussell3 | 0:824466ffa6da | 198 | // Use old style threading |
zrussell3 | 0:824466ffa6da | 199 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
zrussell3 | 0:824466ffa6da | 200 | |
zrussell3 | 0:824466ffa6da | 201 | ecran.move_cursor(2, 0); |
zrussell3 | 0:824466ffa6da | 202 | ecran.puts("Ready!"); |
zrussell3 | 2:d97e71edb2b3 | 203 | //int pix = ecran.read_pixel(400, 0); |
zrussell3 | 2:d97e71edb2b3 | 204 | //printer.printf((char *)pix); |
zrussell3 | 2:d97e71edb2b3 | 205 | makeBitmap(); |
zrussell3 | 2:d97e71edb2b3 | 206 | led3 = 1; |
zrussell3 | 0:824466ffa6da | 207 | |
zrussell3 | 0:824466ffa6da | 208 | //printer.printf("Printing from main"); |
zrussell3 | 0:824466ffa6da | 209 | //printer.feed(2); |
zrussell3 | 0:824466ffa6da | 210 | //printer.test(); |
zrussell3 | 0:824466ffa6da | 211 | |
zrussell3 | 0:824466ffa6da | 212 | // Clear currentLine array before using |
zrussell3 | 0:824466ffa6da | 213 | for(size_t i = 0; i < 48; i++) { |
zrussell3 | 0:824466ffa6da | 214 | currentLine[i] = NULL; |
zrussell3 | 0:824466ffa6da | 215 | } |
zrussell3 | 0:824466ffa6da | 216 | |
zrussell3 | 0:824466ffa6da | 217 | //Thread drawPlot(plot, NULL, osPriorityNormal, 256*4); |
zrussell3 | 0:824466ffa6da | 218 | |
zrussell3 | 2:d97e71edb2b3 | 219 | //int pixel = ecran.read_pixel(200, 200); |
zrussell3 | 2:d97e71edb2b3 | 220 | //char arr[10]; |
zrussell3 | 2:d97e71edb2b3 | 221 | //itoa(pixel, arr, 2); |
zrussell3 | 2:d97e71edb2b3 | 222 | //ecran.put_string(pixel); |
zrussell3 | 2:d97e71edb2b3 | 223 | //ecran.put_string(arr); |
zrussell3 | 2:d97e71edb2b3 | 224 | |
zrussell3 | 2:d97e71edb2b3 | 225 | // char * arr = |
zrussell3 | 2:d97e71edb2b3 | 226 | // reinterpret_cast <char *>(&pixel); |
zrussell3 | 2:d97e71edb2b3 | 227 | |
zrussell3 | 2:d97e71edb2b3 | 228 | // printer.puts(arr); |
zrussell3 | 2:d97e71edb2b3 | 229 | |
zrussell3 | 0:824466ffa6da | 230 | while(1) { |
zrussell3 | 0:824466ffa6da | 231 | led=!led; |
zrussell3 | 0:824466ffa6da | 232 | Thread::wait(500); // Wait .5s in main thread |
zrussell3 | 0:824466ffa6da | 233 | } |
zrussell3 | 0:824466ffa6da | 234 | } |