a command line interface that can dynamically assign pins

Dependencies:   TextLCD mbed PS2

Files at this revision

API Documentation at this revision

Comitter:
gsulc
Date:
Thu Oct 20 20:27:04 2011 +0000
Commit message:

Changed in this revision

Command.cpp Show annotated file Show diff for this revision Revisions of this file
Command.h Show annotated file Show diff for this revision Revisions of this file
Interface.cpp Show annotated file Show diff for this revision Revisions of this file
Interface.h Show annotated file Show diff for this revision Revisions of this file
PS2.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
keyname.cpp Show annotated file Show diff for this revision Revisions of this file
keyname.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Command.cpp	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,386 @@
+/**************************************************************/
+// Name: Commande.h
+// 
+// Description: defines a command object for checking and  
+//  executing commands
+//
+/**************************************************************/
+
+#include "Command.h"
+#include "Interface.h"
+#include "PS2Keyboard.h"
+#include <string.h>
+#include "keyname.h"
+#include "TextLCD.h"
+#include <stdlib.h>
+
+Interface interface[32];
+
+/*Command::Command(char* cmdname){
+    strcpy(name, cmdname);
+    parameter_count = 0;
+}
+
+int Command::addParameter(char* param){
+    
+    return parameter_count++;
+}*/
+
+PS2Keyboard ps2kb(p12, p11); // CLK, DAT
+//Serial pc(USBTX, USBRX);
+TextLCD pc(p15, p16, p17, p18, p19, p20);
+//char input[32];
+
+DigitalOut testled(LED2);
+
+void execute(char* input){
+    char* cmdname = strtok(input," -");
+    char* param;
+    if(strcmp(cmdname, "DIN") == 0){
+        param = strtok(NULL," -");
+        // create operation
+        if(strcmp(param, "C") == 0){
+            param = strtok (NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param+1);
+                if(pin_num >= 5 && pin_num <= 32){
+                    pc.printf("\n\nCreating D_IN...");
+                    wait(2);
+                    interface[pin_num-5].p_object = malloc(sizeof(DigitalIn));
+                    memmove(interface[pin_num-5].p_object, &DigitalIn(getPin(pin_num)), sizeof(DigitalIn));
+                }
+            }
+            else {  //pins not given
+            }
+        }
+        // read operation
+        else if(strcmp(param, "R") == 0){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param+1);
+                if(pin_num >= 5 && pin_num <= 32){
+                    pc.printf("\n\nReading...");
+                    int val = ((DigitalIn)(*(DigitalIn*)interface[pin_num-5].p_object)).read();
+                    pc.printf("\nI am: %d", val);
+                    wait(2);
+                }
+            }
+        }
+    }
+    else if((strcmp(cmdname, "DOUT")) == 0){
+        param = strtok(NULL," -");
+        // create operation
+        if(strcmp(param, "C") == 0){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param + 1);
+                if(pin_num >= 5 && pin_num <= 32){
+                    pc.printf("\n\nCreating D_Out..");
+                    wait(1);
+                    //DigitalOut tmp(getPin(pin_num));
+                    //interface[pin_num-5].p_object = &tmp;
+                    //interface[pin_num-5].dout = &DigitalOut(getPin(pin_num));
+                    interface[pin_num-5].p_object = malloc(sizeof(DigitalOut));
+                    //(DigitalOut)interface[pin_num-5].p_object = DigitalOut(getPin(pin_num));
+                    memmove(interface[pin_num-5].p_object, &DigitalOut(getPin(pin_num)), sizeof(DigitalOut));
+                    //pc.printf("\n\nwriting to %X", interface[pin_num-5].p_object);
+                    //wait(2);
+                    
+                    /*DigitalOut t = *((DigitalOut*)(interface[pin_num-5].p_object));
+                    
+                    wait(4);*/
+                    //interface[pin_num-5].dout = &tmp;
+                    
+                }
+            }
+            else {  //pins not given
+            }
+        }
+        // write operation
+        else if(strcmp(param, "W") == 0){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param + 1);
+                if(pin_num >= 5 && pin_num <= 32){
+                    param = strtok(NULL," -");
+                    int a = str2int(param);
+                    //interface[pin_num-5].dout = a;
+                    //(DigitalOut)(*((DigitalOut*)(interface[pin_num-5].dout))).write(a);
+                    //DigitalOut t = *((DigitalOut*)(interface[pin_num-5].p_object));
+                    //memcpy(, sizeof(DigitalOut));
+                    //pc.printf("\n\na:%X", interface[pin_num-5].p_object);
+                    //pc.printf("\nb:%X", &t);
+                    //wait(2);
+                    pc.printf("\n\nWriting...");
+                    wait(2);
+                    ((DigitalOut)(*(DigitalOut*)interface[pin_num-5].p_object)).write(a);
+                    //t = a;
+                    //(DigitalOut)(*(interface[pin_num-5].dout)).write(a);
+                }
+            }
+            else{ //Bad pin parameter
+            }
+        }
+        
+        else if(strcmp(param, "R") == 0){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param + 1);
+                if(pin_num >= 5 && pin_num <= 32){
+                    pc.printf("\n\nWriting...");
+                    wait(2);
+                    int a = ((DigitalOut)(*(DigitalOut*)interface[pin_num-5].p_object)).read();
+                    pc.printf("\n\n Value = %d", a);
+                    wait(2);
+                }
+            }
+            else{ //Bad pin parameter
+            }
+        }
+    }
+    else if(!strcmp(cmdname, "I2C")){
+        char* param = strtok(NULL," -");
+        // create operation
+        if(!strcmp(param, "C")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                pc.printf("\n\nCreating I2C...");
+                wait(2);
+                int pin_num = str2int(param+1);
+                interface[pin_num-5].p_object = malloc(sizeof(I2C));
+                if(pin_num == 9 || pin_num == 10){
+                    memmove(interface[pin_num-5].p_object, &I2C(p9, p10), sizeof(I2C));
+                }
+                else if(pin_num == 28 || pin_num == 27){
+                    memmove(interface[pin_num-5].p_object, &I2C(p28, p27), sizeof(I2C));
+                }
+            }
+            else {  //pins not given
+            }
+        }
+        // write operation
+        else if(!strcmp(param, "W")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+            
+                pc.printf("\n\nWriting to I2C...");
+                wait(2);
+                int pin_num = str2int(param+1);
+                //I2C tmp = *((I2C*)(interface[pin_num-5].p_object));
+                
+                int addr, strlen;
+                
+                param = strtok(NULL," -");
+                addr = hexstr2int(param);
+                param = strtok(NULL," -");
+                strlen = str2int(param);
+                char cmd[strlen];
+                for(int i = 0; i < strlen; i++){
+                    param = strtok(NULL," -");
+                    cmd[i] = str2int(&param[i]);
+                }
+                
+                
+                ((I2C)(*(I2C*)interface[pin_num-5].p_object)).write(addr, cmd, strlen);
+            }
+        }
+        //read
+        else if(!strcmp(param, "R")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                pc.printf("\n\nReading from I2C...");
+                wait(2);
+            
+                int pin_num = str2int(param+1);
+                int addr, strlen;
+                
+                param = strtok(NULL," -");
+                addr = str2int(param);
+                param = strtok(NULL," -");
+                strlen = str2int(param);
+                char cmd[strlen];
+                
+                ((I2C)(*(I2C*)interface[pin_num-5].p_object)).read(addr, cmd, strlen);
+                // print the ranging data to the screen
+                int e = 0;
+                for(int i = 1; i < strlen; i++){
+                    e =  ((e << 8) + cmd[i]);
+                }
+                float echo = (float)e* 0.01;
+                pc.printf("Value = %.4f\n", echo);
+                wait(2);
+                // print the ranging data to the screen
+                //float echo = 0.01 * ((cmd[0] << 8) + cmd[1]);
+                //pc.printf("Range = %.2f\n", echo);
+                //wait(2);
+            }
+        }
+    }
+    else if(!strcmp(cmdname, "SERIAL")){
+    //baud, putc, getc
+        char* param = strtok(NULL," -");
+        // create operation
+        if(!strcmp(param, "C")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                pc.printf("\n\nCreating Serial...");
+                wait(2);
+                int pin_num = str2int(param+1);
+                interface[pin_num-5].p_object = malloc(sizeof(Serial));
+                if(pin_num == 9 || pin_num == 10){
+                    memmove(interface[pin_num-5].p_object, &Serial(p9, p10), sizeof(Serial));
+                }
+                else if(pin_num == 13 || pin_num == 14){
+                    memmove(interface[pin_num-5].p_object, &Serial(p13, p14), sizeof(Serial));
+                }
+                else if(pin_num == 28 || pin_num == 27){
+                    memmove(interface[pin_num-5].p_object, &Serial(p28, p27), sizeof(Serial));
+                }
+            }
+            else {  //pins not given
+            }
+        }
+        //putc
+        else if(!strcmp(param, "P")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param+1);
+                param = strtok(NULL," -");
+                int c = str2int(param);
+                pc.printf("\n\nPutting a %d", c);
+                wait(2);
+                ((Serial)(*(Serial*)interface[pin_num-5].p_object)).putc(c);
+            }
+        }
+        //getc
+        else if(!strcmp(param, "G")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param+1);
+                int val = ((Serial)(*(Serial*)interface[pin_num-5].p_object)).getc();
+                pc.printf("\nValue: %d", val);
+                wait(2);
+            }
+        }
+        //baud
+        else if(!strcmp(param, "B")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param+1);
+                param = strtok(NULL," -");
+                int a = str2int(param);
+                pc.printf("\n\nSetting baud to %d", a);
+                wait(2);
+                ((Serial)(*(Serial*)interface[pin_num-5].p_object)).baud(a);
+            }
+        }
+    }
+    
+    else if(!strcmp(cmdname, "PWMO")){
+        char* param = strtok(NULL," -");
+        // create operation
+        if(!strcmp(param, "C")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param+1);
+                if(pin_num >= 21 && pin_num <= 26){
+                    pc.printf("\n\nCreating PWM...");
+                    wait(2);
+                    interface[pin_num-5].p_object = malloc(sizeof(PwmOut));
+                    memmove(interface[pin_num-5].p_object, &PwmOut(getPin(pin_num)), sizeof(PwmOut));
+                }
+            }
+            else {  //pins not given
+            }
+        }
+        //write
+        else if(strcmp(param, "W") == 0){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param + 1);
+                if(pin_num >= 21 && pin_num <= 26){
+                    param = strtok(NULL," -");
+                    //float a = str2int(param) / (sizeof(int)*8 - 1);
+                    float a = atof(param);
+                    pc.printf("\n\nWriting... a %f", a);
+                    wait(2);
+                    ((PwmOut)(*(PwmOut*)interface[pin_num-5].p_object)).write(a);
+                }
+            }
+            else{ //Bad pin parameter
+            }
+        }
+        
+        //read
+        else if(!strcmp(param, "R")){
+            param = strtok(NULL," -");
+            if(param[0] == 'P'){
+                int pin_num = str2int(param + 1);
+                if(pin_num >= 21 && pin_num <= 26){
+                    float a = ((PwmOut)(*(PwmOut*)interface[pin_num-5].p_object)).read();
+                    pc.printf("\n\nValue = %f", a);
+                    wait(2);
+                }
+            }
+        }
+    }
+}
+
+void run(){
+        /*// Read Input
+        pc.printf("> ");
+        pc.scanf("%s", input);
+        pc.printf("\n\r");
+        pc.printf("%s\n\r", input);
+        
+        // Execute Instruction
+        if(1)
+            execute(input);*/
+            
+    PS2Keyboard::keyboard_event_t evt_kb;
+    char str[32];
+    for(int i = 0; i<32 ; i++) str[i] = ' ';    
+    int numchar = 0;
+    char *p = str;
+    while (1) {
+        if (ps2kb.processing(&evt_kb)) {
+            for (int i = 0; i < evt_kb.length; i++) {
+                if(evt_kb.scancode[i] == 0xf0){
+                    i++;
+                    char c = evt_kb.scancode[i];
+                    if(c == 0x5A){
+                        // Gordon can call a function here to use str to do whatever
+                        execute(p);
+                    
+                        for(int i = 0; i<32 ; i++) 
+                            p[i] = ' ';
+                        numchar = 0;
+                    }
+                    else numchar = curstr(p,numchar,c);
+                }
+            }
+            for(int i = 0; i<32 ; i++) 
+                pc.printf("%c", str[i]);
+            pc.printf("\n\n"); 
+        }       
+        
+    }
+}
+
+int str2int(char* str) {
+    int num = 0, i = 0;
+    while(str[i] != NULL){
+        num = num * 10 + str[i] - '0';
+        i++;
+    }
+    return num;
+}
+
+int hexstr2int(char* str) {
+    int num = 0, i = 0;
+    while(str[i] != NULL){
+        num = num * 16 + str[i] - '0';
+        i++;
+    }
+    return num;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Command.h	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,23 @@
+/**************************************************************/
+// Name: Commande.h
+// 
+// Description: defines a command object for checking and  
+//  executing commands
+//
+/**************************************************************/
+
+#ifndef _COMMAND_H
+
+#define _COMMAND_H
+
+#include <string.h>
+#include "mbed.h"
+
+//Serial pc(USBTX, USBRX); // USB Virtual COM
+
+#endif
+
+void execute(char* input);
+int str2int(char* str);
+int hexstr2int(char* str);
+void run();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Interface.cpp	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,78 @@
+/**************************************************************/
+// Name: Interface.h
+// 
+// Description: defines an interface object for assigning 
+//  interfaces on the fly
+//
+/**************************************************************/
+
+#include "Interface.h"
+#include <string.h>
+
+/*Interface::Interface(char type_str[], char pins_str[]), void* interface)
+{
+    // read string to find out the type, check the pins against an 
+    // appropriate mask, and create an interface of that type
+    if(strcmp())
+        type = 
+}*/
+
+PinName getPin(int pin) {
+    switch(pin){
+        case 5:
+            return p5;
+        case 6:
+            return p6;
+        case 7:
+            return p7;
+        case 8:
+            return p8;
+        case 9:
+            return p9;
+        case 10:
+            return p10;
+        case 11:
+            return p11;
+        case 12:
+            return p12;
+        case 13:
+            return p13;
+        case 14:
+            return p14;
+        case 15:
+            return p15;
+        case 16:
+            return p16;
+        case 17:
+            return p17;
+        case 18:
+            return p18;
+        case 19:
+            return p19;
+        case 20:
+            return p20;
+        case 21:
+            return p21;
+        case 22:
+            return p22;
+        case 23:
+            return p23;
+        case 24:
+            return p24;
+        case 25:
+            return p25;
+        case 26:
+            return p26;
+        case 27:
+            return p27;
+        case 28:
+            return p28;
+        case 29:
+            return p29;
+        case 30:
+            return p30;
+        default:
+            //return NULL;
+            printf("Error: Not a valid pin");
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Interface.h	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,31 @@
+/**************************************************************/
+// Name: Interface.h
+// 
+// Description: defines an interface object for assigning 
+//  interfaces on the fly
+//
+/**************************************************************/
+
+#include "mbed.h"
+
+#ifndef _INTERFACE_H
+#define _INTERFACE_H
+//enum InterfaceType = {INVALID, DIGITAL_IN, DIGITAL_OUT, PWM, I2C, SERIAL};
+typedef struct{
+    //unsigned char bottom_pin;
+    //InterfaceType type;
+    void* p_object;
+    DigitalOut* dout;
+} Interface;
+
+#endif
+
+PinName getPin(int pin);
+
+/*class Interface {
+    public:
+        Interface(char type_str[], char pins_str[]);
+        InterfaceType type;
+        unsigned char* pins;
+        unsigned char mask;
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PS2.lib	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/PS2/#a57bbbec16b1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#a53b3e2d6f1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/keyname.cpp	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,125 @@
+#include "keyname.h"
+
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
+
+char keyname(char scancode){
+    switch(scancode){
+        case 0x16:
+            return '1';
+        case 0x1E:
+            return '2';
+        case 0x26:
+            return '3';
+        case 0x25:
+            return '4';
+        case 0x2E:
+            return '5';
+        case 0x36:
+            return '6';
+        case 0x3D:
+            return '7';
+        case 0x3E:
+            return '8';
+        case 0x46:
+            return '9';
+        case 0x45:
+            return '0';
+        case 0x15:
+            return 'Q';
+        case 0x1D:
+            return 'W';
+        case 0x24:
+            return 'E';
+        case 0x2D:
+            return 'R';
+        case 0x2C:
+            return 'T';
+        case 0x35:
+            return 'Y';
+        case 0x3C:
+            return 'U';
+        case 0x43:
+            return 'I';
+        case 0x44:
+            return 'O';
+        case 0x4D:
+            return 'P';
+        case 0x1C:
+            return 'A';
+        case 0x1B:
+            return 'S';
+        case 0x23:
+            return 'D';
+        case 0x2B:
+            return 'F';
+        case 0x34:
+            return 'G';
+        case 0x33:
+            return 'H';
+        case 0x3B:
+            return 'J';
+        case 0x42:
+            return 'K';
+        case 0x4B:
+            return 'L';
+        case 0x1A:
+            return 'Z';
+        case 0x22:
+            return 'X';
+        case 0x21:
+            return 'C';
+        case 0x2A:
+            return 'V';
+        case 0x32:
+            return 'B';
+        case 0x31:
+            return 'N';
+        case 0x3A:
+            return 'M';
+        case 0x4e:
+            return '-';
+        case 0x49:
+            return '.';
+        case 0x29:
+            return ' ';
+        default:
+            return '?';
+    }
+}
+
+int curstr(char* str,int numchar,char c){
+    if((c != 0x66)&&(c != 0x5A)){
+        if(numchar < 32){ 
+            char k = keyname(c);
+            if(k!= '?'){
+                str[numchar] = k;
+                numchar++;
+            }
+        }
+        else {
+            myled1 = !myled1;
+            myled2 = !myled2;
+            myled3 = !myled3;
+            myled4 = !myled4;
+            wait(0.2);
+            myled1 = !myled1;
+            myled2 = !myled2;
+            myled3 = !myled3;
+            myled4 = !myled4;    
+            wait(0.2);
+        }
+    }
+    else if(c == 0x66 && numchar > 0){
+        numchar = numchar - 1;
+        str[numchar] = ' ';
+    }
+    return numchar;
+}
+
+int keyhit(char* str,int numchar,PS2Keyboard::keyboard_event_t evt_kb){
+    
+    return numchar;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/keyname.h	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,11 @@
+#ifndef _KEYNAME_H
+#define _KEYNAME_H
+
+#include "mbed.h"
+#include "PS2Keyboard.h"
+
+char keyname(char scancode);
+int curstr(char* str,int numchar,char c);
+int keyhit(char* str,int numchar,PS2Keyboard::keyboard_event_t evt_kb);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+#include <string.h>
+#include "Command.h"
+
+DigitalOut myled(LED1);
+
+//Serial pc(USBTX, USBRX); // USB Virtual COM
+//char input[32];
+
+int main() {
+    while(1) {
+        /*
+        // Read Input
+        pc.printf("> ");
+        pc.scanf("%s", input);
+        pc.printf("\n\r");
+        pc.printf("%s\n\r", input);
+        
+        // Execute Instruction
+        if(1)
+            execute(input);*/
+        run();
+        
+        // Pulse Check
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Oct 20 20:27:04 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912