11664436fSMatthias Ringwald /*
21664436fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
31664436fSMatthias Ringwald *
41664436fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
51664436fSMatthias Ringwald * modification, are permitted provided that the following conditions
61664436fSMatthias Ringwald * are met:
71664436fSMatthias Ringwald *
81664436fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
91664436fSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
101664436fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
111664436fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
121664436fSMatthias Ringwald * documentation and/or other materials provided with the distribution.
131664436fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
141664436fSMatthias Ringwald * contributors may be used to endorse or promote products derived
151664436fSMatthias Ringwald * from this software without specific prior written permission.
161664436fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
171664436fSMatthias Ringwald * personal benefit and not for any commercial purpose or for
181664436fSMatthias Ringwald * monetary gain.
191664436fSMatthias Ringwald *
201664436fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211664436fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221664436fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251664436fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261664436fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271664436fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281664436fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291664436fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301664436fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311664436fSMatthias Ringwald * SUCH DAMAGE.
321664436fSMatthias Ringwald *
331664436fSMatthias Ringwald * Please inquire about commercial licensing options at
341664436fSMatthias Ringwald * [email protected]
351664436fSMatthias Ringwald *
361664436fSMatthias Ringwald */
371664436fSMatthias Ringwald
381664436fSMatthias Ringwald // *****************************************************************************
391664436fSMatthias Ringwald //
401664436fSMatthias Ringwald // keyboard_demo
411664436fSMatthias Ringwald //
421664436fSMatthias Ringwald // *****************************************************************************
431664436fSMatthias Ringwald
441664436fSMatthias Ringwald #include <stdint.h>
451664436fSMatthias Ringwald #include <stdio.h>
461664436fSMatthias Ringwald #include <stdlib.h>
471664436fSMatthias Ringwald
481664436fSMatthias Ringwald #include "btstack_chipset_cc256x.h"
491664436fSMatthias Ringwald #include "hal_adc.h"
501664436fSMatthias Ringwald #include "hal_board.h"
511664436fSMatthias Ringwald #include "hal_compat.h"
521664436fSMatthias Ringwald #include "hal_lcd.h"
531664436fSMatthias Ringwald #include "hal_usb.h"
541664436fSMatthias Ringwald #include "UserExperienceGraphics.h"
551664436fSMatthias Ringwald #include <msp430x54x.h>
561664436fSMatthias Ringwald
571664436fSMatthias Ringwald #include "btstack_run_loop.h"
581664436fSMatthias Ringwald #include "hci_cmd.h"
591664436fSMatthias Ringwald #include "btstack_memory.h"
601664436fSMatthias Ringwald #include "hci.h"
611664436fSMatthias Ringwald #include "l2cap.h"
621664436fSMatthias Ringwald #include "btstack_link_key_db_memory,h"
631664436fSMatthias Ringwald
641664436fSMatthias Ringwald #define INQUIRY_INTERVAL 15
651664436fSMatthias Ringwald
661664436fSMatthias Ringwald #define FONT_HEIGHT 12 // Each character has 13 lines
671664436fSMatthias Ringwald #define FONT_WIDTH 8
681664436fSMatthias Ringwald
691664436fSMatthias Ringwald static int row = 0;
701664436fSMatthias Ringwald
711664436fSMatthias Ringwald extern int dumpCmds;
721664436fSMatthias Ringwald
731664436fSMatthias Ringwald const char *hexMap = "0123456789ABCDEF";
741664436fSMatthias Ringwald
751664436fSMatthias Ringwald static char lineBuffer[20];
761664436fSMatthias Ringwald static uint8_t num_chars = 0;
771664436fSMatthias Ringwald static bd_addr_t keyboard;
781664436fSMatthias Ringwald static int haveKeyboard = 0;
791664436fSMatthias Ringwald
801664436fSMatthias Ringwald typedef enum {
811664436fSMatthias Ringwald boot = 1,
821664436fSMatthias Ringwald inquiry,
831664436fSMatthias Ringwald w4_inquiry_cmd_complete,
841664436fSMatthias Ringwald w4_l2cap_hii_connect,
851664436fSMatthias Ringwald connected
861664436fSMatthias Ringwald } state_t;
871664436fSMatthias Ringwald
881664436fSMatthias Ringwald static state_t state = 0;
891664436fSMatthias Ringwald
901664436fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
911664436fSMatthias Ringwald
921664436fSMatthias Ringwald #define KEYCODE_RETURN '\n'
931664436fSMatthias Ringwald #define KEYCODE_ESCAPE 27
941664436fSMatthias Ringwald #define KEYCODE_TAB '\t'
951664436fSMatthias Ringwald #define KEYCODE_BACKSPACE 0x7f
961664436fSMatthias Ringwald #define KEYCODE_ILLEGAL 0xffff
971664436fSMatthias Ringwald #define KEYCODE_CAPSLOCK KEYCODE_ILLEGAL
981664436fSMatthias Ringwald
991664436fSMatthias Ringwald #define MOD_SHIFT 0x22
1001664436fSMatthias Ringwald
1011664436fSMatthias Ringwald /**
1021664436fSMatthias Ringwald * English (US)
1031664436fSMatthias Ringwald */
1041664436fSMatthias Ringwald static uint16_t keytable_us_none [] = {
1051664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 0-3 */
1061664436fSMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4 - 13 */
1071664436fSMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14 - 23 */
1081664436fSMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24 - 29 */
1091664436fSMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30 - 39 */
1101664436fSMatthias Ringwald KEYCODE_RETURN, KEYCODE_ESCAPE, KEYCODE_BACKSPACE, KEYCODE_TAB, ' ', /* 40 - 44 */
1111664436fSMatthias Ringwald '-', '=', '[', ']', '\\', KEYCODE_ILLEGAL, ';', '\'', 0x60, ',', /* 45 - 54 */
1121664436fSMatthias Ringwald '.', '/', KEYCODE_CAPSLOCK, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 55 - 60 */
1131664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 61-64 */
1141664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 65-68 */
1151664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 69-72 */
1161664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 73-76 */
1171664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 77-80 */
1181664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 81-84 */
1191664436fSMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
1201664436fSMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */
1211664436fSMatthias Ringwald };
1221664436fSMatthias Ringwald
1231664436fSMatthias Ringwald static uint16_t keytable_us_shift[] = {
1241664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 0-3 */
1251664436fSMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4 - 13 */
1261664436fSMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14 - 23 */
1271664436fSMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24 - 29 */
1281664436fSMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30 - 39 */
1291664436fSMatthias Ringwald KEYCODE_RETURN, KEYCODE_ESCAPE, KEYCODE_BACKSPACE, KEYCODE_TAB, ' ', /* 40 - 44 */
1301664436fSMatthias Ringwald '_', '+', '{', '}', '|', KEYCODE_ILLEGAL, ':', '"', 0x7E, '<', /* 45 - 54 */
1311664436fSMatthias Ringwald '>', '?', KEYCODE_CAPSLOCK, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 55 - 60 */
1321664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 61-64 */
1331664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 65-68 */
1341664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 69-72 */
1351664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 73-76 */
1361664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 77-80 */
1371664436fSMatthias Ringwald KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, KEYCODE_ILLEGAL, /* 81-84 */
1381664436fSMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
1391664436fSMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */
1401664436fSMatthias Ringwald };
1411664436fSMatthias Ringwald
1421664436fSMatthias Ringwald // decode hid packet into buffer - return number of valid keys events
1431664436fSMatthias Ringwald #define NUM_KEYS 6
1441664436fSMatthias Ringwald uint8_t last_keyboard_state[NUM_KEYS];
1451664436fSMatthias Ringwald uint16_t input[NUM_KEYS];
1461664436fSMatthias Ringwald static char last_state_init = 0;
1471664436fSMatthias Ringwald uint16_t last_key;
1481664436fSMatthias Ringwald uint16_t last_mod;
1491664436fSMatthias Ringwald uint16_t last_char;
1501664436fSMatthias Ringwald uint8_t last_key_new;
1511664436fSMatthias Ringwald
doLCD(void)1521664436fSMatthias Ringwald void doLCD(void){
1531664436fSMatthias Ringwald //Initialize LCD and backlight
1541664436fSMatthias Ringwald // 138 x 110, 4-level grayscale pixels.
1551664436fSMatthias Ringwald halLcdInit();
1561664436fSMatthias Ringwald // halLcdBackLightInit();
1571664436fSMatthias Ringwald // halLcdSetBackLight(0); // 8 for normal
1581664436fSMatthias Ringwald halLcdSetContrast(100);
1591664436fSMatthias Ringwald halLcdClearScreen();
1601664436fSMatthias Ringwald halLcdImage(TI_TINY_BUG, 4, 32, 104, 12 );
1611664436fSMatthias Ringwald
1621664436fSMatthias Ringwald halLcdPrintLine("BTstack on ", 0, 0);
1631664436fSMatthias Ringwald halLcdPrintLine("TI MSP430", 1, 0);
1641664436fSMatthias Ringwald halLcdPrintLine("Keyboard", 2, 0);
1651664436fSMatthias Ringwald halLcdPrintLine("Init...", 3, 0);
1661664436fSMatthias Ringwald row = 4;
1671664436fSMatthias Ringwald }
1681664436fSMatthias Ringwald
clearLine(int line)1691664436fSMatthias Ringwald void clearLine(int line){
1701664436fSMatthias Ringwald halLcdClearImage(130, FONT_HEIGHT, 0, line*FONT_HEIGHT);
1711664436fSMatthias Ringwald }
1721664436fSMatthias Ringwald
printLine(char * text)1731664436fSMatthias Ringwald void printLine(char *text){
1741664436fSMatthias Ringwald printf("LCD: %s\n\r", text);
1751664436fSMatthias Ringwald halLcdPrintLine(text, row++, 0);
1761664436fSMatthias Ringwald }
1771664436fSMatthias Ringwald
1781664436fSMatthias Ringwald // put 'lineBuffer' on screen
showLine(void)1791664436fSMatthias Ringwald void showLine(void){
1801664436fSMatthias Ringwald clearLine(row);
1811664436fSMatthias Ringwald halLcdPrintLine(lineBuffer, row, 0);
1821664436fSMatthias Ringwald printf("LCD: %s\n\r", lineBuffer);
1831664436fSMatthias Ringwald }
1841664436fSMatthias Ringwald
hid_process_packet(unsigned char * hid_report,uint16_t * buffer,uint8_t max_keys)1851664436fSMatthias Ringwald unsigned char hid_process_packet(unsigned char *hid_report, uint16_t *buffer, uint8_t max_keys){
1861664436fSMatthias Ringwald
1871664436fSMatthias Ringwald // check for key report
1881664436fSMatthias Ringwald if (hid_report[0] != 0xa1 || hid_report[1] != 0x01) {
1891664436fSMatthias Ringwald return 0;
1901664436fSMatthias Ringwald }
1911664436fSMatthias Ringwald
1921664436fSMatthias Ringwald u_char modifier = hid_report[2];
1931664436fSMatthias Ringwald u_char result = 0;
1941664436fSMatthias Ringwald u_char i;
1951664436fSMatthias Ringwald u_char j;
1961664436fSMatthias Ringwald
1971664436fSMatthias Ringwald if (!last_state_init)
1981664436fSMatthias Ringwald {
1991664436fSMatthias Ringwald for (i=0;i<NUM_KEYS;i++){
2001664436fSMatthias Ringwald last_keyboard_state[i] = 0;
2011664436fSMatthias Ringwald }
2021664436fSMatthias Ringwald last_state_init = 1;
2031664436fSMatthias Ringwald }
2041664436fSMatthias Ringwald
2051664436fSMatthias Ringwald for (i=0; i< NUM_KEYS && result < max_keys; i++){
2061664436fSMatthias Ringwald // find key in last state
2071664436fSMatthias Ringwald uint8_t new_event = hid_report[4+i];
2081664436fSMatthias Ringwald if (new_event){
2091664436fSMatthias Ringwald for (j=0; j<NUM_KEYS; j++){
2101664436fSMatthias Ringwald if (new_event == last_keyboard_state[j]){
2111664436fSMatthias Ringwald new_event = 0;
2121664436fSMatthias Ringwald break;
2131664436fSMatthias Ringwald }
2141664436fSMatthias Ringwald }
2151664436fSMatthias Ringwald if (!new_event) continue;
2161664436fSMatthias Ringwald buffer[result++] = new_event;
2171664436fSMatthias Ringwald last_key = new_event;
2181664436fSMatthias Ringwald last_mod = modifier;
2191664436fSMatthias Ringwald }
2201664436fSMatthias Ringwald }
2211664436fSMatthias Ringwald
2221664436fSMatthias Ringwald // store keyboard state
2231664436fSMatthias Ringwald for (i=0;i<NUM_KEYS;i++){
2241664436fSMatthias Ringwald last_keyboard_state[i] = hid_report[4+i];
2251664436fSMatthias Ringwald }
2261664436fSMatthias Ringwald return result;
2271664436fSMatthias Ringwald }
2281664436fSMatthias Ringwald
2291664436fSMatthias Ringwald
2301664436fSMatthias Ringwald
l2cap_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)2311664436fSMatthias Ringwald static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2321664436fSMatthias Ringwald
2331664436fSMatthias Ringwald if (packet_type == HCI_EVENT_PACKET && hci_event_packet_get_type(packet) == L2CAP_EVENT_CHANNEL_OPENED){
2341664436fSMatthias Ringwald if (packet[2]) {
2351664436fSMatthias Ringwald printf("Connection failed\n\r");
2361664436fSMatthias Ringwald return;
2371664436fSMatthias Ringwald }
2381664436fSMatthias Ringwald printf("Connected\n\r");
2391664436fSMatthias Ringwald num_chars = 0;
2401664436fSMatthias Ringwald lineBuffer[num_chars++] = 'T';
2411664436fSMatthias Ringwald lineBuffer[num_chars++] = 'y';
2421664436fSMatthias Ringwald lineBuffer[num_chars++] = 'p';
2431664436fSMatthias Ringwald lineBuffer[num_chars++] = 'e';
2441664436fSMatthias Ringwald lineBuffer[num_chars++] = ' ';
2451664436fSMatthias Ringwald lineBuffer[num_chars] = 0;
2461664436fSMatthias Ringwald showLine();
2471664436fSMatthias Ringwald }
2481664436fSMatthias Ringwald if (packet_type == L2CAP_DATA_PACKET){
2491664436fSMatthias Ringwald // handle input
2501664436fSMatthias Ringwald // printf("HID report, size %u\n\r", size);
2511664436fSMatthias Ringwald uint8_t count = hid_process_packet(packet, (uint16_t *) &input[0], NUM_KEYS);
2521664436fSMatthias Ringwald if (!count) return;
2531664436fSMatthias Ringwald
2541664436fSMatthias Ringwald uint8_t new_char;
2551664436fSMatthias Ringwald // handle shift
2561664436fSMatthias Ringwald if (last_mod & MOD_SHIFT) {
2571664436fSMatthias Ringwald new_char = keytable_us_shift[input[0]];
2581664436fSMatthias Ringwald } else {
2591664436fSMatthias Ringwald new_char = keytable_us_none[input[0]];
2601664436fSMatthias Ringwald }
2611664436fSMatthias Ringwald // add to buffer
2621664436fSMatthias Ringwald if (new_char == KEYCODE_BACKSPACE){
2631664436fSMatthias Ringwald if (num_chars <= 5) return;
2641664436fSMatthias Ringwald --num_chars;
2651664436fSMatthias Ringwald lineBuffer[num_chars] = 0;
2661664436fSMatthias Ringwald showLine();
2671664436fSMatthias Ringwald return;
2681664436fSMatthias Ringwald }
2691664436fSMatthias Ringwald // 17 chars fit into one line
2701664436fSMatthias Ringwald lineBuffer[num_chars] = new_char;
2711664436fSMatthias Ringwald lineBuffer[num_chars+1] = 0;
2721664436fSMatthias Ringwald if(num_chars < 16) num_chars++;
2731664436fSMatthias Ringwald showLine();
2741664436fSMatthias Ringwald }
2751664436fSMatthias Ringwald }
2761664436fSMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)2771664436fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2781664436fSMatthias Ringwald int i,j;
2791664436fSMatthias Ringwald switch (packet_type) {
2801664436fSMatthias Ringwald case HCI_EVENT_PACKET:
2811664436fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
2821664436fSMatthias Ringwald
2831664436fSMatthias Ringwald case BTSTACK_EVENT_STATE:
2841664436fSMatthias Ringwald // bt stack activated, get started - set local name
2851664436fSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
2861664436fSMatthias Ringwald printLine("Inquiry");
2871664436fSMatthias Ringwald state = inquiry;
2881664436fSMatthias Ringwald gap_inquiry_start(INQUIRY_INTERVAL);
2891664436fSMatthias Ringwald break;
2901664436fSMatthias Ringwald }
2911664436fSMatthias Ringwald break;
2921664436fSMatthias Ringwald
2931664436fSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT:
2941664436fSMatthias Ringwald
2951664436fSMatthias Ringwald // ignore further results
2961664436fSMatthias Ringwald if (haveKeyboard) break;
2971664436fSMatthias Ringwald
2981664436fSMatthias Ringwald // ignore none keyboards
2991664436fSMatthias Ringwald if ((packet[12] & 0x40) != 0x40 || packet[13] != 0x25) break;
3001664436fSMatthias Ringwald
3011664436fSMatthias Ringwald // flip addr
3021664436fSMatthias Ringwald reverse_bd_addr(&packet[3], keyboard);
3031664436fSMatthias Ringwald
3041664436fSMatthias Ringwald // show
3051664436fSMatthias Ringwald printf("Keyboard:\n\r");
3061664436fSMatthias Ringwald
3071664436fSMatthias Ringwald // addr
3081664436fSMatthias Ringwald j=0;
3091664436fSMatthias Ringwald for (i=0;i<6;i++){
3101664436fSMatthias Ringwald lineBuffer[j++] = hexMap[ keyboard[i] >> 4 ];
3111664436fSMatthias Ringwald lineBuffer[j++] = hexMap[ keyboard[i] & 0x0f ];
3121664436fSMatthias Ringwald if (i<5) lineBuffer[j++] = ':';
3131664436fSMatthias Ringwald }
3141664436fSMatthias Ringwald lineBuffer[j++] = 0;
3151664436fSMatthias Ringwald printLine(lineBuffer);
3161664436fSMatthias Ringwald
3171664436fSMatthias Ringwald haveKeyboard = 1;
3181664436fSMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel);
3191664436fSMatthias Ringwald state = w4_inquiry_cmd_complete;
3201664436fSMatthias Ringwald break;
3211664436fSMatthias Ringwald
3221664436fSMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE:
3231664436fSMatthias Ringwald printLine("No keyboard found :(");
3241664436fSMatthias Ringwald break;
3251664436fSMatthias Ringwald
3261664436fSMatthias Ringwald case HCI_EVENT_LINK_KEY_REQUEST:
3271664436fSMatthias Ringwald // deny link key request
3281664436fSMatthias Ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &keyboard);
3291664436fSMatthias Ringwald break;
3301664436fSMatthias Ringwald
3311664436fSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST:
3321664436fSMatthias Ringwald // inform about pin code request
3331664436fSMatthias Ringwald printLine( "Enter 0000");
3341664436fSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_reply, &keyboard, 4, "0000");
3351664436fSMatthias Ringwald break;
3361664436fSMatthias Ringwald
3371664436fSMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE:
338*d39264f2SMatthias Ringwald if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_INQUIRY_CANCEL){
3391664436fSMatthias Ringwald // inq successfully cancelled
3401664436fSMatthias Ringwald // printLine("Connecting");
3411664436fSMatthias Ringwald l2cap_create_channel(l2cap_packet_handler, keyboard, PSM_HID_INTERRUPT, 150);
3421664436fSMatthias Ringwald break;
3431664436fSMatthias Ringwald }
3441664436fSMatthias Ringwald }
3451664436fSMatthias Ringwald }
3461664436fSMatthias Ringwald }
3471664436fSMatthias Ringwald
3481664436fSMatthias Ringwald static hci_transport_config_uart_t config = {
3491664436fSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART,
3501664436fSMatthias Ringwald 115200,
3511664436fSMatthias Ringwald 1000000, // main baudrate
3521664436fSMatthias Ringwald 1, // flow control
3531664436fSMatthias Ringwald NULL,
3541664436fSMatthias Ringwald };
3551664436fSMatthias Ringwald
main(void)3561664436fSMatthias Ringwald int main(void){
3571664436fSMatthias Ringwald
3581664436fSMatthias Ringwald // stop watchdog timer
3591664436fSMatthias Ringwald WDTCTL = WDTPW + WDTHOLD;
3601664436fSMatthias Ringwald
3611664436fSMatthias Ringwald //Initialize clock and peripherals
3621664436fSMatthias Ringwald halBoardInit();
3631664436fSMatthias Ringwald halBoardStartXT1();
3641664436fSMatthias Ringwald halBoardSetSystemClock(SYSCLK_16MHZ);
3651664436fSMatthias Ringwald
3661664436fSMatthias Ringwald // Debug UART
3671664436fSMatthias Ringwald halUsbInit();
3681664436fSMatthias Ringwald
3691664436fSMatthias Ringwald // show off
3701664436fSMatthias Ringwald doLCD();
3711664436fSMatthias Ringwald
3721664436fSMatthias Ringwald // init LEDs
3731664436fSMatthias Ringwald LED_PORT_OUT |= LED_1 | LED_2;
3741664436fSMatthias Ringwald LED_PORT_DIR |= LED_1 | LED_2;
3751664436fSMatthias Ringwald
3761664436fSMatthias Ringwald /// GET STARTED ///
3771664436fSMatthias Ringwald btstack_memory_init();
3781664436fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
3791664436fSMatthias Ringwald
3801664436fSMatthias Ringwald // init HCI
3811664436fSMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance());
3821664436fSMatthias Ringwald btstack_link_key_db_t * link_key_db = btstack_link_key_db_memory_instance();
3831664436fSMatthias Ringwald hci_init(transport, &config);
3841664436fSMatthias Ringwald hci_set_link_key_db(link_key_db);
3851664436fSMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance());
3861664436fSMatthias Ringwald
3871664436fSMatthias Ringwald // register for HCI events
3881664436fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
3891664436fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
3901664436fSMatthias Ringwald
3911664436fSMatthias Ringwald // init L2CAP
3921664436fSMatthias Ringwald l2cap_init();
3931664436fSMatthias Ringwald
3941664436fSMatthias Ringwald // ready - enable irq used in h4 task
3951664436fSMatthias Ringwald __enable_interrupt();
3961664436fSMatthias Ringwald
3971664436fSMatthias Ringwald // turn on!
3981664436fSMatthias Ringwald hci_power_control(HCI_POWER_ON);
3991664436fSMatthias Ringwald
4001664436fSMatthias Ringwald return 0;
4011664436fSMatthias Ringwald }
4021664436fSMatthias Ringwald
403