Changeset 32c7682 in thomson


Ignore:
Timestamp:
Jan 28, 2012, 3:13:17 PM (12 years ago)
Author:
Adrien Destugues <pulkomandy@…>
Branches:
main
Children:
c7b4218
Parents:
96bc8fa
Message:
  • Poll motor on pin status and report it in the GUI.
  • Some cleanup of device class.

git-svn-id: svn://localhost/thomson@12 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

Location:
elec/CrO2/software
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • elec/CrO2/software/Makefile

    r96bc8fa r32c7682  
    2323all: $(PROGRAM)
    2424
    25 $(PROGRAM): $(PROJECT).o device.o
     25$(PROGRAM): $(PROJECT).o device.o poller.o
    2626        $(CC) -o $(PROGRAM) $^ $(LIBS)
    2727
  • elec/CrO2/software/cro2.cpp

    r96bc8fa r32c7682  
    55 */
    66
     7#include <iostream>
    78#include <stdio.h>
    89#include <stdlib.h>
     
    1516
    1617#include "device.h"
     18#include "poller.h"
    1719
    1820/* UI */
     
    2931
    3032
     33Ihandle* motoron;
    3134void GUI_open(int* argc, char*** argv)
    3235{
     
    4851        );
    4952
     53        // CONTROL
     54        motoron = IupProgressBar();
     55        IupSetAttribute(motoron, "RASTERSIZE", "16x16");
     56
     57        // EXPLORE
    5058        Ihandle* platformlist = IupList(NULL);
    5159        IupSetAttribute(platformlist, "EXPAND", "HORIZONTAL");
     
    5967        Ihandle* tabs = IupTabs(
    6068                IupVbox(
    61                         IupLabel("Hello World"),
     69                        IupHbox(
     70                                IupLabel("Motor"),
     71                                motoron,
     72                                NULL
     73                        ),
     74                        IupHbox(
     75                                IupToggle("play",NULL),
     76                                IupToggle("REC",NULL),
     77                                NULL
     78                        ),
    6279                        NULL
    6380                ),
     
    89106        IupShow(dialog);
    90107
     108        // Run the timer
     109        startPolling();
     110
    91111        IupMainLoop();
    92112
     
    106126    }
    107127
    108         Device dev; // Constructor inits communication.
    109                 // TODO handle thrown exceptions.
     128        try {
     129                Device& dev = Device::getDevice(); // Constructor inits communication.
    110130
    111     if(strcmp(argv[1], "get") == 0){
    112                 memset(buffer, 0, 275);
    113                 nBytes = dev.read(buffer, sizeof(buffer));
    114     }else if(strcmp(argv[1], "put") == 0){
     131                if(strcmp(argv[1], "get") == 0){
     132                        memset(buffer, 0, 275);
     133                        nBytes = dev.read(buffer, sizeof(buffer));
     134                }else if(strcmp(argv[1], "put") == 0){
    115135
    116                 while (dev.getStatus() & 8)
    117                         usleep(1000000);
     136                        // wait for motor on
     137                        while (dev.getStatus() & 8)
     138                                usleep(1000000);
    118139
    119                 FILE* fptr = fopen(argv[2], "rb");
    120                 int blockid;
    121                 uint8_t blktype, blksize;
    122                 sscanf(argv[3], "%d", &blockid);
     140                        // load file
     141                        FILE* fptr = fopen(argv[2], "rb");
     142                        int blockid;
     143                        uint8_t blktype, blksize;
     144                        sscanf(argv[3], "%d", &blockid);
    123145
    124                 do
    125                 {
     146                        // fast-forward to requested block
    126147                        do
    127148                        {
     149                                do
     150                                {
     151                                        fread(&blktype, 1, 1, fptr);
     152                                        if (feof(fptr))
     153                                        {
     154                                                fprintf(stderr, "end of file.\n");
     155                                                fclose(fptr);
     156                                                exit(0);
     157                                        }
     158                                }
     159                                while(blktype != 0x5A); // skip sync header
     160
    128161                                fread(&blktype, 1, 1, fptr);
    129                                 if (feof(fptr))
     162                                fread(&blksize, 1, 1, fptr);
     163                                blksize -= 2;
     164                                fread(buffer, 1, blksize + 1, fptr);
     165                                if (blktype == 0)
    130166                                {
    131                                         fprintf(stderr, "end of file.\n");
    132                                         fclose(fptr);
    133                                         exit(0);
     167                                        // new file
     168                                        printf("%.11s\n",buffer);
    134169                                }
    135170                        }
    136                         while(blktype != 0x5A); // skip sync header
     171                        while (blockid --);
    137172
    138                         fread(&blktype, 1, 1, fptr);
    139                         fread(&blksize, 1, 1, fptr);
    140                         blksize -= 2;
    141                         fread(buffer, 1, blksize + 1, fptr);
    142                         if (blktype == 0)
    143                         {
    144                                 // new file
    145                                 printf("%.11s\n",buffer);
    146                         }
     173                        fclose(fptr);
     174
     175                        nBytes = dev.write(buffer, blksize, blktype);
     176                }else{
     177                        // TODO print usage
     178                        exit(2);
    147179                }
    148                 while (blockid --);
    149180
    150                 fclose(fptr);
    151 
    152                 nBytes = dev.write(buffer, blksize, blktype);
    153     }else{
    154                 GUI_open(&argc, &argv);
    155     }
    156 
    157         if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
    158     return 0;
     181                if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
     182                return 0;
     183        }
     184        catch(const char* error)
     185        {
     186                std::cerr << error << std::endl;
     187        }
    159188}
    160189
  • elec/CrO2/software/device.cpp

    r96bc8fa r32c7682  
    99#include "device.h"
    1010
    11 #include <iostream> // TODO remove
    12 
    1311bool Device::initOnce = false;
     12Device* Device::instance = NULL;
    1413
    1514const uint32_t Device::vid = 0x16C0;
     
    2423#define PSCMD_STATUS   3
    2524
    26 /* USB device lookup by VID and PID, then Vendor and Product strings, as we use V-USB shared ID. */
    27 static int  usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen)
     25
     26// Gets the device instance. Throws an error message if something bad happens.
     27Device& Device::getDevice() throw(const char*)
     28{
     29        if (instance == NULL)
     30                instance = new Device();
     31
     32        return *instance;
     33}
     34
     35
     36/* USB device lookup by VID and PID, then Vendor and Product strings, as we use
     37 * V-USB shared ID. */
     38static int  usbGetStringAscii(usb_dev_handle *dev, int index, int langid,
     39        char *buf, int buflen)
    2840{
    2941char    buffer[256];
    3042int     rval, i;
    3143
    32     if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0)
     44    if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
     45                        (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0)
    3346        return rval;
    3447    if(buffer[1] != USB_DT_STRING)
     
    4962}
    5063
    51 Device::Device()
     64Device::Device() throw(const char*)
    5265{
    5366        handle = NULL;
     
    6982                                continue;
    7083
    71                         // Found device with correct VID and PID. Now try to match the strings
     84                        // Found device with correct VID and PID. Now try to match the
     85                        // vendor and product strings
    7286                        char    string[256];
    7387                        int     len;
    74                         handle = usb_open(dev); /* we need to open the device in order to query strings */
     88                        handle = usb_open(dev);
     89                                /* we need to open the device in order to query strings */
    7590                        if(!handle){
    76                                 std::cerr << "Warning: cannot open USB device: " << usb_strerror() << std::endl;
    7791                                continue;
    7892                        }
    7993                        /* now check whether the names match: */
    80                         len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
    81                         if(len < 0)
    82                         {
    83                                 std::cerr << "Warning: cannot query manufacturer for device: " << usb_strerror() << std::endl;
    84                         }
    85                         else
     94                        len = usbGetStringAscii(handle, dev->descriptor.iManufacturer,
     95                                0x0409, string, sizeof(string));
     96                        if(len >= 0)
    8697                        {
    8798                                if(strcmp(string, vendor) == 0){
    88                                         len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
    89                                         if(len < 0){
    90                                                 std::cerr << "Warning: cannot query product for device: " << usb_strerror() << std::endl;
    91                                         }else{
     99                                        len = usbGetStringAscii(handle, dev->descriptor.iProduct,
     100                                                0x0409, string, sizeof(string));
     101                                        if(len >= 0){
    92102                                                if(strcmp(string, product) == 0)
    93103                                                        break;
     
    104114        // At this point, either we have found a device and handle is pointing to it,
    105115        // or we failed and handle is NULL.
    106         //
    107         // TODO : use exceptions for error handling; instead of fprintf (not useable in GUI mode).
    108116        if (!handle)
    109         std::cerr << "Could not find USB device \"" << product << "\" with vid=0x" << std::hex << vid << " pid=0x" << pid << std::endl;
     117                throw "Device not found. Is the USB cable plugged correctly?";
    110118}
    111119
     
    119127int Device::read(uint8_t* buffer, size_t max)
    120128{
    121         return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0,0, (char*)buffer, max, 5000);
     129        return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
     130                PSCMD_GET, 0,0, (char*)buffer, max, 5000);
    122131
    123132}
     
    127136{
    128137        int rqtype = (size == 0) ? USB_ENDPOINT_IN:USB_ENDPOINT_OUT;
    129         return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | rqtype, PSCMD_PUT, blktype,0 /*checksum*/, (char*)buffer, size, 5000);
     138        return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | rqtype,
     139                PSCMD_PUT, blktype,0 /*checksum*/, (char*)buffer, size, 5000);
    130140
    131141}
     
    134144{
    135145        uint8_t status;
    136         usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_STATUS, 0,0, (char*)&status, 1, 5000);
     146        usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
     147                PSCMD_STATUS, 0,0, (char*)&status, 1, 5000);
    137148                // TODO handle errors (return value)
    138149        return status;
  • elec/CrO2/software/device.h

    r96bc8fa r32c7682  
    1414{
    1515        public:
    16                 Device(); // Open device and set it up for communication
     16                static Device& getDevice() throw(const char*);
     17
    1718                ~Device();
    1819
     
    2223
    2324        private:
     25                Device() throw(const char*); // Open device and set it up for communication
     26                Device(const Device& other);
     27
     28
    2429                usb_dev_handle* handle;
    2530
    2631                static bool initOnce;
     32                static Device* instance;
    2733
    2834                static const uint32_t vid;
Note: See TracChangeset for help on using the changeset viewer.