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

Committer:
zrussell3
Date:
2018-12-13
Revision:
2:d97e71edb2b3
Parent:
0:824466ffa6da
Child:
3:2b6951038d5b

File content as of revision 2:d97e71edb2b3:

#include "mbed.h"
#include "Thermal.h"
#include "USBHostKeyboard.h"
#include "uVGAIII.h"

//#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

#define SIZE_X       480
#define SIZE_Y       800

// Debugging LEDs
DigitalOut led(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

uVGAIII ecran(p9,p10,p11); // serial tx, serial rx, reset pin;
uLCD_4DGL uLCD(p13,p14,p12); // serial tx, serial rx, reset pin;
Thermal printer(p28, p27, 19200);

int verticalCursor = 2;
int horizontalCursor = 0;

char currentLine[48];
int currentLineIndex = 0;

unsigned char bitmap[5000];
char temp[8];

char getPixel(int x, int y, int bk) {
    int r = ecran.read_pixel(x, y);
    if (r == bk) //same as background
        return '0';
    else
        return '1';
}

void makeBitmap() {
    int bk = ecran.read_pixel(799,479);
    for (int i = 0; i < 32; ++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();
        }
       
        //need to convert to 0b format
        char * end;
        long int value = strtol(temp, &end, 2);
        bitmap[i] = value;
        led3 = !led3;
    }
    printer.printBitmap(16,16,bitmap);
    printer.feed(2);
}

/*
// hardcode plot x^2
void plot(void const *)
{
    int xOffset = 600;
    int yOffset = 260;
    int pastX = 0;
    int pastY = 0;
    int currentX = 0;
    int currentY = 0;
    
    for(double i = -20; i < 20; i++) {
        //evaluate();
        currentX = i*3 + xOffset;
        currentY = yOffset-(i*i);
        if(pastX == 0){
            pastX = currentX;
            pastY = currentY;
            continue;
        }
        ecran.line(pastX, pastY, currentX, currentY, RED);
        //ecran.put_pixel(i*2 + xOffset, yOffset-(i*i),RED);
        pastX = currentX;
        pastY = currentY;
    }
    while(1) {
        Thread::wait(200);
    }
}*/



void onKeyCode(uint8_t key, uint8_t modifier) {
    
    led3 = 1;
    
    if(key == 0) {
        led3 = 0;
        return;
    }
    
    // Handle newline
    if(key == 0x0A) {
        
        // Check if going off end of screen
        if(verticalCursor > 37) {
            ecran.filled_rectangle(0, 0 , 399, 480, DGREY);
            verticalCursor = 0;
        } else {
            // Move cursor to newline
            verticalCursor+=2;
        }
        
        horizontalCursor = 0;
        
        ecran.move_cursor(verticalCursor, horizontalCursor);
         
        ecran.puts(currentLine);  
        
        printer.printf(currentLine);
        printer.feed(2);
        
        for(size_t i = 0; i < 48; i++) {
            currentLine[i] = NULL;
        }
        
        currentLineIndex = 0;
        
        led3 = 0;
        return;
    }
    
    if(key == 0x08)
    {
        if(currentLineIndex != 0)
        {
            currentLineIndex--;
            currentLine[currentLineIndex] = NULL;
        }
        led3 = 0;
        return;
    }
    
    // Move the cursor 1 character over
    //ecran.move_cursor(verticalCursor, horizontalCursor++);
    
    // Append character to curret line string
    currentLine[currentLineIndex] = (char)key;
    
    if(currentLineIndex < 47) {
        currentLineIndex++;
    }
    
    led3 = 0;
}

void keyboard_task(void const *) {
    
    USBHostKeyboard keyboard;
    
    while(1) {
        // try to connect a USB keyboard
        while(!keyboard.connect()) {
            Thread::wait(1);
        } 
        
        if(keyboard.connected()) led2 = 1;
        
        // When connected, attach handler called on keyboard event
        keyboard.attach(onKeyCode);
        
        // Wait until the keyboard is disconnected
        while(keyboard.connected())
            Thread::wait(1); 
            
        led2 = 0;
    }
}

int main() {  
    led = 1; 
    // Set up display
    ecran.baudrate(300000);
    ecran.screen_mode(LANDSCAPE);
    ecran.graphics_parameters(RESOLUTION, 2); 
    ecran.touch_status();
    ecran.background_color(DGREY);
    ecran.cls();

    ecran.move_cursor(0, 0);
    ecran.char_width('d');
    ecran.char_height('d');
    ecran.text_fgd_color(WHITE);
    ecran.text_bgd_color(DGREY);
    ecran.puts("Booting up...");   
    
    ecran.line(400, 0 , 400, 480, RED);
    ecran.filled_rectangle(400,0,405,480,RED);
        
    // Use old style threading
    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);
    makeBitmap();
    led3 = 1;
    
    //printer.printf("Printing from main");
    //printer.feed(2);
    //printer.test();   
    
    // 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);
    
    while(1) {
        led=!led;
        Thread::wait(500); // Wait .5s in main thread
    }
}