USB BARCODE READER

Dependencies:   USBHOST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBHostMSD.h"
00003 #include "USBHostMouse.h"
00004 #include "USBHostKeyboard.h"
00005 #include "FATFileSystem.h"
00006 #include <stdlib.h>
00007 
00008 
00009 DigitalOut led(LED1);
00010  int index=0;
00011 char Barcode[50];
00012 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
00013     printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
00014 }
00015  
00016 void mouse_task(void const *) {
00017 
00018     USBHostMouse mouse;
00019     printf("mouse started\n");
00020     while(1) {
00021        
00022         // try to connect a USB mouse
00023         while(!mouse.connect())
00024             Thread::wait(500);
00025 
00026         // when connected, attach handler called on mouse event
00027         mouse.attachEvent(onMouseEvent);
00028 
00029         // wait until the mouse is disconnected
00030         while(mouse.connected())
00031             Thread::wait(500);
00032         printf("mouse seen disconnected\n");
00033     }
00034 }
00035 void onKey(uint8_t key) {
00036        
00037    // printf("%c\r", key);    
00038    
00039    if(index>=51)
00040    {
00041     
00042     
00043     printf(" Limit Exceeded \r\n   ");
00044     printf(" Please Re Enter \r\n  ");
00045     index=0;
00046     memset(Barcode,'\0',50);
00047     return;
00048     }
00049     if((key!=0x0A))
00050     { 
00051       Barcode[index]=key;
00052       index++;  
00053       //printf("%s \r\n",Barcode);
00054     }
00055     
00056     else if((key==0x0A) &&(index<=50))
00057     {
00058     Barcode[index]='\0';   
00059     printf("BARCODE  : %s \r\n",Barcode);
00060     memset(Barcode,'\0',50);
00061     index=0;
00062     }
00063     
00064     else if((key==0x0A) && (index>=51))
00065     {
00066    
00067     printf(" Too Many Charecters   : %s \r\n",Barcode);
00068     printf(" Invalid Barcode\r\n");
00069        
00070     }
00071     
00072 }
00073 
00074 
00075 void BarCodeReader_task(void const *) {
00076     
00077     USBHostKeyboard BarCodeReader;
00078     
00079     while(1) {
00080         // try to connect a USB keyboard
00081         while(!BarCodeReader.connect())
00082             Thread::wait(500);
00083         // when connected, attach handler called on keyboard event
00084         BarCodeReader.attach(onKey);
00085         // wait until the keyboard is disconnected
00086         while(BarCodeReader.connected())
00087             Thread::wait(500);
00088     }
00089 }
00090 
00091 
00092 
00093 void msd_task(void const *) {
00094 
00095     USBHostMSD msd;
00096     int i = 0;
00097     FATFileSystem fs("usb");
00098     int err;
00099     printf("wait for usb memory stick insertion\n");
00100     while(1) {
00101 
00102         // try to connect a MSD device
00103         while(!msd.connect()) {
00104             Thread::wait(500);
00105         }
00106         if (fs.mount(&msd) != 0) continue;
00107         else  
00108             printf("file system mounted\n");
00109 
00110         if  (!msd.connect()) {
00111             continue;
00112         }
00113 
00114         // in a loop, append a file
00115         // if the device is disconnected, we try to connect it again
00116 
00117         // append a file
00118         File file;
00119         err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND);
00120 
00121         if (err == 0) {
00122             char tmp[100];
00123             sprintf(tmp,"Hello fun USB stick  World: %d!\r\n", i++);
00124             file.write(tmp,strlen(tmp));
00125             sprintf(tmp,"Goodbye World!\r\n");
00126             file.write(tmp,strlen(tmp));
00127             file.close();
00128         } else {
00129             printf("FILE == NULL\r\n");
00130         }
00131         Thread::wait(500);
00132         printf("again\n");
00133         // if device disconnected, try to connect again
00134         while (msd.connected()) {
00135             Thread::wait(500);
00136         }
00137         while (fs.unmount() < 0) {
00138             Thread::wait(500);
00139             printf("unmount\n"); 
00140         }
00141     }
00142 }
00143 
00144 int main() {
00145     
00146     memset(Barcode,'\0',20);
00147     Thread msdTask(msd_task, NULL, osPriorityNormal, 4096);
00148     Thread mouseTask(mouse_task, NULL, osPriorityNormal, 4096);
00149     Thread BarCodeTask(BarCodeReader_task, NULL, osPriorityNormal, 4096);
00150     
00151     while(1) {
00152         led=!led;
00153         Thread::wait(500);
00154     }
00155 }