forked from lebipbip/le-BipBip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlebipbip.c
222 lines (196 loc) · 6.89 KB
/
lebipbip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
//
//
// MS5607 MSP430G2553
// sensor microcontroller
// ----------------- -----------------
// | |---MISO-->|P1.6/ |->Serial Tx (3.3V)
// | |<--CLK----| |
// | | | |<-Serial Rx (3.3V)
// | |<--MOSI-+-|P1.7 |
// | | |-INT--->| |
// | | | | VCC
// | | | | |
// | | | | 47K
// | | | P1.3|-------|
// | | | | Button
// ----------------- ----------------- |
GND
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <msp430.h>
#include <legacymsp430.h>
#include "button.h"
#include "uart.h"
#include "buzzer.h"
#include "bithacks.h"
#include "power.h"
#include "sensor.h"
#include "vario.h"
#include "oscillator.h"
#include "filter.h"
#include "data.h"
#include "charge_pump.h"
#include "target.h"
#include "sink.h"
void SerialStartOutput(void)
{
#ifdef DEBUG
UartXmitString("\r\n\tle BipBip on MSP430G2553 started\r\n");
#endif //DEBUG
}
void SerialPressureOutput(long pressure, long long int filter_output)
{
#ifdef DEBUG
char printf_buff[100];
char printf_len = 0;
//printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, " %ld", pressure_ut);
//printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, " TCo: %d.%d°C\t", (temperature/10), (temperature%10));
printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, "%ld", (long int)pressure);
printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, ",%ld", (long int)filter_output);
//printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, " Vz: %d.%02dm/s\t",(vario_diff/100), (vario_diff%100));*/
printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, " \n\r");
UartXmitString(printf_buff);
printf_len = 0;
#endif //DEBUG
}
typedef struct
{
int32_t temperature_ut;
int32_t temperature;
int32_t pressure_ut;
int32_t pressure;
}SensorValues;
void VarioInitialiseFilter(SensorValues * sensor)
{
int32_t filter_output;
int16_t init_filter = 400;
SensorStartTemperatureSampling();
sensor->temperature_ut = SensorReadTemperatureWhenAvailable();
sensor->temperature = SensorCompensateTemperature(sensor->temperature_ut);
SensorStartPressureSampling();
sensor->pressure_ut = SensorReadPressureWhenAvailable();
sensor->pressure = SensorCompensatePressure(sensor->pressure_ut);
filter_output = Filter(sensor->pressure);
while (init_filter--)
{
filter_output = Filter(sensor->pressure);
}
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
OscillatorStart();
/*
B_UNSET(P1SEL, 0); B_UNSET(P1SEL2, 0); B_SET(P1DIR, 0); B_UNSET(P1OUT, 0); // BT Command
B_UNSET(P1SEL, 1); B_UNSET(P1SEL2, 1); B_UNSET(P1DIR, 1); B_UNSET(P1OUT, 1); // RX pin
B_UNSET(P1SEL, 2); B_UNSET(P1SEL2, 2); B_UNSET(P1DIR, 2); B_UNSET(P1OUT, 2); // TX pin
B_UNSET(P1SEL, 3); B_UNSET(P1SEL2, 3); B_UNSET(P1DIR, 3); B_UNSET(P1OUT, 3); // unused
B_UNSET(P1SEL, 4); B_UNSET(P1SEL2, 4); B_UNSET(P1DIR, 4); B_UNSET(P1OUT, 4); // unused (I2C SDA fix)
B_UNSET(P1SEL, 5); B_UNSET(P1SEL2, 5); B_UNSET(P1DIR, 5); B_UNSET(P1OUT, 5); // unused (I2C SDA fix)
B_UNSET(P1SEL, 6); B_UNSET(P1SEL2, 6); B_UNSET(P1DIR, 6); B_UNSET(P1OUT, 6); // SCL
B_UNSET(P1SEL, 7); B_UNSET(P1SEL2, 7); B_UNSET(P1DIR, 7); B_UNSET(P1OUT, 7); // SDA
B_UNSET(P2SEL, 0); B_UNSET(P2SEL2, 0); B_UNSET(P2DIR, 0); B_UNSET(P2OUT, 0); // EOC
B_UNSET(P2SEL, 1); B_UNSET(P2SEL2, 1); B_SET(P2DIR, 1); B_UNSET(P2OUT, 1); // H bridge
B_UNSET(P2SEL, 2); B_UNSET(P2SEL2, 2); B_SET(P2DIR, 2); B_SET(P2OUT, 2); // BT AT
B_UNSET(P2SEL, 3); B_UNSET(P2SEL2, 3); B_SET(P2DIR, 3); B_UNSET(P2OUT, 3); // unused
B_UNSET(P2SEL, 4); B_UNSET(P2SEL2, 4); B_SET(P2DIR, 4); B_UNSET(P2OUT, 4); // H bridge
B_UNSET(P2SEL, 5); B_UNSET(P2SEL2, 5); B_UNSET(P2DIR, 5); B_UNSET(P2OUT, 5); // unused (I2C PWD fix)
B_UNSET(P2SEL, 6); B_UNSET(P2SEL2, 6); B_SET(P2DIR, 6); B_UNSET(P2OUT, 6); // Boost
B_UNSET(P2SEL, 7); B_UNSET(P2SEL2, 7); B_SET(P2DIR, 7); B_UNSET(P2OUT, 7); // unused
//PowerEnterUltraLowPower();
*/
SensorValues sensor;
int32_t filter_output = 0;
uint8_t button_state;
#ifdef SIMULTATED_DATA
int index = 0;
#endif //SIMULTATED_DATA
ButtonInit();
UartInit();
BatteryCheckInit();
BatteryCheck();
while (BatteryIsLow())
{
BatteryCheckStop();
ButtonEnableInterrupt();
PowerEnterUltraLowPower();
ButtonDisableInterrupt();
BatteryCheckInit();
BatteryCheck();
}
SerialStartOutput();
ChargePumpStart();
BuzzerResetSound();
ChargePumpDemo();
//SinkSetting();
SensorStart();
VarioInitialiseFilter(&sensor);
while(1)
{
#ifndef SENSOR_SIMULATED
SensorStartTemperatureSampling();
filter_output = Filter(sensor.pressure);
sensor.temperature = SensorCompensateTemperature(sensor.temperature_ut);
sensor.pressure = SensorCompensatePressure(sensor.pressure_ut);
sensor.temperature_ut = SensorReadTemperatureWhenAvailable();
SensorStartPressureSampling();
#else
sensor.pressure = DataSampleElevator[index];
if ((index++) == sizeof(DataSampleElevator)/sizeof(DataSampleElevator[0]))
{
index = 0;
BuzzerSetBlocking(3000,500);
}
sleep_ms(20);
#endif //SIMULTATED_DATA
#ifndef DISABLE_VARIO
if (Vario(filter_output))
{
AutoPowerOffReset();
}
#endif // DISABLE_VARIO
SerialPressureOutput(sensor.pressure, filter_output);
sensor.pressure_ut = SensorReadPressureWhenAvailable();
BatteryCheck();
button_state = Button();
// BUTTON, Low Battery and Auto-Off mangement
BuzzerSetVolume(button_state);
if (button_state!=BUTTON_NONE)
VarioDisableFor(1500);
if(button_state == BUTTON_SHORT)
{
AutoPowerOffReset();
}
else if(button_state == BUTTON_DOUBLE)
{
VarioToggleStationaryMode();
}
else if((button_state == BUTTON_LONG)||BatteryIsLow()||AutoPowerOffCheck())
{
BuzzerStopSound();
ChargePumpStop();
SensorStop();
do
{
BatteryCheckStop();
ButtonEnableInterrupt();
PowerEnterUltraLowPower();
// Button pressed here (interrupt wake up)
ButtonDisableInterrupt();
BatteryCheckInit();
BatteryCheck();
} while (BatteryIsLow());
ButtonDisableInterrupt();
SensorStart();
ChargePumpStart();
BuzzerStartSound();
VarioInitialiseFilter(&sensor);
AutoPowerOffReset();
}
BuzzerTask();
}
}