demo demo / Mbed 2 deprecated BluetoothSumo

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NokiaPresenter.cpp Source File

NokiaPresenter.cpp

00001 // Nokia Presenter Bluetooth
00002 // Copyright (c) 2009 sford
00003 // Released under the MIT License: http://mbed.org/license/mit
00004 
00005 #include "NokiaPresenter.h"
00006 
00007 #include "mbed.h"
00008 
00009 NokiaPresenter::NokiaPresenter(PinName tx, PinName rx, PinName rst) : _bt(tx, rx), _rst(rst) {
00010     printf("Setup bt module\n");
00011     _bt.baud(9600);
00012     _rst = 0;
00013     wait(0.5);
00014     _rst = 1;
00015     wait(0.5);
00016 
00017     printf("Ensure we are in AT command mode...\n");
00018     _bt.printf("+++");
00019     wait(1);
00020     while (_bt.readable()) {
00021         _bt.getc();    // empty buffer
00022     }
00023 
00024     printf("Looking for BT Module...\n");
00025     at("AT\r", "\r\nOK\r\n");
00026 
00027     printf("Reset factory settings...\n");
00028     at("AT&F\r", "\r\nOK\r\n");
00029 
00030     printf("Setting name...\n");
00031     at("AT+BTLNM=\"simon\"\r", "\r\nOK\r\n");
00032 
00033     printf("Allow connections...\n");
00034     at("AT+BTAUT=1, 0\r", "\r\nOK\r\n");
00035 
00036     printf("Start BT server...\n");
00037     at("AT+BTSRV=1\r", "\r\nOK\r\n");
00038 
00039     printf("Waiting for presenter connection...\n");
00040     char buffer[23];
00041     for (int i=0; i<21; i++) {
00042         buffer[i] = _bt.getc();
00043     }
00044 
00045     printf("Got request, starting handshake...\n");
00046     for (int i=0; i<21; i++) {
00047         _bt.putc(buffer[i]);
00048     }
00049     for (int i=0; i<3; i++) {
00050         buffer[i] = _bt.getc();
00051     }
00052     const char connect[27] = {0x05,0x18,0x00,0x01,0x10,0x01,0x02,0x00,0xFF,0xFF,0x00,0x44,0x00,0x65,0x00,0x73,0x00,0x6B,0x00,0x74,0x00,0x6F,0x00,0x70,0x00,0x00,0x00};
00053     for (int i=0; i<27; i++) {
00054         _bt.putc(connect[i]);
00055     }
00056 
00057     printf("Handshake complete, waiting for desktop request...\n");
00058     for (int i=0; i<23; i++) {
00059         buffer[i] = _bt.getc();
00060     }
00061     for (int i=0; i<23; i++) {
00062         _bt.putc(buffer[i]);
00063     }
00064 
00065     printf("Presenter connection all setup!\n");
00066 }
00067 
00068 char NokiaPresenter::key() {
00069     char buffer[6];
00070     for (int i=0; i<6; i++) {
00071         buffer[i] = _bt.getc();
00072     }
00073 
00074     if (buffer[3] == 0x01) {
00075         return 0x00; // keyup
00076     }
00077     if (buffer[5] == 0xF8) {
00078         switch (buffer[4]) {
00079             case 0x07:
00080                 return 'L';
00081             case 0x08:
00082                 return 'R';
00083             case 0x09:
00084                 return 'U';
00085             case 0x0A:
00086                 return 'D';
00087             case 0x45:
00088                 return 'C';
00089         }
00090     }
00091     return buffer[4];
00092 }
00093 
00094 void NokiaPresenter::at(char *command, char *response) {
00095     _bt.printf(command);
00096     for (int i=0; i<strlen(response); i++) {
00097         if (_bt.getc() != response[i]) {
00098             error("AT Command [%s] Failed", command);
00099         }
00100     }
00101 }