Browse code

Maybe a little sniff mode, add ambient light adc (not working), fix accelerometer display orientation

Nils Faerber authored on 06/05/2013 23:39:18
Showing 8 changed files
... ...
@@ -435,6 +435,27 @@ void bt_hci_cmd(const uint8_t OGF, const uint8_t OCF, const uint8_t data_len, co
435 435
 		mw_bt_uart_tx(data, data_len);
436 436
 }
437 437
 
438
+typedef struct {
439
+	uint16_t acl_handle;
440
+	uint16_t max_interval;
441
+	uint16_t min_interval;
442
+	uint16_t sniff_attempt;
443
+	uint16_t sniff_timeout;
444
+} __attribute__((packed)) bt_hci_sniff_cmd_t;
445
+
446
+void bt_hci_set_sniff_mode(const uint16_t acl_handle, const uint16_t max_interval, const uint16_t min_interval, const uint16_t sniff_attempt, const uint16_t sniff_timeout)
447
+{
448
+	bt_hci_sniff_cmd_t sniff_cmd;
449
+
450
+	sniff_cmd.acl_handle = acl_handle;
451
+	sniff_cmd.max_interval = max_interval;
452
+	sniff_cmd.min_interval = min_interval;
453
+	sniff_cmd.sniff_attempt = sniff_attempt;
454
+	sniff_cmd.sniff_timeout = sniff_timeout;
455
+	
456
+	bt_hci_cmd(HCI_LINK_POLICY_OGF, HCI_SNIFF_MODE_OCF, sizeof(sniff_cmd), &sniff_cmd);
457
+}
458
+
438 459
 typedef struct {
439 460
 	uint8_t type;
440 461
 	uint16_t handle;
... ...
@@ -23,6 +23,7 @@
23 23
 #define HCI_R_STORED_LINK_KEY_OCF	0x0D
24 24
 #define HCI_W_STORED_LINK_KEY_OCF	0x11
25 25
 #define HCI_LINK_KEY_REQ_REP_OCF	0x0B
26
+#define HCI_SNIFF_MODE_OCF		0x03
26 27
 
27 28
 #define HCI_COMMAND_PACKET		0x01
28 29
 #define HCI_ACL_DATA_PACKET		0x02
... ...
@@ -87,8 +88,8 @@
87 88
 
88 89
 void bt_hci_init(void);
89 90
 void bt_hci_cmd(const uint8_t OGF, const uint8_t OCF, const uint8_t data_len, const void *data);
91
+void bt_hci_set_sniff_mode(const uint16_t acl_handle, const uint16_t max_interval, const uint16_t min_interval, const uint16_t sniff_attempt, const uint16_t sniff_timeout);
90 92
 void bt_acl_send(const uint16_t handle, const uint8_t PB, const uint8_t BC, const uint16_t channel, const uint16_t len, const void *dat);
91 93
 uint8_t *bt_hci_get_local_bdaddr(void);
92 94
 void bt_hci_ehcill_wake(void);
93 95
 #endif
94
-
... ...
@@ -224,7 +224,8 @@ void bt_l2cap_handle_connection_request(const uint16_t handle, const uint8_t ide
224 224
 {
225 225
 	bt_l2cap_conn_resp_t resp;
226 226
 
227
-	if (_l2cap_con.cstate == BT_L2CAP_CON_IDLE) {
227
+	// for now we only support one connection, only on PSM 0x1001
228
+	if (_l2cap_con.cstate == BT_L2CAP_CON_IDLE && PSM == 0x1001) {
228 229
 		bt_l2cap_conf_req_t req;
229 230
 
230 231
 		_l2cap_con.cstate = BT_L2CAP_CON_CONNECTED;
... ...
@@ -258,6 +259,12 @@ void bt_l2cap_handle_connection_request(const uint16_t handle, const uint8_t ide
258 259
 		req.odat = _l2cap_con.locMTU;
259 260
 
260 261
 		bt_acl_send(handle, PB_FIRST_FLUSHABLE, BC_NO_BROADCAST, L2CAP_CID_SIGNALING, sizeof(bt_l2cap_conf_req_t), &req);
262
+
263
+		// max_interval Mandatory Range: 0x0006 to 0x0540
264
+		// min_interval Mandatory Range: 0x0006 to 0x0540
265
+		// sniff_attempt Mandatory Range for Controller: 1 to Tsniff/2
266
+		// sniff_timeout Mandatory Range for Controller: 0 to 0x0028
267
+		bt_hci_set_sniff_mode(handle, 0x10, 0x06, 0x20, 0x10);
261 268
 	} else {
262 269
 		resp.resp = CONNECTION_RESPONSE;
263 270
 		resp.ident = ident;
... ...
@@ -277,7 +277,7 @@ void mw_acc_init(void)
277 277
     
278 278
 	/* this causes data to always be sent */
279 279
 	// WriteRegisterData = 0x00;
280
-	WriteRegisterData = 0x02 /*0x08*/;
280
+	WriteRegisterData = 0x01 /*0x08*/;
281 281
 	mw_acc_i2c_write(KIONIX_WUF_THRESH, &WriteRegisterData, 1);
282 282
      
283 283
 	/* single byte read test */
... ...
@@ -58,17 +58,18 @@ unsigned int mw_get_battery_adc_val(void)
58 58
 	ENABLE_ADC();
59 59
 
60 60
 	/* WaitForAdcBusy(); */
61
-	while (ADC12CTL1 & ADC12BUSY);
61
+	while (ADC12CTL1 & ADC12BUSY)
62
+		nop();
62 63
 
63
-	/* Convert the ADC count for the battery input into a voltage 
64
+	/* Convert the ADC count for the battery input into a voltage
64 65
 	 * ADC12MEM1: Counts Battery Voltage in ADC counts
65 66
 	 * Result: Battery voltage in millivolts */
66 67
 	BATTERY_SENSE_DISABLE();
67 68
 
68
-	return (unsigned int)(CONVERSION_FACTOR_BATTERY * (double)ADC12MEM1);  
69
+	return (unsigned int)(CONVERSION_FACTOR_BATTERY * (double)ADC12MEM1);
69 70
 }
70 71
 
71
-unsigned char mw_get_battery_percentage_from_val(unsigned int BattVal)
72
+uint8_t mw_get_battery_percentage_from_val(unsigned int BattVal)
72 73
 {
73 74
 	if (BattVal > BATTERY_FULL_LEVEL)
74 75
 		BattVal = BATTERY_FULL_LEVEL;
... ...
@@ -81,3 +82,20 @@ unsigned char mw_get_battery_percentage_from_val(unsigned int BattVal)
81 82
 	return (unsigned char)BattVal;
82 83
 }
83 84
 
85
+unsigned int mw_get_amblight_adc_val(void)
86
+{ 
87
+	LIGHT_SENSE_ENABLE();
88
+
89
+	CLEAR_START_ADDR();
90
+	ADC12CTL1 |= ADC12CSTARTADD_2;
91
+	ENABLE_ADC();
92
+
93
+	/* WaitForAdcBusy(); */
94
+	while (ADC12CTL1 & ADC12BUSY)
95
+		nop();
96
+
97
+	LIGHT_SENSE_DISABLE();
98
+
99
+	return ADC12MEM2;
100
+}
101
+
... ...
@@ -3,7 +3,8 @@
3 3
 
4 4
 void mw_init_adc(void);
5 5
 unsigned int mw_get_battery_adc_val(void);
6
-unsigned char mw_get_battery_percentage_from_val(unsigned int BatVal);
6
+uint8_t mw_get_battery_percentage_from_val(unsigned int BatVal);
7
+unsigned int mw_get_amblight_adc_val(void);
7 8
 
8 9
 #endif
9 10
 
... ...
@@ -337,6 +337,11 @@ static void handle_uart_rx_event(void)
337 337
 			mw_acc_read(&x, &y, &z);
338 338
 			snprintf(tstr, 64, "x:%d y:%d z:%d\n", x,y,z);
339 339
 			debug_uart_tx(tstr);
340
+		} else if (c =='R') {
341
+			int16_t al;
342
+			al = mw_get_amblight_adc_val();
343
+			snprintf(tstr, 64, "light: %d\n", al);
344
+			debug_uart_tx(tstr);
340 345
 		} else if (c == 'b') {
341 346
 			debug_uart_tx("\nenabling BT\n");
342 347
 			mw_enable_bt();
... ...
@@ -165,7 +165,7 @@ void draw_accel_screen(accel_data_t *accel_data)
165 165
 	oswald_draw_line(40, 82, 40, 30);
166 166
 
167 167
 	x = 41+25+((accel_data->x * 50) / (254));
168
-	y = 31+25+((accel_data->y * 50) / (254));
168
+	y = 31+25-((accel_data->y * 50) / (254));
169 169
 	oswald_draw_pixel(x, y);
170 170
 	oswald_draw_pixel(x+1, y);
171 171
 	oswald_draw_pixel(x-1, y);