Thomas Hamilton
/
3DM-GX2
interface class for an inertial measurement unit that uses a serial protocol.
MS3DMGX2.h@1:555c2c2bf9d3, 2011-02-13 (annotated)
- Committer:
- Blaze513
- Date:
- Sun Feb 13 10:49:06 2011 +0000
- Revision:
- 1:555c2c2bf9d3
interrupt still under construction
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Blaze513 | 1:555c2c2bf9d3 | 1 | #ifndef MS3DMGX2Library |
Blaze513 | 1:555c2c2bf9d3 | 2 | #define MS3DMGX2Library |
Blaze513 | 1:555c2c2bf9d3 | 3 | |
Blaze513 | 1:555c2c2bf9d3 | 4 | #include "stdint.h" |
Blaze513 | 1:555c2c2bf9d3 | 5 | #include "mbed.h" |
Blaze513 | 1:555c2c2bf9d3 | 6 | |
Blaze513 | 1:555c2c2bf9d3 | 7 | class MS3DMGX2 |
Blaze513 | 1:555c2c2bf9d3 | 8 | { |
Blaze513 | 1:555c2c2bf9d3 | 9 | private: |
Blaze513 | 1:555c2c2bf9d3 | 10 | ///////////////////// |
Blaze513 | 1:555c2c2bf9d3 | 11 | //Serial PC; |
Blaze513 | 1:555c2c2bf9d3 | 12 | ////////////////////////// |
Blaze513 | 1:555c2c2bf9d3 | 13 | |
Blaze513 | 1:555c2c2bf9d3 | 14 | Serial DataLines; |
Blaze513 | 1:555c2c2bf9d3 | 15 | volatile unsigned char Buffer[87];//volatile is for interrupted access |
Blaze513 | 1:555c2c2bf9d3 | 16 | volatile unsigned char BufferEnd; |
Blaze513 | 1:555c2c2bf9d3 | 17 | volatile unsigned char PacketSize; |
Blaze513 | 1:555c2c2bf9d3 | 18 | bool Checksum(unsigned char Index, unsigned char Length); |
Blaze513 | 1:555c2c2bf9d3 | 19 | void FillSerialBuffer(); |
Blaze513 | 1:555c2c2bf9d3 | 20 | |
Blaze513 | 1:555c2c2bf9d3 | 21 | public: |
Blaze513 | 1:555c2c2bf9d3 | 22 | MS3DMGX2(PinName tx, PinName rx); |
Blaze513 | 1:555c2c2bf9d3 | 23 | //serial output, serial input |
Blaze513 | 1:555c2c2bf9d3 | 24 | ~MS3DMGX2(); |
Blaze513 | 1:555c2c2bf9d3 | 25 | //release resources |
Blaze513 | 1:555c2c2bf9d3 | 26 | unsigned char BufferStart; |
Blaze513 | 1:555c2c2bf9d3 | 27 | |
Blaze513 | 1:555c2c2bf9d3 | 28 | unsigned char CommandByte; |
Blaze513 | 1:555c2c2bf9d3 | 29 | unsigned char ResponseLength; |
Blaze513 | 1:555c2c2bf9d3 | 30 | unsigned char Continuous; |
Blaze513 | 1:555c2c2bf9d3 | 31 | |
Blaze513 | 1:555c2c2bf9d3 | 32 | |
Blaze513 | 1:555c2c2bf9d3 | 33 | bool Readable(); |
Blaze513 | 1:555c2c2bf9d3 | 34 | |
Blaze513 | 1:555c2c2bf9d3 | 35 | bool Mode(unsigned char Selection); |
Blaze513 | 1:555c2c2bf9d3 | 36 | //argument sets operating mode; |
Blaze513 | 1:555c2c2bf9d3 | 37 | //set flag 0x08 to 0 for polled modes, or 1 for interrupt modes; |
Blaze513 | 1:555c2c2bf9d3 | 38 | //set flag 0x04 to 0 for synchronous modes, or 1 for asynchronous modes; |
Blaze513 | 1:555c2c2bf9d3 | 39 | //set flags 0x03 to 0 for pwm, 1 for analog, or 2 for serial; |
Blaze513 | 1:555c2c2bf9d3 | 40 | //asynchronous modes read user input command, measures and calculates |
Blaze513 | 1:555c2c2bf9d3 | 41 | //output, and writes the data packet to the serial buffer every 10 ms; |
Blaze513 | 1:555c2c2bf9d3 | 42 | //interrupt modes automatically read the packet into the buffer provided |
Blaze513 | 1:555c2c2bf9d3 | 43 | //by AttachInterruptBuffer with the selected input method every 10 ms |
Blaze513 | 1:555c2c2bf9d3 | 44 | //if in an asynchronous mode, or 10 ms after RequestSyncRead is called; |
Blaze513 | 1:555c2c2bf9d3 | 45 | //interrupt mode interrupts are generated if there are bytes on the |
Blaze513 | 1:555c2c2bf9d3 | 46 | //serial buffer and are only cleared if the serial buffer is emptied; |
Blaze513 | 1:555c2c2bf9d3 | 47 | //default is 0 |
Blaze513 | 1:555c2c2bf9d3 | 48 | //void AttachInterruptBuffer(float* Buffer); |
Blaze513 | 1:555c2c2bf9d3 | 49 | //if interrupts are used, user must provide address to write result to |
Blaze513 | 1:555c2c2bf9d3 | 50 | //void AttachInterruptFunction(); |
Blaze513 | 1:555c2c2bf9d3 | 51 | //this overload reattaches the native interrupt function |
Blaze513 | 1:555c2c2bf9d3 | 52 | //void AttachInterruptFunction(void (*Function)()); |
Blaze513 | 1:555c2c2bf9d3 | 53 | //this overload attaches a function to the serial interrupt |
Blaze513 | 1:555c2c2bf9d3 | 54 | //template<class Class> void AttachInterruptFunction |
Blaze513 | 1:555c2c2bf9d3 | 55 | // (Class* Object, void (Class::*Function)()); |
Blaze513 | 1:555c2c2bf9d3 | 56 | //this overload attaches a member function to the serial interrupt; |
Blaze513 | 1:555c2c2bf9d3 | 57 | //to change the interrupt function, call one of the |
Blaze513 | 1:555c2c2bf9d3 | 58 | //"AttachInterruptFunction" overloads with pointers to the desired function; |
Blaze513 | 1:555c2c2bf9d3 | 59 | //changes polled mode to interrupt equivalent; |
Blaze513 | 1:555c2c2bf9d3 | 60 | //the interrupt will not be cleared until the serial buffer is emptied |
Blaze513 | 1:555c2c2bf9d3 | 61 | void RequestSyncRead(); |
Blaze513 | 1:555c2c2bf9d3 | 62 | //this tells the device to prepare a synchronous reading; |
Blaze513 | 1:555c2c2bf9d3 | 63 | //must be called at least 10 ms before the reading is needed; |
Blaze513 | 1:555c2c2bf9d3 | 64 | //changes asynchronous mode to synchronous equivalent |
Blaze513 | 1:555c2c2bf9d3 | 65 | void DiscardSerialBuffer(); |
Blaze513 | 1:555c2c2bf9d3 | 66 | //the serial port has a buffer and only the oldest data is read from it; |
Blaze513 | 1:555c2c2bf9d3 | 67 | //the buffer has limited space and, when full, the newest data is discarded; |
Blaze513 | 1:555c2c2bf9d3 | 68 | //this method allows the user to empty the buffer of old data so new data is used |
Blaze513 | 1:555c2c2bf9d3 | 69 | bool Read(float* Data); |
Blaze513 | 1:555c2c2bf9d3 | 70 | //get a reading from the device in the set mode; |
Blaze513 | 1:555c2c2bf9d3 | 71 | //RequestSyncRead() must be called at least 10 ms |
Blaze513 | 1:555c2c2bf9d3 | 72 | //before this method can be called in a synchronous mode; |
Blaze513 | 1:555c2c2bf9d3 | 73 | //may be called at any time during asynchronous mode; |
Blaze513 | 1:555c2c2bf9d3 | 74 | //it is assumed that the input buffer has enough |
Blaze513 | 1:555c2c2bf9d3 | 75 | //room to accomodate the desired data packet |
Blaze513 | 1:555c2c2bf9d3 | 76 | //operator float*(); |
Blaze513 | 1:555c2c2bf9d3 | 77 | //shorthand for taking a reading; |
Blaze513 | 1:555c2c2bf9d3 | 78 | //ex: "float reading[packetlength]; reading = MB1210Object;" |
Blaze513 | 1:555c2c2bf9d3 | 79 | }; |
Blaze513 | 1:555c2c2bf9d3 | 80 | |
Blaze513 | 1:555c2c2bf9d3 | 81 | #endif |