astek

Files at this revision

API Documentation at this revision

Comitter:
RoddyRod
Date:
Fri Dec 27 15:32:37 2019 +0000
Commit message:
fsabatier;

Changed in this revision

GPS_I2C.cpp Show annotated file Show diff for this revision Revisions of this file
GPS_I2C.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f4c7ece483fe GPS_I2C.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS_I2C.cpp	Fri Dec 27 15:32:37 2019 +0000
@@ -0,0 +1,63 @@
+#include "GPS_I2C.h"
+
+GPS_I2C::GPS_I2C(PinName sda, PinName scl, uint32_t freq)
+    : _i2c(sda, scl), _freq(freq)
+{
+    _i2c.frequency(_freq);
+}
+
+GPS_I2C::~GPS_I2C() {}
+/*****************************************************/
+bool    GPS_I2C::begin()
+{
+    int ack;
+
+    _head = 0;
+    _tail = 0;
+
+    ack = _i2c.read(MT333x_ADDR << 1, (char*)gpsData, 1);
+    //Verify if module is connected
+    if( ack != 0 ) return false;
+    else           return true;
+}
+
+void    GPS_I2C::check()
+{
+    uint8_t packetData[MAX_PACKET_SIZE];
+
+    for (uint8_t x = 0 ; x < MAX_PACKET_SIZE ; x++) {
+        packetData[x] = 0x0A; //Fill with garbage byte
+    }
+
+    _i2c.read(MT333x_ADDR << 1, ((char*)packetData), MAX_PACKET_SIZE);
+
+    for (uint8_t x = 0 ; x < MAX_PACKET_SIZE ; x++) {
+        if (packetData[x] != 0x0A) {
+            gpsData[_head++] = packetData[x];
+            if (_head == MAX_PACKET_SIZE) _head = 0; //Wrap variable
+        }
+    }
+}
+
+uint8_t GPS_I2C::available()
+{
+    //If tail=head then no new data is available in the local buffer
+    //So now check to see if the module has anything new in its buffer
+    if (_tail == _head) {
+        check(); //Check to module to see if new I2C bytes are available
+    }
+
+    //Return new data count
+    if (_head > _tail) return (_head - _tail);
+    if (_tail > _head) return (MAX_PACKET_SIZE - _tail + _head);
+    return (0); //No data available
+}
+uint8_t GPS_I2C::read()
+{
+    if (_tail != _head) {
+        uint8_t datum = gpsData[_tail++];
+        if (_tail == MAX_PACKET_SIZE) _tail = 0; //Wrap variable
+        return (datum);
+    } else
+        return (0); //No new data
+}
\ No newline at end of file
diff -r 000000000000 -r f4c7ece483fe GPS_I2C.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS_I2C.h	Fri Dec 27 15:32:37 2019 +0000
@@ -0,0 +1,39 @@
+#ifndef __GPSI2C__
+#define __GPSI2C__
+
+#include "mbed.h"
+#include <string>
+
+#define MT333x_ADDR 0x10 //7-bit unshifted default I2C Address
+
+#define MAX_PACKET_SIZE 255
+//If packet size is ever more than 255 the head and tail variables will need to be
+//changed to something other than uint8_t
+
+#define I2C_SPEED_STANDARD        100000
+#define I2C_SPEED_FAST            400000
+
+class GPS_I2C{
+ public:
+    //Constructor
+    GPS_I2C(PinName sda, PinName scl, uint32_t freq); //Init I2C interface with Pin and frequency
+    ~GPS_I2C();
+    //Methods
+    bool begin(); //Test if sensor is available
+    void check();
+    uint8_t available();
+    uint8_t read(); 
+    //Variables
+    uint8_t gpsData[MAX_PACKET_SIZE]; //The place to store valid incoming gps data
+    
+ private:
+    /*I2C config*/   
+    I2C _i2c;
+    uint32_t _freq;
+    
+    /*GPS buffer beginning/ending*/
+    uint8_t _head;
+    uint8_t _tail;
+};
+
+#endif
\ No newline at end of file