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
Diff: main.cpp
- Revision:
- 3:2b6951038d5b
- Parent:
- 2:d97e71edb2b3
- Child:
- 4:b79d152dec32
--- a/main.cpp Thu Dec 13 03:05:28 2018 +0000 +++ b/main.cpp Thu Dec 13 07:49:27 2018 +0000 @@ -1,12 +1,10 @@ #include "mbed.h" #include "Thermal.h" +#include "uLCD_4DGL.h" #include "USBHostKeyboard.h" #include "uVGAIII.h" -//#include "stdafx.h" -#include <iostream> -#include <cstdlib> -#include <ctime> +#include <math.h> #define SIZE_X 480 #define SIZE_Y 800 @@ -21,17 +19,27 @@ uLCD_4DGL uLCD(p13,p14,p12); // serial tx, serial rx, reset pin; Thermal printer(p28, p27, 19200); +// Variables to track cursor location on VGA int verticalCursor = 2; int horizontalCursor = 0; +// Variables to hold user input & index char currentLine[48]; int currentLineIndex = 0; +// Variables to track graph bitmaps unsigned char bitmap[5000]; char temp[8]; +// Mutexes for displays +Mutex VGA_Lock; +Mutex LCD_Lock; + char getPixel(int x, int y, int bk) { - int r = ecran.read_pixel(x, y); + LCD_Lock.lock(); + int r = uLCD.read_pixel(x, y); + LCD_Lock.unlock(); + if (r == bk) //same as background return '0'; else @@ -39,37 +47,106 @@ } void makeBitmap() { - int bk = ecran.read_pixel(799,479); - for (int i = 0; i < 32; ++i) { + LCD_Lock.lock(); + int bk = uLCD.read_pixel(127,127); + LCD_Lock.unlock(); + for (int i = 0; i < 128*128/8; ++i) { for (int j = 0; j < 8; ++j) { //get next 8 bits and put them in temp array - temp[j] = getPixel(1+((i%2)*8+j),1+(i/2),bk); // wtf - //temp[j] = ecran.read_pixel(); + //temp[2*j] = getPixel((i%32)*4+j,i/32,bk); + //temp[2*j+1] = temp[2*j]; + temp[j] = getPixel((i%16)*8+j,i/16,bk); } //need to convert to 0b format char * end; long int value = strtol(temp, &end, 2); + //bitmap[64*(i/32)+(i%32)] = value; + //bitmap[64*(i/32)+(i%32)+32] = value; bitmap[i] = value; led3 = !led3; } - printer.printBitmap(16,16,bitmap); + //printer.printf(bitmap); + //printer.justify('C'); + printer.printBitmap(128,128,bitmap); printer.feed(2); } -/* + // hardcode plot x^2 -void plot(void const *) +void plotx2() +{ + LCD_Lock.lock(); + uLCD.cls(); + int xOffset = 0; + int yOffset = 127; + int pastX = 0; + int pastY = 127; + int currentX = 0; + int currentY = 0; + + for(double i = 0; i < 11; i++) { + //evaluate(); + currentX = i*10 + xOffset; + currentY = yOffset-(i*i); + if(pastX == 0){ + pastX = currentX; + pastY = currentY; + continue; + } + uLCD.line(pastX, pastY, currentX, currentY, RED); + uLCD.line(pastX, pastY+1, currentX, currentY+1, RED); + uLCD.line(pastX, pastY-1, currentX, currentY-1, RED); + pastX = currentX; + pastY = currentY; + } + LCD_Lock.unlock(); +} + +void plotsin() +{ + LCD_Lock.lock(); + uLCD.cls(); + int xOffset = 0; + int yOffset = 64; + int pastX = 0; + int pastY = 64; + int currentX = 0; + int currentY = 0; + + for(double i = 0; i < 7; i+=.01) { + //evaluate(); + currentX = i*16 + xOffset; + currentY = yOffset-40*sin(i); + if(pastX == 0){ + pastX = currentX; + pastY = currentY; + continue; + } + uLCD.line(pastX, pastY, currentX, currentY, RED); + uLCD.line(pastX, pastY+1, currentX, currentY+1, RED); + uLCD.line(pastX, pastY-1, currentX, currentY-1, RED); + pastX = currentX; + pastY = currentY; + } + LCD_Lock.unlock(); +} + + +void plotx2vga(void const *) { int xOffset = 600; - int yOffset = 260; + int yOffset = 401; int pastX = 0; int pastY = 0; int currentX = 0; int currentY = 0; - for(double i = -20; i < 20; i++) { - //evaluate(); - currentX = i*3 + xOffset; + VGA_Lock.lock(); + + for(double i = -20; i < 21; i++) { + led4 = !led4; + //evaluate(); // Where evaluate would go...if we could parse input + currentX = i*9 + xOffset; currentY = yOffset-(i*i); if(pastX == 0){ pastX = currentX; @@ -77,26 +154,31 @@ continue; } ecran.line(pastX, pastY, currentX, currentY, RED); - //ecran.put_pixel(i*2 + xOffset, yOffset-(i*i),RED); pastX = currentX; pastY = currentY; } + + VGA_Lock.unlock(); + while(1) { - Thread::wait(200); + Thread::wait(500); } -}*/ - + +} void onKeyCode(uint8_t key, uint8_t modifier) { led3 = 1; - if(key == 0) { + // Skip spaces, empty spaces + if(key == 0 || key == 0x20) { led3 = 0; return; } + VGA_Lock.lock(); + // Handle newline if(key == 0x0A) { @@ -124,6 +206,8 @@ currentLineIndex = 0; + VGA_Lock.unlock(); + led3 = 0; return; } @@ -136,12 +220,12 @@ currentLine[currentLineIndex] = NULL; } led3 = 0; + + VGA_Lock.unlock(); + return; } - // Move the cursor 1 character over - //ecran.move_cursor(verticalCursor, horizontalCursor++); - // Append character to curret line string currentLine[currentLineIndex] = (char)key; @@ -149,6 +233,8 @@ currentLineIndex++; } + VGA_Lock.unlock(); + led3 = 0; } @@ -159,7 +245,7 @@ while(1) { // try to connect a USB keyboard while(!keyboard.connect()) { - Thread::wait(1); + Thread::wait(200); } if(keyboard.connected()) led2 = 1; @@ -169,15 +255,34 @@ // Wait until the keyboard is disconnected while(keyboard.connected()) - Thread::wait(1); + Thread::wait(500); led2 = 0; } } +void LCD_Plots(void const *) { + // Draw plots & print + plotx2(); + makeBitmap(); + led3 = 1; + plotsin(); + makeBitmap(); + led3 = 1; + + while(1) { + Thread::wait(300); + } +} + int main() { led = 1; - // Set up display + + // Set up uLCD + uLCD.baudrate(300000); + uLCD.background_color(BLACK); + + // Set up VGA display ecran.baudrate(300000); ecran.screen_mode(LANDSCAPE); ecran.graphics_parameters(RESOLUTION, 2); @@ -192,43 +297,36 @@ ecran.text_bgd_color(DGREY); ecran.puts("Booting up..."); - ecran.line(400, 0 , 400, 480, RED); - ecran.filled_rectangle(400,0,405,480,RED); + ecran.filled_rectangle(398,0,402,480,RED); // Use old style threading - Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); + Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); - ecran.move_cursor(2, 0); - ecran.puts("Ready!"); - //int pix = ecran.read_pixel(400, 0); - //printer.printf((char *)pix); + // Launch VGA plot through thread + Thread vgaPlot(plotx2vga, NULL, osPriorityNormal, 256 * 4); + + plotx2(); makeBitmap(); led3 = 1; - - //printer.printf("Printing from main"); - //printer.feed(2); - //printer.test(); + plotsin(); + makeBitmap(); + led3 = 1; + + // Launch LCD plots with thread + // Thread lcdPlots(LCD_Plots, NULL, osPriorityNormal, 256 * 4); // Clear currentLine array before using for(size_t i = 0; i < 48; i++) { currentLine[i] = NULL; } - - //Thread drawPlot(plot, NULL, osPriorityNormal, 256*4); - //int pixel = ecran.read_pixel(200, 200); - //char arr[10]; - //itoa(pixel, arr, 2); - //ecran.put_string(pixel); - //ecran.put_string(arr); - - // char * arr = - // reinterpret_cast <char *>(&pixel); - - // printer.puts(arr); + VGA_Lock.lock(); + ecran.move_cursor(2, 0); + ecran.puts("Ready!"); + VGA_Lock.unlock(); while(1) { led=!led; - Thread::wait(500); // Wait .5s in main thread + Thread::wait(600); // Wait .6s in main thread } }