canio.h

00001 #ifndef _CANIO_H_
00002 #define _CANIO_H_
00003 
00004 #if HAVE_CONFIG_H
00005   #include <config.h>
00006 #endif
00007 #if HAVE_STDINT_H
00008   #include <stdint.h>
00009 #endif
00010 
00011 #include <sys/types.h>
00012 #include <string.h>
00013 #include <stdio.h>
00014 
00015 // Copied this from <canlib.h>. I assume that it's portable across CAN
00016 // implementations.
00017 #ifndef canMSG_STD
00018   #define canMSG_STD              0x0002
00019 #endif
00020 
00021 
00022 class CanPacket
00023 {
00024   public:
00025     long id;
00026     uint8_t msg[8];
00027     uint32_t dlc;
00028     uint32_t flags;
00029 
00030     CanPacket()
00031     {
00032       memset(msg,0,sizeof(msg));
00033 
00034       flags = canMSG_STD;
00035       dlc = 8;
00036     }
00037 
00038     uint16_t GetSlot(int s)  const 
00039     {
00040       return (uint16_t) ((msg[s*2] << 8) | (msg[s*2+1]));
00041     }
00042 
00043     void PutSlot(const int slot, const uint16_t val) 
00044     {
00045       msg[slot*2] = (val >> 8) & 0xFF;
00046       msg[slot*2+1] = val & 0xFF;
00047     }
00048 
00049     void PutByte(const int byte, const uint16_t val) 
00050     {
00051       msg[byte] = val & 0xFF;
00052     }
00053 
00054     char* toString() 
00055     {
00056       static char buf[256];
00057       sprintf(buf, "id:%04lX %02X %02X %02X %02X %02X %02X %02X %02X",
00058               id, msg[0], msg[1], msg[2], msg[3], msg[4], msg[5], 
00059               msg[6], msg[7]);
00060 
00061       return buf;
00062     }
00063 };
00064 
00065 #define DUALCAN_NR_CHANNELS 2  
00066 
00067 /* this class encapsulates the low level CAN stuff.... so it deals
00068    with reading and writing packets on the dual CAN channels.
00069    We make the assumption that we only have to read off of one
00070    channel though (looking at rmi_demo, it appears that this is
00071    OK.)
00072    A higher level entity will make sense of the packets, and call
00073    the read/write methods with the required timing.
00074 
00075    It wouldn't take much to make this an abstract base class so that
00076    the SegwayIO can use it, and then have different CAN hardwares
00077    implement the virtual methods.  So then we can just drop in 
00078    a new CAN hardware driver class and everything would still work.
00079    Would also be able to split up the files, so we could keep
00080    canio.[cc,h] in player, and the CAN hardware specific files
00081    can be local.
00082 */
00083 
00084 class DualCANIO
00085 {
00086   public:
00087     DualCANIO() {}
00088     virtual int Init(long channel_freq) = 0;
00089     virtual int ReadPacket(CanPacket *pkt, int channel) = 0;
00090     virtual int WritePacket(CanPacket &pkt) = 0;
00091     virtual int Shutdown() = 0;
00092 };
00093 
00094 #endif

Last updated 12 September 2005 21:38:45