Maybe a little sniff mode, add ambient light adc (not working),
authorNils Faerber <nils.faerber@kernelconcepts.de>
Mon, 6 May 2013 23:39:18 +0000 (01:39 +0200)
committerNils Faerber <nils.faerber@kernelconcepts.de>
Mon, 6 May 2013 23:39:18 +0000 (01:39 +0200)
fix accelerometer display orientation

metawatch/bt_hci.c
metawatch/bt_hci.h
metawatch/bt_l2cap.c
metawatch/mw_acc.c
metawatch/mw_adc.c
metawatch/mw_adc.h
metawatch/mw_main.c
ui/oswald_screens.c

index 38ffb7f..838aafc 100644 (file)
@@ -435,6 +435,27 @@ void bt_hci_cmd(const uint8_t OGF, const uint8_t OCF, const uint8_t data_len, co
                mw_bt_uart_tx(data, data_len);
 }
 
+typedef struct {
+       uint16_t acl_handle;
+       uint16_t max_interval;
+       uint16_t min_interval;
+       uint16_t sniff_attempt;
+       uint16_t sniff_timeout;
+} __attribute__((packed)) bt_hci_sniff_cmd_t;
+
+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)
+{
+       bt_hci_sniff_cmd_t sniff_cmd;
+
+       sniff_cmd.acl_handle = acl_handle;
+       sniff_cmd.max_interval = max_interval;
+       sniff_cmd.min_interval = min_interval;
+       sniff_cmd.sniff_attempt = sniff_attempt;
+       sniff_cmd.sniff_timeout = sniff_timeout;
+       
+       bt_hci_cmd(HCI_LINK_POLICY_OGF, HCI_SNIFF_MODE_OCF, sizeof(sniff_cmd), &sniff_cmd);
+}
+
 typedef struct {
        uint8_t type;
        uint16_t handle;
index 7a0f99a..e446b09 100644 (file)
@@ -23,6 +23,7 @@
 #define HCI_R_STORED_LINK_KEY_OCF      0x0D
 #define HCI_W_STORED_LINK_KEY_OCF      0x11
 #define HCI_LINK_KEY_REQ_REP_OCF       0x0B
+#define HCI_SNIFF_MODE_OCF             0x03
 
 #define HCI_COMMAND_PACKET             0x01
 #define HCI_ACL_DATA_PACKET            0x02
@@ -87,8 +88,8 @@
 
 void bt_hci_init(void);
 void bt_hci_cmd(const uint8_t OGF, const uint8_t OCF, const uint8_t data_len, const void *data);
+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);
 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);
 uint8_t *bt_hci_get_local_bdaddr(void);
 void bt_hci_ehcill_wake(void);
 #endif
