Firmware SDK
twr_module_gps.c
1 #include <twr_module_gps.h>
2 #include <twr_tca9534a.h>
3 
4 static struct
5 {
6  twr_module_gps_event_handler_t *event_handler;
7  twr_sam_m8q_driver_t sam_m8q_driver;
8  twr_sam_m8q_t sam_m8q;
9  twr_tca9534a_t tca9534;
10  void *event_param;
11 
12 } _twr_module_gps;
13 
14 static twr_tca9534a_pin_t _twr_module_gps_led_pin_lut[2] =
15 {
16  [TWR_MODULE_GPS_LED_RED] = TWR_TCA9534A_PIN_P3,
17  [TWR_MODULE_GPS_LED_GREEN] = TWR_TCA9534A_PIN_P5
18 };
19 
20 static void _twr_module_gps_led_init(twr_led_t *self);
21 static void _twr_module_gps_led_on(twr_led_t *self);
22 static void _twr_module_gps_led_off(twr_led_t *self);
23 static void _twr_module_gps_sam_m8q_event_handler(twr_sam_m8q_t *self, twr_sam_m8q_event_t event, void *event_param);
24 static bool _twr_module_gps_sam_m8q_on(twr_sam_m8q_t *self);
25 static bool _twr_module_gps_sam_m8q_off(twr_sam_m8q_t *self);
26 
28 {
29  memset(&_twr_module_gps, 0, sizeof(_twr_module_gps));
30 
31  _twr_module_gps.sam_m8q_driver.on = _twr_module_gps_sam_m8q_on;
32  _twr_module_gps.sam_m8q_driver.off = _twr_module_gps_sam_m8q_off;
33 
34  if (!twr_tca9534a_init(&_twr_module_gps.tca9534, TWR_I2C_I2C0, 0x21))
35  {
36  return false;
37  }
38 
39  if (!twr_tca9534a_write_port(&_twr_module_gps.tca9534, 0))
40  {
41  return false;
42  }
43 
44  if (!twr_tca9534a_set_port_direction(&_twr_module_gps.tca9534, 0))
45  {
46  return false;
47  }
48 
49  if (!twr_tca9534a_write_pin(&_twr_module_gps.tca9534, TWR_TCA9534A_PIN_P7, 1))
50  {
51  return false;
52  }
53 
54  twr_sam_m8q_init(&_twr_module_gps.sam_m8q, TWR_I2C_I2C0, 0x42, &_twr_module_gps.sam_m8q_driver);
55  twr_sam_m8q_set_event_handler(&_twr_module_gps.sam_m8q, _twr_module_gps_sam_m8q_event_handler, NULL);
56 
57  return true;
58 }
59 
60 void twr_module_gps_set_event_handler(twr_module_gps_event_handler_t event_handler, void *event_param)
61 {
62  _twr_module_gps.event_handler = event_handler;
63  _twr_module_gps.event_param = event_param;
64 }
65 
67 {
68  twr_sam_m8q_start(&_twr_module_gps.sam_m8q);
69 }
70 
72 {
73  twr_sam_m8q_stop(&_twr_module_gps.sam_m8q);
74 }
75 
77 {
78  twr_sam_m8q_invalidate(&_twr_module_gps.sam_m8q);
79 }
80 
82 {
83  return twr_sam_m8q_get_time(&_twr_module_gps.sam_m8q, time);
84 }
85 
87 {
88  return twr_sam_m8q_get_position(&_twr_module_gps.sam_m8q, position);
89 }
90 
92 {
93  return twr_sam_m8q_get_altitude(&_twr_module_gps.sam_m8q, altitude);
94 }
95 
97 {
98  return twr_sam_m8q_get_quality(&_twr_module_gps.sam_m8q, quality);
99 }
100 
102 {
103  return twr_sam_m8q_get_accuracy(&_twr_module_gps.sam_m8q, accuracy);
104 }
105 
107 {
108  static const twr_led_driver_t twr_module_gps_led_driver =
109  {
110  .init = _twr_module_gps_led_init,
111  .on = _twr_module_gps_led_on,
112  .off = _twr_module_gps_led_off,
113  };
114 
115  return &twr_module_gps_led_driver;
116 }
117 
118 static void _twr_module_gps_led_init(twr_led_t *self)
119 {
120  (void) self;
121 }
122 
123 static void _twr_module_gps_led_on(twr_led_t *self)
124 {
125  if (!twr_tca9534a_write_pin(&_twr_module_gps.tca9534, _twr_module_gps_led_pin_lut[self->_channel.virtual], self->_idle_state ? 0 : 1))
126  {
127  // TODO
128  }
129 }
130 
131 static void _twr_module_gps_led_off(twr_led_t *self)
132 {
133  if (!twr_tca9534a_write_pin(&_twr_module_gps.tca9534, _twr_module_gps_led_pin_lut[self->_channel.virtual], self->_idle_state ? 1 : 0))
134  {
135  // TODO
136  }
137 }
138 
139 static void _twr_module_gps_sam_m8q_event_handler(twr_sam_m8q_t *self, twr_sam_m8q_event_t event, void *event_param)
140 {
141  (void) self;
142  (void) event_param;
143 
144  if (_twr_module_gps.event_handler == NULL)
145  {
146  return;
147  }
148 
149  if (event == TWR_SAM_M8Q_EVENT_ERROR)
150  {
151  _twr_module_gps.event_handler(TWR_MODULE_GPS_EVENT_ERROR, _twr_module_gps.event_param);
152  }
153  else if (event == TWR_SAM_M8Q_EVENT_START)
154  {
155  _twr_module_gps.event_handler(TWR_MODULE_GPS_EVENT_START, _twr_module_gps.event_param);
156  }
157  else if (event == TWR_SAM_M8Q_EVENT_UPDATE)
158  {
159  _twr_module_gps.event_handler(TWR_MODULE_GPS_EVENT_UPDATE, _twr_module_gps.event_param);
160  }
161  else if (event == TWR_SAM_M8Q_EVENT_STOP)
162  {
163  _twr_module_gps.event_handler(TWR_MODULE_GPS_EVENT_STOP, _twr_module_gps.event_param);
164  }
165 }
166 
167 static bool _twr_module_gps_sam_m8q_on(twr_sam_m8q_t *self)
168 {
169  (void) self;
170 
171  return twr_tca9534a_write_pin(&_twr_module_gps.tca9534, TWR_TCA9534A_PIN_P0, 1);
172 }
173 
174 static bool _twr_module_gps_sam_m8q_off(twr_sam_m8q_t *self)
175 {
176  (void) self;
177 
178  return twr_tca9534a_write_pin(&_twr_module_gps.tca9534, TWR_TCA9534A_PIN_P0, 0);
179 }
const twr_led_driver_t * twr_module_gps_get_led_driver(void)
Get LED driver.
void twr_sam_m8q_init(twr_sam_m8q_t *self, twr_i2c_channel_t channel, uint8_t i2c_address, const twr_sam_m8q_driver_t *driver)
Initialize SAM-M8Q module driver.
Definition: twr_sam_m8q.c:13
bool twr_module_gps_get_position(twr_module_gps_position_t *position)
Get position.
LCD green LED channel.
bool twr_module_gps_get_time(twr_module_gps_time_t *time)
Get time.
bool twr_sam_m8q_get_altitude(twr_sam_m8q_t *self, twr_sam_m8q_altitude_t *altitude)
Get altitude.
Definition: twr_sam_m8q.c:94
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
Definition: twr_tca9534a.c:35
void twr_sam_m8q_start(twr_sam_m8q_t *self)
Start navigation module.
Definition: twr_sam_m8q.c:32
void twr_module_gps_start(void)
Start tracking.
void twr_module_gps_invalidate(void)
Invalidate navigation data.
twr_sam_m8q_event_t
Callback events.
Definition: twr_sam_m8q.h:13
Position data structure.
Definition: twr_sam_m8q.h:71
struct twr_led_t twr_led_t
LED instance.
Definition: twr_led.h:52
void twr_sam_m8q_stop(twr_sam_m8q_t *self)
Stop navigation module.
Definition: twr_sam_m8q.c:43
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
Definition: twr_tca9534a.c:8
SAM-M8Q driver.
Definition: twr_sam_m8q.h:35
bool twr_tca9534a_write_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int state)
Write pin state.
Definition: twr_tca9534a.c:61
void twr_sam_m8q_invalidate(twr_sam_m8q_t *self)
Invalidate navigation data.
Definition: twr_sam_m8q.c:53
Accuracy data structure.
Definition: twr_sam_m8q.h:107
Quality data structure.
Definition: twr_sam_m8q.h:95
bool twr_module_gps_get_altitude(twr_module_gps_altitude_t *altitude)
Get altitude.
bool twr_sam_m8q_get_quality(twr_sam_m8q_t *self, twr_sam_m8q_quality_t *quality)
Get quality.
Definition: twr_sam_m8q.c:109
LCD red LED channel.
void twr_sam_m8q_set_event_handler(twr_sam_m8q_t *self, twr_sam_m8q_event_handler_t event_handler, void *event_param)
Set callback function.
Definition: twr_sam_m8q.c:26
void twr_module_gps_set_event_handler(twr_module_gps_event_handler_t event_handler, void *event_param)
Set callback function.
Time data structure.
Definition: twr_sam_m8q.h:47
void(* init)(twr_led_t *self)
Callback for initialization.
Definition: twr_led.h:59
bool twr_module_gps_get_accuracy(twr_module_gps_accuracy_t *accuracy)
Get accuracy.
Pin state.
Definition: twr_tca9534a.h:42
bool twr_module_gps_init(void)
Initialize HARDWARIO GPS Module.
bool twr_sam_m8q_get_accuracy(twr_sam_m8q_t *self, twr_sam_m8q_accuracy_t *accuracy)
Get accuracy.
Definition: twr_sam_m8q.c:124
LED driver interface.
Definition: twr_led.h:56
twr_tca9534a_pin_t
Individual pin names.
Definition: twr_tca9534a.h:16
I2C channel I2C0.
Definition: twr_i2c.h:18
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
Definition: twr_tca9534a.c:87
void twr_module_gps_stop(void)
Stop tracking.
struct twr_sam_m8q_t twr_sam_m8q_t
SAM-M8Q instance.
Definition: twr_sam_m8q.h:31
Altitude data structure.
Definition: twr_sam_m8q.h:83
bool twr_sam_m8q_get_time(twr_sam_m8q_t *self, twr_sam_m8q_time_t *time)
Get time.
Definition: twr_sam_m8q.c:60
bool twr_module_gps_get_quality(twr_module_gps_quality_t *quality)
Get quality.
bool twr_sam_m8q_get_position(twr_sam_m8q_t *self, twr_sam_m8q_position_t *position)
Get position.
Definition: twr_sam_m8q.c:79