]> git.karo-electronics.de Git - oswald.git/blob - metawatch/oswald_hal.c
Make accelerometer work, interrup driven tilt change mode
[oswald.git] / metawatch / oswald_hal.c
1 /*
2  * Adaptation to Oswald
3  */
4 #include <msp430.h>
5 #include <msp430xgeneric.h>
6 #include <stdint.h>
7 #include <string.h>
8
9 #include "mw_main.h"
10
11 #include "mw_lcd.h"
12 #include "mw_adc.h"
13 #include "mw_bt.h"
14 #include "bt_hci.h"
15 #include "bt_l2cap.h"
16 #include "bluetooth_init_cc256x.h"
17 #include "mw_acc.h"
18
19 #include "oswald.h"
20 #include "oswald_hal.h"
21
22 #include "calendar.h"
23
24
25 const char *hal_get_version_string(void)
26 {
27         return MW_MAIN_VERSION;
28 }
29
30 const char *hal_get_buildno_string(void)
31 {
32         return BUILDNO;
33 }
34
35 const char *hal_get_radio_version_string(void)
36 {
37         return cc256x_version;
38 }
39
40 void hal_lcd_set_pixel(uint8_t x, uint8_t y, uint8_t color)
41 {
42         x %= 96;
43         y %= 96;
44         mw_lcd_draw_pixel(x, y, color ? LCD_BLACK : LCD_WHITE);
45 }
46
47 void hal_lcd_clear_display(void)
48 {
49         mw_lcd_clear_fb();
50 }
51
52 void hal_lcd_update_display(void)
53 {
54         mw_lcd_update_screen();
55 }
56
57 void hal_lcd_set_backlight(boolean state)
58 {
59         if (state) {
60                 ENABLE_LCD_LED();
61         } else {
62                 DISABLE_LCD_LED();
63         }
64 }
65
66 boolean hal_lcd_get_backlight(void)
67 {
68         return (LCD_LED_POUT & LCD_LED_PIN) ? TRUE : FALSE;
69 }
70
71 void hal_enable_centisecond_timer(void)
72 {
73         start_timer(TIMER_100MS_CYCLES);
74 }
75
76 void hal_disable_centisecond_timer(void)
77 {
78         stop_timer();
79 }
80
81 void hal_enable_halfsecond_timer(void)
82 {
83         start_timer(TIMER_500MS_CYCLES);
84 }
85
86 void hal_disable_halfsecond_timer(void)
87 {
88         stop_timer();
89 }
90
91 void hal_get_rtc(clock_state *rtc)
92 {
93         /* Update clock state from RTC */
94         rtc->hour = RTCHOUR;
95         rtc->minute = RTCMIN;
96         rtc->second = RTCSEC;
97         rtc->day = RTCDAY;
98         rtc->month = RTCMON;
99         rtc->year = RTCYEAR;
100 }
101
102 void hal_set_rtc(clock_state *rtc, boolean set_sec)
103 {
104         /* Update clock state from RTC */
105         RTCHOUR = rtc->hour;
106         RTCMIN = rtc->minute;
107         if (set_sec)
108                 RTCSEC = rtc->second;
109         RTCDAY = rtc->day;
110         RTCMON = rtc->month;
111         RTCYEAR = rtc->year;
112         rtc->wday = getWochentag(rtc->day, rtc->month, rtc->year);
113         RTCDOW = rtc->wday;
114 }
115
116 void hal_get_power_state(power_state *pwr)
117 {
118         unsigned int val;
119
120         pwr->source = (BAT_CHARGE_IN & BAT_CHARGE_PWR_BIT) ? POWER_SOURCE_BATTERY : POWER_SOURCE_EXTERNAL;
121
122         /* unless the charger is enabled we do not get a reasonable state */
123         if (!(BAT_CHARGE_IN & BAT_CHARGE_ENABLE_PIN)) {
124                 switch (BAT_CHARGE_IN & (BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2)) {
125                         case BAT_CHARGE_STAT1:
126                                 pwr->charge_state = POWER_CHARGER_DONE;
127                                 break;
128                         case BAT_CHARGE_STAT2:
129                                 pwr->charge_state = POWER_CHARGER_CHARGING;
130                                 break;
131                         case (BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2):
132                                 pwr->charge_state = POWER_CHARGER_UNK;
133                                 break;
134                         default:
135                                 pwr->charge_state = POWER_CHARGER_PRECHARGE;
136                                 break;
137                 }
138         } else {
139                 pwr->charge_state = POWER_CHARGER_UNK;
140         }
141
142         if ((pwr->source == POWER_SOURCE_BATTERY) && (RTCSEC != 0)) {
143                 /* the ADC and activating the measuring shunts is
144                  * power expensive so only do this every minute */
145                 return;
146         };
147
148         /* get new values and so some averaging to avoid jumps */
149         val = mw_get_battery_adc_val();
150         pwr->percent = mw_get_battery_percentage_from_val(val);
151         pwr->level = val;
152 }
153
154 void hal_vibration_set_state(boolean state)
155 {
156 #ifdef MW_DIGITAL_V2
157         if (state) {
158                 TA1CTL |= TASSEL__ACLK | MC__UPDOWN | ID_0;
159                 P7SEL |= BIT3;
160         } else {
161                 TA1CTL = 0;
162                 P7SEL &= ~BIT3;
163         }
164 #endif
165 }
166
167 boolean hal_vibration_get_state(void)
168 {
169 #ifdef MW_DIGITAL_V2
170         return (P7SEL & BIT3) ? TRUE : FALSE;
171 #else
172         return FALSE;
173 #endif
174 }
175
176 #define BLUETOOTH_DEVICE_NAME "Oswald on MetaWatch"
177 static boolean bt_is_visible = FALSE;
178
179
180 bluetooth_state hal_bluetooth_set_state(bluetooth_state state)
181 {
182         uint8_t buf[32];
183
184         if (state == BLUETOOTH_OFF && mw_bt_is_enabled() == 1) {
185                 mw_disable_bt();
186                 bt_is_visible = FALSE;
187                 return BLUETOOTH_OFF;
188         } else if (state == BLUETOOTH_ON && mw_bt_is_enabled() == 0) {
189                 mw_enable_bt();
190                 // set our name
191                 memset(buf, 0, 32);
192                 strncpy((char *)buf, BLUETOOTH_DEVICE_NAME, strlen(BLUETOOTH_DEVICE_NAME));
193                 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_LOCAL_NAME_OCF, strlen(BLUETOOTH_DEVICE_NAME)+1, buf);
194                 // read our local address
195                 bt_hci_cmd(HCI_INFO_PARAM_OGF, HCI_R_BD_ADDR_OCF, 0, NULL);
196                 // enable page scan
197                 buf[0] = HCI_BB_SCAN_PAGE;
198                 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_SCAN_EN_OCF, 1, buf);
199                 bt_is_visible = FALSE;
200                 return BLUETOOTH_ON;
201         } else
202                 return BLUETOOTH_ILL;
203 }
204
205 bluetooth_state hal_bluetooth_get_state(void)
206 {
207         if (mw_bt_is_enabled() == 1) {
208                 if (bt_l2cap_get_connected(0x40))
209                         return BLUETOOTH_CONNECTED;
210                 else
211                         return BLUETOOTH_ON;
212         } else
213                 return BLUETOOTH_OFF;
214 }
215
216 uint8_t *hal_bluetooth_get_local_bdaddr(void)
217 {
218         return bt_hci_get_local_bdaddr();
219 }
220
221 void hal_bluetooth_set_visible(boolean visible)
222 {
223         uint8_t buf[2];
224
225         if (mw_bt_is_enabled() == 0) {
226                 bt_is_visible = FALSE;
227                 return;
228         }
229
230         if (visible) {
231                 // enable page and inquiry scan
232                 buf[0] = HCI_BB_SCAN_INQUIRY | HCI_BB_SCAN_PAGE;
233                 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_SCAN_EN_OCF, 1, buf);
234                 bt_is_visible = TRUE;
235         } else {
236                 // enable page scan only
237                 buf[0] = HCI_BB_SCAN_PAGE;
238                 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_SCAN_EN_OCF, 1, buf);
239                 bt_is_visible = FALSE;
240         }
241 }
242
243 boolean hal_bluetooth_get_visible(void)
244 {
245         return bt_is_visible;
246 }
247
248 void hal_bluetooth_send_data(const void *mdat, uint16_t mlen)
249 {
250         bt_l2cap_send_channel(0x40, mdat, mlen);
251 }
252
253 /*
254  * Control the accelerometer
255  */
256 void hal_accelerometer_enable(void)
257 {
258         mw_acc_enable();
259 }
260
261 void hal_accelerometer_disable(void)
262 {
263         mw_acc_disable();
264 }
265
266