-
index 68015ef..1a7c686 100644 (file)
@@ -224,7 +224,8 @@ void bt_l2cap_handle_connection_request(const uint16_t handle, const uint8_t ide
 {
        bt_l2cap_conn_resp_t resp;
 
-       if (_l2cap_con.cstate == BT_L2CAP_CON_IDLE) {
+       // for now we only support one connection, only on PSM 0x1001
+       if (_l2cap_con.cstate == BT_L2CAP_CON_IDLE && PSM == 0x1001) {
                bt_l2cap_conf_req_t req;
 
                _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
                req.odat = _l2cap_con.locMTU;
 
                bt_acl_send(handle, PB_FIRST_FLUSHABLE, BC_NO_BROADCAST, L2CAP_CID_SIGNALING, sizeof(bt_l2cap_conf_req_t), &req);
+
+               // max_interval Mandatory Range: 0x0006 to 0x0540
+               // min_interval Mandatory Range: 0x0006 to 0x0540
+               // sniff_attempt Mandatory Range for Controller: 1 to Tsniff/2
+               // sniff_timeout Mandatory Range for Controller: 0 to 0x0028
+               bt_hci_set_sniff_mode(handle, 0x10, 0x06, 0x20, 0x10);
        } else {
                resp.resp = CONNECTION_RESPONSE;
                resp.ident = ident;
index 8fdd2a6..0717a92 100644 (file)
@@ -277,7 +277,7 @@ void mw_acc_init(void)
     
        /* this causes data to always be sent */
        // WriteRegisterData = 0x00;
-       WriteRegisterData = 0x02 /*0x08*/;
+       WriteRegisterData = 0x01 /*0x08*/;
        mw_acc_i2c_write(KIONIX_WUF_THRESH, &WriteRegisterData, 1);
      
        /* single byte read test */
index 4850126..52c9e04 100644 (file)
@@ -58,17 +58,18 @@ unsigned int mw_get_battery_adc_val(void)
        ENABLE_ADC();
 
        /* WaitForAdcBusy(); */
-       while (ADC12CTL1 & ADC12BUSY);
+       while (ADC12CTL1 & ADC12BUSY)
+               nop();
 
-       /* Convert the ADC count for the battery input into a voltage 
+       /* Convert the ADC count for the battery input into a voltage
         * ADC12MEM1: Counts Battery Voltage in ADC counts
         * Result: Battery voltage in millivolts */
        BATTERY_SENSE_DISABLE();
 
-       return (unsigned int)(CONVERSION_FACTOR_BATTERY * (double)ADC12MEM1);  
+       return (unsigned int)(CONVERSION_FACTOR_BATTERY * (double)ADC12MEM1);
 }
 
-unsigned char mw_get_battery_percentage_from_val(unsigned int BattVal)
+uint8_t mw_get_battery_percentage_from_val(unsigned int BattVal)
 {
        if (BattVal > BATTERY_FULL_LEVEL)
                BattVal = BATTERY_FULL_LEVEL;
@@ -81,3 +82,20 @@ unsigned char mw_get_battery_percentage_from_val(unsigned int BattVal)
        return (unsigned char)BattVal;
 }
 
+unsigned int mw_get_amblight_adc_val(void)
+{ 
+       LIGHT_SENSE_ENABLE();
+
+       CLEAR_START_ADDR();
+       ADC12CTL1 |= ADC12CSTARTADD_2;
+       ENABLE_ADC();
+
+       /* WaitForAdcBusy(); */
+       while (ADC12CTL1 & ADC12BUSY)
+               nop();
+
+       LIGHT_SENSE_DISABLE();
+
+       return ADC12MEM2;
+}
+
index 20db9c1..78a45cd 100644 (file)
@@ -3,7 +3,8 @@
 
 void mw_init_adc(void);
 unsigned int mw_get_battery_adc_val(void);
-unsigned char mw_get_battery_percentage_from_val(unsigned int BatVal);
+uint8_t mw_get_battery_percentage_from_val(unsigned int BatVal);
+unsigned int mw_get_amblight_adc_val(void);
 
 #endif
 
index 4f8ee90..fa1f98e 100644 (file)
@@ -337,6 +337,11 @@ static void handle_uart_rx_event(void)
                        mw_acc_read(&x, &y, &z);
                        snprintf(tstr, 64, "x:%d y:%d z:%d\n", x,y,z);
                        debug_uart_tx(tstr);
+               } else if (c =='R') {
+                       int16_t al;
+                       al = mw_get_amblight_adc_val();
+                       snprintf(tstr, 64, "light: %d\n", al);
+                       debug_uart_tx(tstr);
                } else if (c == 'b') {
                        debug_uart_tx("\nenabling BT\n");
                        mw_enable_bt();
index b6f5f05..5a29d39 100644 (file)
@@ -165,7 +165,7 @@ void draw_accel_screen(accel_data_t *accel_data)
        oswald_draw_line(40, 82, 40, 30);
 
        x = 41+25+((accel_data->x * 50) / (254));
-       y = 31+25+((accel_data->y * 50) / (254));
+       y = 31+25-((accel_data->y * 50) / (254));
        oswald_draw_pixel(x, y);
        oswald_draw_pixel(x+1, y);
        oswald_draw_pixel(x-1, y);