SLCAN/CAN-USB implementation for mbed targets

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers slcan.h Source File

slcan.h

00001 #ifndef SLCAN_H_INCLUDED
00002 #define SLCAN_H_INCLUDED
00003 
00004 #include <mbed.h>
00005 #include <USBSerial.h>
00006 
00007 class SLCANBase {
00008 public:
00009     bool update();
00010 
00011 protected:
00012     SLCANBase();
00013     virtual ~SLCANBase();
00014     
00015     // To be implemented by subclasses
00016     virtual bool setBaudrate(int baudrate) = 0;
00017     virtual bool setMode(CAN::Mode mode) = 0;
00018     virtual bool transmitMessage(CANMessage& msg) = 0;
00019     
00020     virtual bool processCommands() = 0;
00021     virtual bool processCANMessages() = 0;
00022     virtual bool flush() = 0;
00023     
00024     virtual uint8_t getFirmwareVersion();
00025     virtual uint8_t getHardwareVersion();
00026     virtual const char* getSerialString();
00027     
00028     // Shared amongst subclasses
00029     static size_t formatCANMessage(const CANMessage& msg, char* buf, size_t max_len);
00030     static size_t formattedCANMessageLength(const CANMessage& msg);
00031     static size_t commandResponseLength(const char* command);
00032     bool execCommand(const char* command, char* response=NULL);
00033     
00034 private:
00035     bool execConfigCommand(const char* command);
00036     bool execTransmitCommand(const char* command, char* response);
00037     bool execDiagnosticCommand(const char *command, char* response);
00038 };
00039 
00040 class USBSLCAN : public SLCANBase {
00041 public:
00042     USBSLCAN(USBSerial& stream, CAN& can);
00043 protected:
00044     virtual bool setBaudrate(int baudrate);
00045     virtual bool setMode(CAN::Mode mode);
00046     virtual bool transmitMessage(CANMessage& msg);
00047     
00048     virtual bool processCommands();
00049     virtual bool processCANMessages();
00050     virtual bool flush();
00051     
00052     bool getNextCANMessage(CANMessage& msg);
00053 private:
00054     USBSerial& stream;
00055     CAN& can;
00056     CANMessage queuedMessage;
00057     bool messageQueued;
00058     bool commandQueued;
00059     bool commandOverflow;
00060     char inputCommandBuffer[32];
00061     char outputPacketBuffer[64];
00062     size_t inputCommandLen;
00063     size_t outputPacketLen;
00064 };
00065 
00066 class SerialSLCAN : public SLCANBase {
00067 public:
00068     SerialSLCAN(Serial& stream, CAN& can);
00069 protected:
00070     virtual bool setBaudrate(int baudrate);
00071     virtual bool setMode(CAN::Mode mode);
00072     virtual bool transmitMessage(CANMessage& msg);
00073     
00074     virtual bool processCommands();
00075     virtual bool processCANMessages();
00076     virtual bool flush();
00077     
00078     bool getNextCANMessage(CANMessage& msg);
00079 private:
00080     Serial& stream;
00081     CAN& can;
00082     bool commandQueued;
00083     bool commandOverflow;
00084     char inputCommandBuffer[32];
00085     size_t inputCommandLen;
00086 };
00087 
00088 #endif