astek
GPS_I2C.cpp@0:f4c7ece483fe, 2019-12-27 (annotated)
- Committer:
- RoddyRod
- Date:
- Fri Dec 27 15:32:37 2019 +0000
- Revision:
- 0:f4c7ece483fe
fsabatier;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RoddyRod | 0:f4c7ece483fe | 1 | #include "GPS_I2C.h" |
RoddyRod | 0:f4c7ece483fe | 2 | |
RoddyRod | 0:f4c7ece483fe | 3 | GPS_I2C::GPS_I2C(PinName sda, PinName scl, uint32_t freq) |
RoddyRod | 0:f4c7ece483fe | 4 | : _i2c(sda, scl), _freq(freq) |
RoddyRod | 0:f4c7ece483fe | 5 | { |
RoddyRod | 0:f4c7ece483fe | 6 | _i2c.frequency(_freq); |
RoddyRod | 0:f4c7ece483fe | 7 | } |
RoddyRod | 0:f4c7ece483fe | 8 | |
RoddyRod | 0:f4c7ece483fe | 9 | GPS_I2C::~GPS_I2C() {} |
RoddyRod | 0:f4c7ece483fe | 10 | /*****************************************************/ |
RoddyRod | 0:f4c7ece483fe | 11 | bool GPS_I2C::begin() |
RoddyRod | 0:f4c7ece483fe | 12 | { |
RoddyRod | 0:f4c7ece483fe | 13 | int ack; |
RoddyRod | 0:f4c7ece483fe | 14 | |
RoddyRod | 0:f4c7ece483fe | 15 | _head = 0; |
RoddyRod | 0:f4c7ece483fe | 16 | _tail = 0; |
RoddyRod | 0:f4c7ece483fe | 17 | |
RoddyRod | 0:f4c7ece483fe | 18 | ack = _i2c.read(MT333x_ADDR << 1, (char*)gpsData, 1); |
RoddyRod | 0:f4c7ece483fe | 19 | //Verify if module is connected |
RoddyRod | 0:f4c7ece483fe | 20 | if( ack != 0 ) return false; |
RoddyRod | 0:f4c7ece483fe | 21 | else return true; |
RoddyRod | 0:f4c7ece483fe | 22 | } |
RoddyRod | 0:f4c7ece483fe | 23 | |
RoddyRod | 0:f4c7ece483fe | 24 | void GPS_I2C::check() |
RoddyRod | 0:f4c7ece483fe | 25 | { |
RoddyRod | 0:f4c7ece483fe | 26 | uint8_t packetData[MAX_PACKET_SIZE]; |
RoddyRod | 0:f4c7ece483fe | 27 | |
RoddyRod | 0:f4c7ece483fe | 28 | for (uint8_t x = 0 ; x < MAX_PACKET_SIZE ; x++) { |
RoddyRod | 0:f4c7ece483fe | 29 | packetData[x] = 0x0A; //Fill with garbage byte |
RoddyRod | 0:f4c7ece483fe | 30 | } |
RoddyRod | 0:f4c7ece483fe | 31 | |
RoddyRod | 0:f4c7ece483fe | 32 | _i2c.read(MT333x_ADDR << 1, ((char*)packetData), MAX_PACKET_SIZE); |
RoddyRod | 0:f4c7ece483fe | 33 | |
RoddyRod | 0:f4c7ece483fe | 34 | for (uint8_t x = 0 ; x < MAX_PACKET_SIZE ; x++) { |
RoddyRod | 0:f4c7ece483fe | 35 | if (packetData[x] != 0x0A) { |
RoddyRod | 0:f4c7ece483fe | 36 | gpsData[_head++] = packetData[x]; |
RoddyRod | 0:f4c7ece483fe | 37 | if (_head == MAX_PACKET_SIZE) _head = 0; //Wrap variable |
RoddyRod | 0:f4c7ece483fe | 38 | } |
RoddyRod | 0:f4c7ece483fe | 39 | } |
RoddyRod | 0:f4c7ece483fe | 40 | } |
RoddyRod | 0:f4c7ece483fe | 41 | |
RoddyRod | 0:f4c7ece483fe | 42 | uint8_t GPS_I2C::available() |
RoddyRod | 0:f4c7ece483fe | 43 | { |
RoddyRod | 0:f4c7ece483fe | 44 | //If tail=head then no new data is available in the local buffer |
RoddyRod | 0:f4c7ece483fe | 45 | //So now check to see if the module has anything new in its buffer |
RoddyRod | 0:f4c7ece483fe | 46 | if (_tail == _head) { |
RoddyRod | 0:f4c7ece483fe | 47 | check(); //Check to module to see if new I2C bytes are available |
RoddyRod | 0:f4c7ece483fe | 48 | } |
RoddyRod | 0:f4c7ece483fe | 49 | |
RoddyRod | 0:f4c7ece483fe | 50 | //Return new data count |
RoddyRod | 0:f4c7ece483fe | 51 | if (_head > _tail) return (_head - _tail); |
RoddyRod | 0:f4c7ece483fe | 52 | if (_tail > _head) return (MAX_PACKET_SIZE - _tail + _head); |
RoddyRod | 0:f4c7ece483fe | 53 | return (0); //No data available |
RoddyRod | 0:f4c7ece483fe | 54 | } |
RoddyRod | 0:f4c7ece483fe | 55 | uint8_t GPS_I2C::read() |
RoddyRod | 0:f4c7ece483fe | 56 | { |
RoddyRod | 0:f4c7ece483fe | 57 | if (_tail != _head) { |
RoddyRod | 0:f4c7ece483fe | 58 | uint8_t datum = gpsData[_tail++]; |
RoddyRod | 0:f4c7ece483fe | 59 | if (_tail == MAX_PACKET_SIZE) _tail = 0; //Wrap variable |
RoddyRod | 0:f4c7ece483fe | 60 | return (datum); |
RoddyRod | 0:f4c7ece483fe | 61 | } else |
RoddyRod | 0:f4c7ece483fe | 62 | return (0); //No new data |
RoddyRod | 0:f4c7ece483fe | 63 | } |