Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_module_gps.c
1 #include <bc_module_gps.h>
2 #include <bc_tca9534a.h>
3 
4 static struct
5 {
6  bc_module_gps_event_handler_t *event_handler;
7  bc_sam_m8q_driver_t sam_m8q_driver;
8  bc_sam_m8q_t sam_m8q;
9  bc_tca9534a_t tca9534;
10  void *event_param;
11 
12 } _bc_module_gps;
13 
14 static bc_tca9534a_pin_t _bc_module_gps_led_pin_lut[2] =
15 {
16  [BC_MODULE_GPS_LED_RED] = BC_TCA9534A_PIN_P3,
17  [BC_MODULE_GPS_LED_GREEN] = BC_TCA9534A_PIN_P5
18 };
19 
20 static void _bc_module_gps_led_init(bc_led_t *self);
21 static void _bc_module_gps_led_on(bc_led_t *self);
22 static void _bc_module_gps_led_off(bc_led_t *self);
23 static void _bc_module_gps_sam_m8q_event_handler(bc_sam_m8q_t *self, bc_sam_m8q_event_t event, void *event_param);
24 static bool _bc_module_gps_sam_m8q_on(bc_sam_m8q_t *self);
25 static bool _bc_module_gps_sam_m8q_off(bc_sam_m8q_t *self);
26 
28 {
29  memset(&_bc_module_gps, 0, sizeof(_bc_module_gps));
30 
31  _bc_module_gps.sam_m8q_driver.on = _bc_module_gps_sam_m8q_on;
32  _bc_module_gps.sam_m8q_driver.off = _bc_module_gps_sam_m8q_off;
33 
34  if (!bc_tca9534a_init(&_bc_module_gps.tca9534, BC_I2C_I2C0, 0x21))
35  {
36  return false;
37  }
38 
39  if (!bc_tca9534a_write_port(&_bc_module_gps.tca9534, 0))
40  {
41  return false;
42  }
43 
44  if (!bc_tca9534a_set_port_direction(&_bc_module_gps.tca9534, 0))
45  {
46  return false;
47  }
48 
49  if (!bc_tca9534a_write_pin(&_bc_module_gps.tca9534, BC_TCA9534A_PIN_P7, 1))
50  {
51  return false;
52  }
53 
54  bc_sam_m8q_init(&_bc_module_gps.sam_m8q, BC_I2C_I2C0, 0x42, &_bc_module_gps.sam_m8q_driver);
55  bc_sam_m8q_set_event_handler(&_bc_module_gps.sam_m8q, _bc_module_gps_sam_m8q_event_handler, NULL);
56 
57  return true;
58 }
59 
60 void bc_module_gps_set_event_handler(bc_module_gps_event_handler_t event_handler, void *event_param)
61 {
62  _bc_module_gps.event_handler = event_handler;
63  _bc_module_gps.event_param = event_param;
64 }
65 
67 {
68  bc_sam_m8q_start(&_bc_module_gps.sam_m8q);
69 }
70 
72 {
73  bc_sam_m8q_stop(&_bc_module_gps.sam_m8q);
74 }
75 
77 {
78  bc_sam_m8q_invalidate(&_bc_module_gps.sam_m8q);
79 }
80 
82 {
83  return bc_sam_m8q_get_time(&_bc_module_gps.sam_m8q, time);
84 }
85 
87 {
88  return bc_sam_m8q_get_position(&_bc_module_gps.sam_m8q, position);
89 }
90 
92 {
93  return bc_sam_m8q_get_altitude(&_bc_module_gps.sam_m8q, altitude);
94 }
95 
97 {
98  return bc_sam_m8q_get_quality(&_bc_module_gps.sam_m8q, quality);
99 }
100 
102 {
103  return bc_sam_m8q_get_accuracy(&_bc_module_gps.sam_m8q, accuracy);
104 }
105 
107 {
108  static const bc_led_driver_t bc_module_gps_led_driver =
109  {
110  .init = _bc_module_gps_led_init,
111  .on = _bc_module_gps_led_on,
112  .off = _bc_module_gps_led_off,
113  };
114 
115  return &bc_module_gps_led_driver;
116 }
117 
118 static void _bc_module_gps_led_init(bc_led_t *self)
119 {
120  (void) self;
121 }
122 
123 static void _bc_module_gps_led_on(bc_led_t *self)
124 {
125  if (!bc_tca9534a_write_pin(&_bc_module_gps.tca9534, _bc_module_gps_led_pin_lut[self->_channel.virtual], self->_idle_state ? 0 : 1))
126  {
127  // TODO
128  }
129 }
130 
131 static void _bc_module_gps_led_off(bc_led_t *self)
132 {
133  if (!bc_tca9534a_write_pin(&_bc_module_gps.tca9534, _bc_module_gps_led_pin_lut[self->_channel.virtual], self->_idle_state ? 1 : 0))
134  {
135  // TODO
136  }
137 }
138 
139 static void _bc_module_gps_sam_m8q_event_handler(bc_sam_m8q_t *self, bc_sam_m8q_event_t event, void *event_param)
140 {
141  (void) self;
142  (void) event_param;
143 
144  if (_bc_module_gps.event_handler == NULL)
145  {
146  return;
147  }
148 
149  if (event == BC_SAM_M8Q_EVENT_ERROR)
150  {
151  _bc_module_gps.event_handler(BC_MODULE_GPS_EVENT_ERROR, _bc_module_gps.event_param);
152  }
153  else if (event == BC_SAM_M8Q_EVENT_START)
154  {
155  _bc_module_gps.event_handler(BC_MODULE_GPS_EVENT_START, _bc_module_gps.event_param);
156  }
157  else if (event == BC_SAM_M8Q_EVENT_UPDATE)
158  {
159  _bc_module_gps.event_handler(BC_MODULE_GPS_EVENT_UPDATE, _bc_module_gps.event_param);
160  }
161  else if (event == BC_SAM_M8Q_EVENT_STOP)
162  {
163  _bc_module_gps.event_handler(BC_MODULE_GPS_EVENT_STOP, _bc_module_gps.event_param);
164  }
165 }
166 
167 static bool _bc_module_gps_sam_m8q_on(bc_sam_m8q_t *self)
168 {
169  (void) self;
170 
171  return bc_tca9534a_write_pin(&_bc_module_gps.tca9534, BC_TCA9534A_PIN_P0, 1);
172 }
173 
174 static bool _bc_module_gps_sam_m8q_off(bc_sam_m8q_t *self)
175 {
176  (void) self;
177 
178  return bc_tca9534a_write_pin(&_bc_module_gps.tca9534, BC_TCA9534A_PIN_P0, 0);
179 }
void bc_sam_m8q_init(bc_sam_m8q_t *self, bc_i2c_channel_t channel, uint8_t i2c_address, const bc_sam_m8q_driver_t *driver)
Initialize SAM-M8Q module driver.
Definition: bc_sam_m8q.c:13
bool bc_module_gps_get_quality(bc_module_gps_quality_t *quality)
Get quality.
Definition: bc_module_gps.c:96
void bc_sam_m8q_set_event_handler(bc_sam_m8q_t *self, bc_sam_m8q_event_handler_t event_handler, void *event_param)
Set callback function.
Definition: bc_sam_m8q.c:26
bool bc_sam_m8q_get_accuracy(bc_sam_m8q_t *self, bc_sam_m8q_accuracy_t *accuracy)
Get accuracy.
Definition: bc_sam_m8q.c:124
LED driver interface.
Definition: bc_led.h:56
bool bc_tca9534a_set_port_direction(bc_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
Definition: bc_tca9534a.c:87
bool bc_module_gps_get_position(bc_module_gps_position_t *position)
Get position.
Definition: bc_module_gps.c:86
bool bc_tca9534a_write_pin(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, int state)
Write pin state.
Definition: bc_tca9534a.c:61
void bc_module_gps_start(void)
Start tracking.
Definition: bc_module_gps.c:66
I2C channel I2C0.
Definition: bc_i2c.h:18
Pin state.
Definition: bc_tca9534a.h:42
void bc_sam_m8q_start(bc_sam_m8q_t *self)
Start navigation module.
Definition: bc_sam_m8q.c:32
void bc_sam_m8q_invalidate(bc_sam_m8q_t *self)
Invalidate navigation data.
Definition: bc_sam_m8q.c:53
Stop event.
Definition: bc_sam_m8q.h:25
struct bc_sam_m8q_t bc_sam_m8q_t
SAM-M8Q instance.
Definition: bc_sam_m8q.h:31
bool bc_tca9534a_write_port(bc_tca9534a_t *self, uint8_t state)
Write state to all pins.
Definition: bc_tca9534a.c:35
Quality data structure.
Definition: bc_sam_m8q.h:95
void bc_sam_m8q_stop(bc_sam_m8q_t *self)
Stop navigation module.
Definition: bc_sam_m8q.c:43
bool bc_sam_m8q_get_time(bc_sam_m8q_t *self, bc_sam_m8q_time_t *time)
Get time.
Definition: bc_sam_m8q.c:60
struct bc_led_t bc_led_t
LED instance.
Definition: bc_led.h:52
void bc_module_gps_stop(void)
Stop tracking.
Definition: bc_module_gps.c:71
void bc_module_gps_set_event_handler(bc_module_gps_event_handler_t event_handler, void *event_param)
Set callback function.
Definition: bc_module_gps.c:60
LCD red LED channel.
Definition: bc_module_gps.h:34
const bc_led_driver_t * bc_module_gps_get_led_driver(void)
Get LED driver.
bool bc_sam_m8q_get_altitude(bc_sam_m8q_t *self, bc_sam_m8q_altitude_t *altitude)
Get altitude.
Definition: bc_sam_m8q.c:94
Accuracy data structure.
Definition: bc_sam_m8q.h:107
bool bc_tca9534a_init(bc_tca9534a_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
Definition: bc_tca9534a.c:8
bool bc_sam_m8q_get_position(bc_sam_m8q_t *self, bc_sam_m8q_position_t *position)
Get position.
Definition: bc_sam_m8q.c:79
void(* init)(bc_led_t *self)
Callback for initialization.
Definition: bc_led.h:59
void bc_module_gps_invalidate(void)
Invalidate navigation data.
Definition: bc_module_gps.c:76
bool bc_module_gps_get_accuracy(bc_module_gps_accuracy_t *accuracy)
Get accuracy.
bc_tca9534a_pin_t
Individual pin names.
Definition: bc_tca9534a.h:16
Update event.
Definition: bc_sam_m8q.h:22
Position data structure.
Definition: bc_sam_m8q.h:71
Error event.
Definition: bc_sam_m8q.h:16
Time data structure.
Definition: bc_sam_m8q.h:47
Start event.
Definition: bc_sam_m8q.h:19
bool bc_module_gps_init(void)
Initialize BigClown GPS Module.
Definition: bc_module_gps.c:27
Altitude data structure.
Definition: bc_sam_m8q.h:83
bool bc_module_gps_get_altitude(bc_module_gps_altitude_t *altitude)
Get altitude.
Definition: bc_module_gps.c:91
bc_sam_m8q_event_t
Callback events.
Definition: bc_sam_m8q.h:13
bool bc_sam_m8q_get_quality(bc_sam_m8q_t *self, bc_sam_m8q_quality_t *quality)
Get quality.
Definition: bc_sam_m8q.c:109
bool bc_module_gps_get_time(bc_module_gps_time_t *time)
Get time.
Definition: bc_module_gps.c:81
SAM-M8Q driver.
Definition: bc_sam_m8q.h:35
LCD green LED channel.
Definition: bc_module_gps.h:37