root / tidsense / audio_power.c @ 166:1a6a85e8d2e7
History | View | Annotate | Download (3.4 kB)
1 | #include "scheduler.h" |
---|---|
2 | #include "report.h" |
3 | #include "audio_power_def.h" |
4 | #include "audio_power.h" |
5 | |
6 | i2c_bus_t *power_bus; |
7 | sched_task_t *audio_power_task; |
8 | |
9 | /* static definitions */
|
10 | static void audio_power_task_fn(void); |
11 | |
12 | bool audio_power_setup(i2c_bus_t *b) {
|
13 | power_bus = b; |
14 | |
15 | if(!audio_power_probe())
|
16 | return false; |
17 | |
18 | audio_power_task = sched_task_create(audio_power_task_fn, 30, 0, |
19 | SCHED_ENABLED); |
20 | return true; |
21 | } |
22 | |
23 | bool audio_power_probe(void) { |
24 | uint8_t data; |
25 | |
26 | i2c_error_t status = i2c_read_generic(power_bus, AUDIO_POWER_DEVICE, 0x00,
|
27 | &data, 1);
|
28 | if(status != I2C_SUCCESS)
|
29 | return false; |
30 | if(data != AUDIO_POWER_SIG)
|
31 | return false; |
32 | return true; |
33 | } |
34 | |
35 | static void audio_power_task_fn(void) { |
36 | audio_power_lipo_data_t lipo; |
37 | audio_power_mppt_data_t mppt; |
38 | report_t report; |
39 | |
40 | if(!report_start(&report, AUDIO_POWER_NAMESPACE))
|
41 | return;
|
42 | |
43 | /* read lipo status */
|
44 | i2c_error_t status = i2c_read_generic(power_bus, AUDIO_POWER_DEVICE, |
45 | AUDIO_POWER_LIPO_REG, &lipo, sizeof(lipo));
|
46 | if(status == I2C_SUCCESS) {
|
47 | report_add_payload(&report, |
48 | AUDIO_POWER_LIPO_CHARGE_CAPACITY | SENSOR_SIZE_16BIT | SENSOR_SIGNED, |
49 | &lipo.charge_capacity); |
50 | report_add_payload(&report, |
51 | AUDIO_POWER_LIPO_CHARGE | SENSOR_SIZE_16BIT | SENSOR_SIGNED, |
52 | &lipo.charge); |
53 | report_add_payload(&report, |
54 | AUDIO_POWER_LIPO_CURRENT | SENSOR_SIZE_16BIT | SENSOR_SIGNED, |
55 | &lipo.current); |
56 | report_add_payload(&report, |
57 | AUDIO_POWER_LIPO_VOLTAGE | SENSOR_SIZE_16BIT | SENSOR_SIGNED, |
58 | &lipo.voltage); |
59 | report_add_payload(&report, |
60 | AUDIO_POWER_LIPO_STATE | SENSOR_SIZE_8BIT, |
61 | &lipo.state); |
62 | } |
63 | |
64 | /* check mppt validity, read status */
|
65 | uint8_t mppt_status; |
66 | status = i2c_read_generic(power_bus, AUDIO_POWER_DEVICE, |
67 | AUDIO_POWER_MPPT_STATUS_REG, &mppt_status, 1);
|
68 | if(status == I2C_SUCCESS && mppt_status == 0) { |
69 | status = i2c_read_generic(power_bus, AUDIO_POWER_DEVICE, |
70 | AUDIO_POWER_MPPT_REG, &mppt, sizeof(mppt));
|
71 | if(status == I2C_SUCCESS) {
|
72 | report_add_payload(&report, |
73 | AUDIO_POWER_SLA_VOLTAGE | SENSOR_SIZE_16BIT, |
74 | &mppt.sla_voltage); |
75 | report_add_payload(&report, |
76 | AUDIO_POWER_PV_VOLTAGE | SENSOR_SIZE_16BIT, |
77 | &mppt.pv_voltage); |
78 | report_add_payload(&report, |
79 | AUDIO_POWER_SLA_LOAD_CURRENT | SENSOR_SIZE_16BIT, |
80 | &mppt.load_current); |
81 | report_add_payload(&report, |
82 | AUDIO_POWER_SLA_OVERDISCHARGE_VOLTAGE | SENSOR_SIZE_16BIT, |
83 | &mppt.overdischarge_voltage); |
84 | report_add_payload(&report, |
85 | AUDIO_POWER_SLA_FULL_VOLTAGE | SENSOR_SIZE_16BIT, |
86 | &mppt.full_voltage); |
87 | report_add_payload(&report, |
88 | AUDIO_POWER_SLA_FLAGS | SENSOR_SIZE_8BIT, |
89 | &mppt.flags); |
90 | report_add_payload(&report, |
91 | AUDIO_POWER_SLA_TEMPERATURE | SENSOR_SIZE_8BIT, |
92 | &mppt.temperature); |
93 | report_add_payload(&report, |
94 | AUDIO_POWER_SLA_CHARGE_CURRENT | SENSOR_SIZE_16BIT, |
95 | &mppt.charge_current); |
96 | report_add_payload(&report, |
97 | AUDIO_POWER_SLA_CHARGE | SENSOR_SIZE_16BIT | SENSOR_SIGNED, |
98 | &mppt.charge); |
99 | } |
100 | } |
101 | |
102 | report_send(&report); |
103 | } |
104 |