Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2136 - 1
/**
2
 * source for the Bluetooth driver
3
 * @file bluetooth.c
4
 * @author Linus Lotz<lotz@in.tum.de>
5
 * @author Salomon Sickert
6
 */
7
 
8
 
9
//############################################################################
10
//# HISTORY  bluetooth.c
11
//#
12
//#
13
//# 02.08.2015 CB
14
//# - chg: bt_showsettings(), angepasst und vollendet, zur Zeit nur im Mastermodus des BTM222 nutzbar
15
//#
16
//# 30.06.2014 OG
17
//# - chg: bt_discover() wieder auf (berechnete) Progressbar umgestellt
18
//#
19
//# 26.06.2014 OG
20
//# - del: bt_receiveNMEA() - jetzt in gps/nmea.c
21
//# - chg: bt_discover(), bt_searchDevice() - stark geaendert; Timeout geht jetzt
22
//#        nach Zeit und Abbruch durch Benutzer moeglich
23
//# - fix: BT-Devicesuche - wenn bei einer Suche Devices gefunden wurden und bei
24
//#        einer nachfolgenden Suche weniger (z.B. weil ein Device ausgeschaltet wurde)
25
//#        dann wurden zuviele Devices angezeigt; fixed
26
//#
27
//# 25.06.2014 OG
28
//# - chg: bt_discover(), bt_searchDevice()
29
//#
30
//# 22.06.2014 OG
31
//# - chg: Code-Formattierung
32
//#
33
//# 21.06.2014 CB
34
//# - fix: Fehler in "copy_DevName", zwei Leerzeichen am Anfang beendeten sofort den Namen, Namen war dann leer
35
//# - fix: Umbau der Timeoutfunktion bei der Devicesuche, wenn kein BT Device in Reichweite wurde die Suche nicht beendet
36
//# - add: Progressbar bei BT Device Suche
37
//#
38
//# 19.06.2014 OG
39
//# - chg: Code-Formattierung
40
//#
41
//# 16.06.2014 OG
42
//# - etliche Aenderungen und Funktionsumbenennungen - vorallem rund um den
43
//#   Bereich BT-Init
44
//# - viel Code-Layout (noch nicht abgeschlossen)
45
//#
46
//# 08.06.2014 OG
47
//# - del: BT_New_Baudrate
48
//#
49
//# 30.01.2014 OG
50
//# - add: Unterstuetzung fuer USE_BLUETOOTH
51
//#
52
//# 2013 Cebra
53
//# - Erweiterung um BT Local-ID Abfrage
54
//#
55
//# 26.06.2013 Cebra
56
//# - chg: Modulumschaltung fuer WiFlypatch geaendert
57
//#
58
//# 24.06.2013 OG
59
//# - add: bt_fixname() - korrigiert ggf. fehlehaften BT-Namen in Config.bt_name
60
//#
61
//# 24.06.2013 OG
62
//# - chg: bt_init(): Code-Layout, Code-Struktur, ggf. Debug-Ausgaben
63
//# - chg: bt_searchmodul(): Code-Layout und Ausgabe
64
//# - chg: send_cmd(): Handling von BT_SET_NAME & Code-Layout
65
//# - add: define DEBUG_BT
66
//#
67
//# 23.05.2013 OG
68
//# - chg: Timeout reduziert in bt_disconnect() (trennen der BT-Verbindung)
69
//# - fix: copy_DevName() Range angepasst und 0 Terminierung
70
//#
71
//# 24.04.2013 OG
72
//# - chg: Auskommentiert: #define __DELAY_BACKWARD_COMPATIBLE__
73
//#        das wird bereits vom Compiler-Symbol dieses PKT-Projektes definiert
74
//#        und erzeugt nur zusaetzliche Warnings
75
//#
76
//# 03.04.2013 Cebra
77
//# - chg: Init Routinen BT überarbeitet, es kam zu falschen Baudrateneinstellungen
78
//#        beim BTM-222 ohne Hinweis, mehr Displayinformationen beim Init
79
//############################################################################
80
 
81
 
82
#include <string.h>
83
#include "../cpu.h"
84
#include <util/delay.h>
85
#include <avr/interrupt.h>
86
#include <avr/pgmspace.h>
87
#include <stdlib.h>
88
 
89
#include "bluetooth.h"
90
#include "../main.h"
91
 
92
 
93
//----------------------------------------------------------------------------
94
 
95
#ifdef USE_BLUETOOTH        // 30.01.2014 OG
96
                            // brutal hier den Source ausklammern, damit man an anderen Stellen des PKT
97
                            // den BT-Code durch den Compiler entdeckt
98
                            // ansonsten kann es passieren, dass einiges an BT-Code unenddeckt einkompiliert wird
99
                            // -> bei den ersten Test's waren das immerhin ca. 10 KByte durch unenddeckten Code...
100
 
101
//----------------------------------------------------------------------------
102
 
103
//#define DEBUG_BT
104
//#ifdef HWVERSION3_9
105
 
106
#include "../uart/uart1.h"
107
#include "../uart/usart.h"
108
#include "../timer/timer.h"
109
#include "fifo.h"
110
#include "error.h"
111
#include "../lcd/lcd.h"
112
#include "../eeprom/eeprom.h"
113
#include "../setup/setup.h"
114
#include "bluetooth.h"
115
#include "../tracking/tracking.h"
116
#include "../utils/xutils.h"
117
 
118
#include "../messages.h"
119
#include "../pkt/pkt.h"
120
#include "../lipo/lipo.h"
121
#include "../utils/menuctrl.h"
122
#include "../utils/scrollbox.h"
123
 
124
//############################################################################
125
//############################################################################
126
 
127
 
128
//#define BT_CMD_TIMEOUT_MS 2000
129
#define BT_CMD_TIMEOUT_MS   500         // 15.06.2014 OG: Timeout reduziert von 2000 auf 500
130
 
131
 
132
static const char STR_BTM222INIT[]    PROGMEM = "BTM222 Init";
133
 
134
 
135
//#define SaveMem
136
 
137
//
138
// Baudrate for the UART-connection to the BTM-222 on SQUIRREL
139
//
140
 
141
#define SQUIRREL
142
 
143
#ifdef SQUIRREL
144
#define UART_BAUD_RATE      19200
145
#endif
146
 
147
#ifdef NUT
148
#define UART_BAUD_RATE      19200
149
#endif
150
 
151
 
152
typedef enum {
153
    BT_RAW,
154
    BT_DATA,
155
    BT_CMD,
156
    BT_NOECHO,
157
    BT_NOANSWER
158
} communication_mode_t;
159
 
160
 
161
 
162
typedef enum {
163
    BT_TEST,                // AT
164
    BT_CONNECT,             // ATA
165
    BT_DISCONNECT,          // ATH
166
    BT_CLEAR_ADDRESS,       // ATD0
167
    BT_SET_ADDRESS,         // ATD=_____
168
    BT_GET_LOCALID,         // ATB? Inquire the Local BD address
169
    BT_FIND_DEVICES,        // ATF?
170
    BT_DISABLE_AUTOCONNECT, // ATO1
171
    BT_ENABLE_AUTOCONNECT,  // ATO0
172
    BT_SET_MASTER,          // ATR0
173
    BT_SET_SLAVE,           // ATR1
174
    BT_SET_PIN,             // ATP=1234
175
    BT_SET_2400,            // ATL* Baudrate 2400
176
    BT_SET_4800,            // ATL0 Baudrate 4800
177
    BT_SET_9600,            // ATL1 Baudrate 9600
178
    BT_SET_19200,           // ATL2 Baudrate 19200
179
    BT_SET_38400,           // ATL3 Baudrate 38400
180
    BT_SET_57600,           // ATL4 Baudrate 57600
181
    BT_SET_115200,          // ATL5 Baudrate 115200
182
    BT_SET_NOANSWER,        // ATQ1 Rückmeldungen aus
183
    BT_SET_NOECHO,          // ATE0 ECHO deaktivieren
184
    BT_SET_ANSWER,          // ATQ0 Rückmeldungen
185
    BT_SET_ECHO,            // ATE1 ECHO aktivieren
186
    BT_SET_DEFAULT,         // Defaultwerte setzen
187
    BT_SET_NAME,            // Devicename
188
    BT_SET_DISPWRDOWN,      // disable auto Powerdown
189
    BT_SHOW_SETTINGS        // ATI1 Listing all setting value
190
} bt_cmd_t;
191
 
192
 
193
//TODO: FIFO Groesse
194
#define IN_FIFO_SIZE 512
195
 
196
char localID[15]="12345678901234";
197
static uint8_t bt_buffer[IN_FIFO_SIZE];
198
fifo_t in_fifo;
199
 
200
char bt_rx_buffer[RXD_BUFFER_SIZE];
201
volatile uint8_t bt_rx_len;
202
volatile uint8_t bt_rx_ready = 0;
203
 
204
 
205
uint8_t EchoAnswerOn=false;                   //Merkzelle
206
static bt_mode_t bt_mode = BLUETOOTH_SLAVE;
207
static communication_mode_t comm_mode = BT_CMD;
208
 
209
uint8_t i = 0;
210
 
211
uint8_t bt_devicecount = 0;
212
 
213
uint8_t bt_rxerror = 0;
214
uint8_t bt_frameerror = 0;
215
uint8_t bt_overrun = 0;
216
uint8_t bt_bufferoverflow = 0;
217
 
218
 
219
device_info device_list[NUTS_LIST];
220
 
221
 
222
 
223
 
224
// Set a timeout of Y ms and a Conditon X, which have to be true while timeout
225
#define while_timeout(X, Y) for(uint16_t __timeout = 0; __timeout++ <= Y && (X); Delay_MS(Y ? 1 : 0))
226
 
227
 
228
//--------------------------------------------------------------
229
// entfernt ungueltige Zeichen und Leerzeichen (rechts) aus dem
230
// BT-Namen die evtl. durch Default's reingekommen sind.
231
// Falls ungueltige Zeichen ('.') wird der BT-Name auf "PKT"
232
// gesetzt.
233
//--------------------------------------------------------------
234
void bt_fixname(void)
235
{
236
    char *p;
237
 
238
    p = Config.bt_name;
239
    while( *p!=0 && *p!='.' ) p++;                                  // suche '.'
240
    if( *p=='.' || Config.bt_name[0]==' ' || Config.bt_name[0]==0)  // wenn '.' gefunden oder Anfang mit ' ' oder kein String
241
        strcpy_P(Config.bt_name, PSTR("PKT"));                      //   -> dann BT-Name auf "PKT" setzen
242
    strrtrim( Config.bt_name );                                     // ggf. Leerzeichen rechts loeschen
243
}
244
 
245
 
246
 
247
//--------------------------------------------------------------
248
//--------------------------------------------------------------
249
void Delay_MS(int count)
250
{
251
    for (int i = 0; i < count; i++)
252
        _delay_ms(1);
253
}
254
 
255
 
256
 
257
//--------------------------------------------------------------
258
//--------------------------------------------------------------
259
void bt_start(void)
260
{
261
    if( Config.BTIsSlave == true )  EchoAnswerOn = false;
262
    else                            EchoAnswerOn = true;
263
}
264
 
265
 
266
 
267
//--------------------------------------------------------------
268
//--------------------------------------------------------------
269
void uart_receive(void)
270
{
271
    unsigned int uart_data;
272
 
273
    while( !fifo_is_full(&in_fifo) )
274
    {
275
        uart_data = uart1_getc();
276
 
277
        //USART_puts(".");
278
        if (!(uart_data==UART_NO_DATA)) USART_putc(uart_data);
279
 
280
        switch( uart_data & 0xFF00 )
281
        {
282
            // Framing Error detected, i.e no stop bit detected
283
            case UART_FRAME_ERROR:
284
                    bt_rxerror++;
285
                    bt_frameerror++;
286
                    return;
287
 
288
            // Overrun, a character already presend in the UART UDR register was
289
            // not read by the interrupt handler before the next character arrived,
290
            // one or more received characters have been dropped
291
            //
292
            case UART_OVERRUN_ERROR:
293
                    bt_rxerror++;
294
                    bt_overrun++;
295
                    return;
296
 
297
            // We are not reading the receive buffer fast enough,
298
            // one or more received character have been dropped
299
            //
300
            case UART_BUFFER_OVERFLOW:
301
                    bt_rxerror++;
302
                    bt_bufferoverflow++;
303
                    return;
304
 
305
            // UART Inputbuffer empty, nothing to do
306
            case UART_NO_DATA:
307
                    return;
308
 
309
            default:
310
                    fifo_write( &in_fifo, uart_data);
311
        }
312
    }
313
}
314
 
315
 
316
 
317
//--------------------------------------------------------------
318
//--------------------------------------------------------------
319
static void uart_send(const char *data, const uint8_t length)
320
{
321
    #ifdef SND_DEBUG
322
    debug_pgm(PSTR("bt_uart_send"));
323
    if (comm_mode == BT_CMD) debug_pgm(PSTR("bt_uart_send:BT_CMD")); else debug_pgm(PSTR("bt_uart_send:Wrong comm-mode"));
324
    if (EchoAnswerOn == true) debug_pgm(PSTR("bt_uart_send:EchoAnswer ON")); else debug_pgm(PSTR("bt_uart_send:EchoAnswer OFF"));
325
    #endif
326
 
327
    char echo;
328
 
329
//  lcd_printp_at (i++, 1, PSTR("."), 0);
330
    for (uint8_t i = 0; i < length; i++)
331
    {
332
 
333
    #ifdef SND_DEBUG
334
        USART_putc((data[i]));    //test
335
    #endif
336
//      debug_pgm(PSTR("bt_init_S"));
337
 
338
        if (uart1_putc(data[i]) == 0)
339
        {
340
    #ifdef SND_DEBUG
341
            warn_pgm(PSTR("UART: Remote not ready"));
342
    #endif
343
            return;
344
        }
345
 
346
        if (comm_mode == BT_RAW)
347
            _delay_ms(50);
348
 
349
        if (comm_mode == BT_DATA)
350
            _delay_ms(1);
351
 
352
 
353
        if ((comm_mode == BT_CMD) && (EchoAnswerOn == true))
354
        {
355
    #ifdef SND_DEBUG
356
                        warn_pgm(PSTR ("UARTsend: get Echo"));
357
    #endif
358
            uint8_t x = 0;
359
            for (; x < 3; x++)
360
            {
361
 
362
                while_timeout(fifo_is_empty(&in_fifo), 200)
363
//              for(uint16_t __timeout = 0; __timeout++ <= 200 && (fifo_is_empty(&in_fifo)); _delay_ms(200 ? 1 : 0))
364
 
365
                uart_receive();
366
 
367
 
368
                fifo_read(&in_fifo, &echo);
369
 
370
                if (echo != data[i]) {
371
                    if (uart1_putc(data[i]) == 0)
372
                    {
373
    #ifdef SND_DEBUG
374
                            warn_pgm(PSTR ("UART: Remote not ready"));
375
    #endif
376
                        return;
377
                    }
378
                }
379
                else
380
                    break;
381
            }
382
    #ifdef DEBUG
383
            if (x == 3)
384
            {
385
 
386
                error_putc(data[i]);
387
 
388
                error_pgm(PSTR("BT: WRONG ECHO"));
389
                                //_delay_ms(2000);
390
 
391
            }
392
    #endif
393
        }
394
        else
395
          {
396
                    {
397
                            uart_receive();
398
                            fifo_read(&in_fifo, &echo);
399
 
400
                    }
401
 
402
    #ifdef SND_DEBUG
403
                    warn_pgm(PSTR ("UARTsend: skip Echo"));
404
    #endif
405
          }
406
    }
407
}
408
 
409
 
410
//--------------------------------------------------------------
411
// send_cmd( command, *data)
412
//--------------------------------------------------------------
413
static uint16_t send_cmd( const bt_cmd_t command, const char *data)
414
{
415
    uint16_t CommandDelay;      // nach BTM222 Kommandos verschiedene Verzögerungszeiten bevor es weitergehen kann
416
    uint8_t i;
417
 
418
    char full_command[20];      // Maximum command size
419
 
420
    CommandDelay = 100;         // Default = 100ms
421
 
422
    switch( command )
423
    {
424
        case BT_SET_PIN:        strcpy_P( full_command, PSTR("ATP="));
425
                                for( i = 0; i < bt_pin_length; i++)
426
                                {
427
                                    full_command[i+4] = Config.bt_pin[i];
428
                                }
429
                                full_command[(bt_pin_length+4)] = 0;
430
                                break;
431
 
432
        case BT_SET_DEFAULT:    strcpy_P( full_command, PSTR("ATZ0"));
433
                                CommandDelay = 3000;
434
                                break;
435
 
436
 
437
        case BT_SET_2400:       strcpy_P( full_command, PSTR("ATL*"));
438
                                #ifdef DEBUG
439
                                  debug_pgm(PSTR("ATL*"));
440
                                #endif
441
                                break;
442
 
443
        case BT_SET_4800:       strcpy_P( full_command, PSTR("ATL0"));
444
                                #ifdef DEBUG
445
                                  debug_pgm(PSTR("ATL0"));
446
                                #endif
447
                                break;
448
 
449
        case BT_SET_9600:       strcpy_P( full_command, PSTR("ATL1"));
450
                                #ifdef DEBUG
451
                                  debug_pgm(PSTR("ATL1"));
452
                                #endif
453
                                break;
454
 
455
        case BT_SET_19200:      strcpy_P( full_command, PSTR("ATL2"));
456
                                #ifdef DEBUG
457
                                  debug_pgm(PSTR("ATL2"));
458
                                #endif
459
                                break;
460
 
461
        case BT_SET_38400:      strcpy_P( full_command, PSTR("ATL3"));
462
                                #ifdef DEBUG
463
                                  debug_pgm(PSTR("ATL3"));
464
                                #endif
465
                                break;
466
 
467
        case BT_SET_57600:      strcpy_P( full_command, PSTR("ATL4"));
468
                                #ifdef DEBUG
469
                                  debug_pgm(PSTR("ATL4"));
470
                                #endif
471
                                break;
472
 
473
        case BT_SET_115200:     strcpy_P( full_command, PSTR("ATL5"));
474
                                #ifdef DEBUG
475
                                  debug_pgm(PSTR("ATL5"));
476
                                #endif
477
                                break;
478
 
479
        case BT_SET_NOANSWER:   strcpy_P( full_command, PSTR("ATQ1"));
480
                                #ifdef DEBUG
481
                                  debug_pgm(PSTR("ATQ1"));
482
                                #endif
483
                                break;
484
 
485
        case BT_SET_NOECHO:     strcpy_P( full_command, PSTR("ATE0"));
486
                                #ifdef DEBUG
487
                                  debug_pgm(PSTR("ATE0"));
488
                                #endif
489
                                break;
490
 
491
        case BT_SET_ANSWER:     strcpy_P( full_command, PSTR("ATQ0"));
492
                                #ifdef DEBUG
493
                                  debug_pgm(PSTR("ATQ0"));
494
                                #endif
495
                                break;
496
 
497
        case BT_SET_ECHO:       strcpy_P( full_command, PSTR("ATE1"));
498
                                #ifdef DEBUG
499
                                  debug_pgm(PSTR("ATE1"));
500
                                #endif
501
                                break;
502
 
503
        case BT_TEST:           strcpy_P( full_command, PSTR("AT"));
504
                                #ifdef DEBUG
505
                                  debug_pgm(PSTR("AT"));
506
                                #endif
507
                                break;
508
 
509
        case BT_CONNECT:        strcpy_P( full_command, PSTR("ATA"));
510
                                #ifdef DEBUG
511
                                  debug_pgm(PSTR("ATA"));
512
                                #endif
513
                                break;
514
 
515
        case BT_DISCONNECT:     strcpy_P( full_command, PSTR("ATH"));
516
                                #ifdef DEBUG
517
                                  debug_pgm(PSTR("ATH"));
518
                                #endif
519
                                break;
520
 
521
        case BT_CLEAR_ADDRESS:  strcpy_P( full_command, PSTR("ATD0"));
522
                                break;
523
 
524
        case BT_SET_ADDRESS:    strcpy_P( full_command, PSTR("ATD="));
525
                                memcpy( (full_command + strlen(full_command)), data, 12);
526
                                full_command[16] = 0;
527
                                #ifdef DEBUG
528
                                  debug_pgm(PSTR("ATLD="));
529
                                #endif
530
                                break;
531
 
532
        case BT_FIND_DEVICES:   strcpy_P( full_command, PSTR("ATF?"));
533
                                #ifdef DEBUG
534
                                  debug_pgm(PSTR("ATF?"));
535
                                #endif
536
                                break;
537
 
538
        case BT_DISABLE_AUTOCONNECT:
539
                                strcpy_P( full_command, PSTR("ATO1"));
540
                                CommandDelay = 3500;
541
                                #ifdef DEBUG
542
                                  debug_pgm(PSTR("ATO1"));
543
                                #endif
544
                                break;
545
 
546
        case BT_ENABLE_AUTOCONNECT:
547
                                strcpy_P( full_command, PSTR("ATO0"));
548
                                CommandDelay = 3500;
549
                                #ifdef DEBUG
550
                                  debug_pgm(PSTR("ATO0"));
551
                                #endif
552
                                break;
553
 
554
        case BT_SET_MASTER:     strcpy_P( full_command, PSTR("ATR0"));
555
                                CommandDelay = 3000;
556
                                #ifdef DEBUG
557
                                  debug_pgm(PSTR("ATR0"));
558
                                #endif
559
                                break;
560
 
561
        case BT_SET_SLAVE:      strcpy_P( full_command, PSTR("ATR1"));
562
                                CommandDelay = 3000;
563
                                #ifdef DEBUG
564
                                  debug_pgm(PSTR("ATR1"));
565
                                #endif
566
                                break;
567
 
568
        case BT_SET_NAME:       strcpy_P( full_command, PSTR("ATN=") );
569
                                strcat( full_command, Config.bt_name);
570
                                //lcd_print_at( 0, 7, full_command, 0);     // DEBUG
571
                                //lcdx_printf_at( 17, 7, MNORMAL, 0,0, "%3d", strlen(full_command) );
572
                                //_delay_ms(6000);
573
                                #ifdef DEBUG
574
                                  debug_pgm(PSTR("ATN="));
575
                                #endif
576
                                break;
577
 
578
        case BT_SET_DISPWRDOWN: strcpy_P(full_command, PSTR("ATS1"));
579
                                #ifdef DEBUG
580
                                  debug_pgm(PSTR("ATS1"));
581
                                #endif
582
                                break;
583
 
584
        case BT_GET_LOCALID:    strcpy_P(full_command, PSTR("ATB?"));
585
                                #ifdef DEBUG
586
                                  debug_pgm(PSTR("ATB?"));
587
                                #endif
588
                                break;
589
        case BT_SHOW_SETTINGS:  strcpy_P(full_command, PSTR("ATI1"));
590
                                break;
591
 
592
        default:                warn_pgm(PSTR("CMD UNK"));
593
                                CommandDelay = 0;
594
                                return false;
595
    }
596
 
597
    strcat_P( full_command, PSTR("\r") );
598
 
599
    // throw away your television
600
    uart_receive();
601
    fifo_clear( &in_fifo );
602
 
603
    // send command
604
    uart_send( full_command, strlen(full_command));
605
 
606
    // get response
607
    #ifdef DEBUG
608
    debug_pgm(PSTR("send_cmd:get Response"));
609
    #endif
610
 
611
 
612
    if( !EchoAnswerOn )
613
    {
614
        _delay_ms(CommandDelay);
615
        uart1_flush();
616
        fifo_clear(&in_fifo);
617
        return true;
618
    }
619
    else
620
    {
621
        while_timeout(true, BT_CMD_TIMEOUT_MS)
622
        {
623
            uart_receive();
624
 
625
            if( command == BT_GET_LOCALID )
626
            {
627
                _delay_ms(CommandDelay);
628
                return bt_getID();
629
            }
630
 
631
            if( fifo_strstr_pgm( &in_fifo, PSTR("OK\r\n")) )
632
            {
633
                _delay_ms(CommandDelay);
634
                return true;
635
            }
636
 
637
            if( fifo_strstr_pgm(&in_fifo, PSTR("ERROR\r\n")) )
638
            {
639
                return false;
640
            }
641
        }  // end: while_timeout()
642
    }
643
 
644
    return false;
645
}
646
 
647
 
648
 
649
//--------------------------------------------------------------
650
//--------------------------------------------------------------
651
static void clean_line(void)
652
{
653
    while_timeout(true, 50)
654
        uart_receive();
655
 
656
    fifo_strstr_pgm( &in_fifo, PSTR("\r\n") );
657
}
658
 
659
 
660
 
661
//--------------------------------------------------------------
662
//--------------------------------------------------------------
663
static communication_mode_t update_comm_mode( uint16_t timeout_ms )
664
{
665
    while_timeout(true, timeout_ms)
666
    {
667
        uart_receive();
668
 
669
        if( fifo_strstr_pgm(&in_fifo, PSTR("DISCONNECT")) )
670
        {
671
            clean_line();
672
            comm_mode = BT_CMD;
673
            send_cmd(BT_TEST, NULL);
674
            return comm_mode;
675
        }
676
 
677
        if( fifo_strstr_pgm(&in_fifo, PSTR("CONNECT")) )
678
        {
679
            _delay_ms(100);                                     // don't delete this, else there will be no success!!!!!!!!!
680
            comm_mode = BT_DATA;
681
            return comm_mode;
682
        }
683
 
684
        if( fifo_strstr_pgm (&in_fifo, PSTR("Time out,Fail to connect!")) )
685
        {
686
            clean_line();
687
 
688
            #ifdef DEBUG
689
            debug_pgm(PSTR("CONNECT FAILED\n"));
690
            #endif
691
 
692
            send_cmd(BT_TEST, NULL);
693
            comm_mode = BT_CMD;
694
            return comm_mode;
695
        }
696
    }
697
 
698
    return comm_mode;
699
}
700
 
701
 
702
 
703
//--------------------------------------------------------------
704
//--------------------------------------------------------------
705
void bt_set_EchoAnswer( uint8_t on )
706
{
707
    comm_mode = BT_CMD;     // Set comm_mode to CMD
708
    EchoAnswerOn = false;
709
 
710
    if( on )
711
    {
712
        send_cmd( BT_SET_ECHO   , NULL);
713
        send_cmd( BT_SET_ANSWER , NULL);
714
        send_cmd( BT_TEST       , NULL);
715
//        EchoAnswerOn = true;
716
 
717
        /*
718
        if( !send_cmd( BT_TEST  , NULL) )
719
        {
720
            //lcd_printp_at (0, 6, PSTR("Error set Echo"), MNORMAL);
721
            //_delay_ms(3000);
722
        }
723
        */
724
    }
725
    else
726
    {
727
        send_cmd( BT_SET_NOECHO  , NULL);
728
        send_cmd( BT_SET_NOANSWER, NULL);
729
//        EchoAnswerOn = false;
730
    }
731
}
732
 
733
 
734
 
735
//--------------------------------------------------------------
736
//--------------------------------------------------------------
737
uint16_t BTM222_SetBaud( uint8_t baudrate )
738
{
739
    bt_cmd_t btcommand = BT_SET_57600;
740
    uint8_t  lOk       = true;
741
 
742
 
743
    comm_mode = BT_CMD;                 // Set comm_mode to CMD
744
    uart1_flush();
745
    fifo_clear( &in_fifo );
746
 
747
    bt_set_EchoAnswer(true);
748
 
749
    if( !send_cmd(BT_TEST, NULL) )      // Test mit PKT_Baudrate
750
    {
751
        return false;
752
    }
753
    else
754
    {
755
        switch( baudrate )
756
        {
757
            case Baud_2400:     btcommand = BT_SET_2400;    break;
758
            case Baud_4800:     btcommand = BT_SET_4800;    break;
759
            case Baud_9600:     btcommand = BT_SET_9600;    break;
760
            case Baud_19200:    btcommand = BT_SET_19200;   break;
761
            case Baud_38400:    btcommand = BT_SET_38400;   break;
762
            case Baud_57600:    btcommand = BT_SET_57600;   break;
763
            case Baud_115200:   btcommand = BT_SET_115200;  break;
764
        }
765
 
766
        if( !(send_cmd( btcommand, NULL)) )     lOk = false;        // Fehler
767
 
768
        SetBaudUart1( baudrate );
769
        uart1_flush();
770
        fifo_clear( &in_fifo );
771
        send_cmd( BT_TEST, NULL);
772
 
773
        if( !(send_cmd(BT_TEST, NULL)) )        lOk = false;        // Fehler
774
 
775
        bt_set_EchoAnswer(false);
776
    }
777
 
778
    return lOk;
779
}
780
 
781
 
782
 
783
//--------------------------------------------------------------
784
// foundbaud = BTM222_CheckBaud( Baudrate )
785
//
786
// suche BTM222 mit entsprechender Baudrate
787
//
788
// RUECKGABE:
789
//  =0: Error (BTM222 not found at given Baud)
790
//  >0: Baudrate
791
//
792
// INFO: Baudrate
793
//   Baud_9600       1
794
//   Baud_19200      2
795
//   Baud_38400      3
796
//   Baud_57600      4
797
//   Baud_115200     5
798
//   Baud_4800       6
799
//   Baud_2400       7
800
//--------------------------------------------------------------
801
uint8_t BTM222_CheckBaud( uint8_t Baudrate )
802
{
803
  //lcdx_cls_row( y, mode, yoffs )
804
    lcdx_cls_row( 2, MNORMAL, 1 );                          // Zeile loeschen
805
    lcdx_printf_center_P( 2, MNORMAL, 0,1, PSTR("%S %lu Baud"), strGet(STR_SEARCH), Baud_to_uint32(Baudrate) );   // "suche nnn Baud"
806
 
807
    comm_mode = BT_CMD;             // Set comm_mode to CMD
808
    SetBaudUart1( Baudrate );
809
 
810
    uart1_flush();
811
    fifo_clear( &in_fifo );
812
 
813
    EchoAnswerOn = false;
814
 
815
    send_cmd( BT_TEST      , NULL);
816
    send_cmd( BT_SET_ANSWER, NULL);
817
    send_cmd( BT_SET_ECHO  , NULL);
818
 
819
    EchoAnswerOn = true;
820
 
821
 
822
    if( send_cmd(BT_TEST, NULL) )
823
    {
824
        return Baudrate;            // BTM222 Baudrate gefunden
825
    }
826
 
827
    return 0;                       // 0 = nicht gefunden
828
}
829
 
830
 
831
 
832
//--------------------------------------------------------------
833
//--------------------------------------------------------------
834
void BTM222_Initalize( void )
835
{
836
    uint8_t foundbaud  = 0;
837
    uint8_t nError     = 0;
838
 
839
    // ggf. rechte Leerzeichen oder ungueltige Zeichen
840
    // von bt_name entfernen
841
    bt_fixname();
842
 
843
 
844
    //----------------------------
845
    // 1. Frage: initialisieren?
846
    //----------------------------
847
    // Text1: "Kopter ausschalten!"
848
    // Text2: NULL
849
    // Head : "* ACHTUNG *"
850
    // Titel: "Wi.232 Init"
851
 
852
    if( !PKT_Ask_P( ASK_CANCEL_OK, PSTR("initialisieren?"), NULL, STR_BTM222INIT, STR_BTM222INIT) )
853
    {
854
        return;
855
    }
856
 
857
 
858
 
859
    //------------------------------------
860
    // 2. BTM222 suchen
861
    //------------------------------------
862
    PKT_Title_P( STR_BTM222INIT, true, true );                                                          // Titel - mit ShowLipo und clearscreen
863
    lcdx_printf_center_P( 2, MNORMAL, 0,1, PSTR("%S %lu Baud"), strGet(STR_SEARCH), Baud_to_uint32(Old_Baudrate) );   // "suche nnn Baud"
864
 
865
    set_Modul_On( Bluetooth );
866
    EchoAnswerOn = false;
867
 
868
    fifo_init( &in_fifo, bt_buffer, IN_FIFO_SIZE);
869
 
870
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Old_Baudrate );                                   // versuche zuerst mit der 'alten' Baudrate (hohe Trefferwahrscheinlichkeit)
871
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Baud_2400    );
872
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Baud_9600    );
873
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Baud_19200   );
874
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Baud_38400   );
875
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Baud_57600   );
876
    if( !foundbaud )    foundbaud = BTM222_CheckBaud( Baud_115200  );
877
 
878
    if( !foundbaud )
879
    {
880
      //PKT_Message_P( text, error, timeout, beep, clearscreen)
881
        PKT_Message_P( PSTR("BTM222 not found!"), true, 2000, true, true);                              // ERROR: max. 20 Sekunden anzeigen
882
        Config.UseBT = false;                                                                           // Modul in Konfig sperren, verhindert Init beim Start
883
    }
884
 
885
 
886
    //------------------------------------
887
    // 2. Initialisieren
888
    //------------------------------------
889
    if( foundbaud > 0 )
890
    {
891
        PKT_Title_P( STR_BTM222INIT, true, true );                                                      // Titel: mit ShowLipo und clearscreen
892
        lcdx_printf_center_P( 2, MNORMAL, 0,1, STR_BTM222INIT );                                        // Headline
893
 
894
      //PKT_Progress_Init( max, yoffs, width, height);
895
        PKT_Progress_Init( 7, 0,  0,0);                                                                 // 7 Progress Steps
896
 
897
        comm_mode = BT_CMD;                                                                             // Set comm_mode to CMD
898
 
899
        if( !nError && PKT_Progress_Next() && !send_cmd( BT_CLEAR_ADDRESS, NULL)   )    nError = 1;     // Clear remote address
900
        if( !nError && PKT_Progress_Next() && !send_cmd( BT_SET_SLAVE, NULL)       )    nError = 2;     // Set BTM to SLAVE
901
        if( !nError && PKT_Progress_Next() && !send_cmd( BT_SET_PIN, NULL)         )    nError = 3;     // Set BTM PIN
902
        if( !nError && PKT_Progress_Next() && !send_cmd( BT_SET_NAME, NULL)        )    nError = 4;     // Set BTM Name
903
        if( !nError && PKT_Progress_Next() && !send_cmd( BT_SET_DISPWRDOWN, NULL)  )    nError = 5;
904
        if( !nError && PKT_Progress_Next() && !send_cmd( BT_GET_LOCALID, NULL)     )    nError = 6;     // MAC ermitteln
905
      //if( !nError && PKT_Progress_Next() && !send_cmd( BT_SET_ANSWER, NULL)      )    nError = 7;
906
 
907
        if( !nError && PKT_Progress_Next() && !BTM222_SetBaud( Config.PKT_Baudrate))    nError = 8;     // Set BTM Baudrate
908
      //if( !nError && PKT_Progress_Next() && !BTM222_SetBaud( Baud_38400)) nError = 8;                 // * TEST/DEBUG * Set BTM Baudrate
909
 
910
 
911
        SetBaudUart1( Config.PKT_Baudrate );  // PKT wieder auf Original Baudrate setzen
912
 
913
        if( !nError )
914
        {
915
            bt_set_EchoAnswer( false );
916
            bt_mode = BLUETOOTH_SLAVE;
917
 
918
            WriteBTInitFlag();  // Init merken
919
            WriteBTSlaveFlag();
920
 
921
            bt_start();
922
            set_Modul_On( USB );
923
 
924
          //PKT_Message_P( text, error, timeout, beep, clearscreen)
925
            PKT_Message_P( PSTR("BTM222 Init OK!"), false, 1000, true, true);                           // OK: 1000 = max. 10 Sekunden anzeigen
926
            return;
927
        }
928
        else
929
        {
930
            set_Modul_On(USB);
931
 
932
          //PKT_Message( text, error, timeout, beep, clearscreen)
933
            PKT_Message( buffered_sprintf_P(PSTR("BTM Error: %d"),nError), true, 2000, true, true);     // ERROR: 2000 = max. 20 Sekunden anzeigen
934
            return;
935
        }
936
    }
937
 
938
    SetBaudUart1( Config.PKT_Baudrate );                                                                // die richtige UART Baudrate wiederherstellen !!
939
    set_Modul_On( USB );
940
    clear_key_all();
941
 
942
    return;
943
}
944
 
945
 
946
 
947
//######################################################################################
948
//######################################################################################
949
 
950
 
951
 
952
//--------------------------------------------------------------
953
//--------------------------------------------------------------
954
uint8_t bt_set_mode( const bt_mode_t mode )
955
{
956
    #ifdef DEBUG
957
    debug_pgm(PSTR("bt_setmode: set Mode"));
958
    #endif
959
 
960
    //if (update_comm_mode(0) == BT_DATA) // 30.1.2012 CB
961
    //  return false;
962
 
963
    if( mode == bt_mode )
964
        return true;
965
 
966
    if( mode == BLUETOOTH_MASTER )
967
    {
968
        comm_mode = BT_CMD;
969
        bt_set_EchoAnswer(true);
970
 
971
        if( send_cmd(BT_SET_MASTER, NULL) )
972
        {
973
            bt_mode = BLUETOOTH_MASTER;
974
            send_cmd(BT_DISABLE_AUTOCONNECT, NULL);
975
            WriteBTMasterFlag();
976
 
977
            #ifdef DEBUG
978
            debug_pgm(PSTR("bt_setmode: Master is set"));
979
            #endif
980
        }
981
    }
982
 
983
    if( mode == BLUETOOTH_SLAVE )
984
    {
985
        comm_mode = BT_CMD;
986
        bt_set_EchoAnswer(true);
987
 
988
        if( send_cmd(BT_ENABLE_AUTOCONNECT, NULL) )
989
        {
990
            bt_mode = BLUETOOTH_SLAVE;
991
            send_cmd(BT_SET_SLAVE, NULL);
992
            WriteBTSlaveFlag();
993
            bt_set_EchoAnswer(false);
994
            comm_mode = BT_CMD;
995
 
996
            #ifdef DEBUG
997
            debug_pgm(PSTR("bt_setmode: Slave is set"));
998
            #endif
999
        }
1000
    }
1001
 
1002
    //  if (bt_mode == BLUETOOTH_MASTER)  debug_pgm(PSTR("bt_mode:BLUETOOTH_MASTER "));
1003
    //  if (bt_mode == BLUETOOTH_SLAVE)  debug_pgm(PSTR("bt_mode:BLUETOOTH_SLAVE"));
1004
    //        if (mode == BLUETOOTH_MASTER)  debug_pgm(PSTR("mode:BLUETOOTH_MASTER "));
1005
    //        if (mode == BLUETOOTH_SLAVE)  debug_pgm(PSTR("mode:BLUETOOTH_SLAVE"));
1006
 
1007
    return (mode == bt_mode);
1008
}
1009
 
1010
 
1011
 
1012
//--------------------------------------------------------------
1013
//--------------------------------------------------------------
1014
uint16_t bt_receive( void *data, uint8_t length, uint16_t timeout_ms )
1015
{
1016
    uint8_t rec_length = 0;
1017
    uint8_t i = 0;
1018
 
1019
    //while_timeout(true, timeout_ms);
1020
    for( uint16_t __timeout = 0; __timeout++ <= true && (timeout_ms); _delay_ms(true ? 1 : 0))
1021
    {
1022
        if( i == length )
1023
            return true;
1024
 
1025
        uart_receive();
1026
 
1027
        if( fifo_is_empty(&in_fifo) )
1028
            continue;
1029
 
1030
        if( update_comm_mode(0) != BT_DATA)
1031
        {
1032
            #ifdef DEBUG
1033
            debug_pgm(PSTR("not connected"));
1034
            #endif
1035
 
1036
            return false;
1037
        }
1038
 
1039
 
1040
        if( timeout_ms == 0 )                            // We have a connection
1041
            timeout_ms += 2000;
1042
 
1043
        if( fifo_is_empty(&in_fifo) )
1044
            continue;
1045
 
1046
 
1047
        if( !rec_length )                                // Find starting point of packet
1048
        {
1049
            fifo_read( &in_fifo, (char *)&rec_length);
1050
 
1051
            if( rec_length != length )
1052
            {
1053
                rec_length = 0;
1054
            }
1055
            else
1056
            {
1057
                timeout_ms += 2000;
1058
            }
1059
        }
1060
        else
1061
        {
1062
            fifo_read( &in_fifo, (char *)data + i);
1063
            i++;
1064
        }
1065
    }
1066
    return false;
1067
}
1068
 
1069
 
1070
 
1071
 
1072
#ifndef SaveMem
1073
 
1074
 
1075
 
1076
//--------------------------------------------------------------
1077
//--------------------------------------------------------------
1078
uint16_t bt_send( void *data, const uint8_t length )
1079
{
1080
    if (update_comm_mode(0) == BT_CMD)
1081
        return false;
1082
 
1083
    uart_send((const char *)&length, 1);
1084
    uart_send((char *)data, length);
1085
    return (update_comm_mode(0) == BT_DATA);
1086
}
1087
 
1088
 
1089
#ifdef SQUIRREL
1090
 
1091
 
1092
 
1093
//--------------------------------------------------------------
1094
//--------------------------------------------------------------
1095
uint16_t bt_connect( const char *address )
1096
{
1097
    fifo_init(&in_fifo, bt_buffer, IN_FIFO_SIZE);
1098
    uart_receive();
1099
    fifo_clear(&in_fifo);
1100
 
1101
    // Maybe we already disconnected???
1102
    if (BT_DATA == update_comm_mode(0))
1103
    {
1104
        #ifdef DEBUG
1105
        debug_pgm(PSTR("We are still connected..."));
1106
        #endif
1107
        return false;
1108
    }
1109
 
1110
//    send_cmd(BT_TEST, NULL);
1111
 
1112
 
1113
    /*
1114
    if (!send_cmd(BT_DISABLE_AUTOCONNECT, address))
1115
        return false;
1116
    */
1117
 
1118
    send_cmd(BT_TEST, NULL);
1119
 
1120
    #ifdef DEBUG
1121
    debug_pgm (PSTR ("SET_ADD"));
1122
    #endif
1123
 
1124
    if (!send_cmd(BT_SET_ADDRESS, address))
1125
        return false;
1126
 
1127
    send_cmd(BT_TEST, NULL);
1128
 
1129
    #ifdef DEBUG
1130
    debug_pgm (PSTR ("CONNECT"));
1131
    #endif
1132
 
1133
    if (!send_cmd(BT_CONNECT, NULL))
1134
        return false;
1135
 
1136
    #ifdef DEBUG
1137
    debug_pgm (PSTR ("WAIT FOR COMM"));
1138
    #endif
1139
 
1140
    return (BT_DATA == update_comm_mode(20000));
1141
}
1142
 
1143
 
1144
 
1145
//--------------------------------------------------------------
1146
//--------------------------------------------------------------
1147
uint16_t bt_disconnect(void)
1148
{
1149
uint8_t RetVal = false;
1150
 
1151
    for( uint8_t i = 0; i < 4; i++)             // Switch to online cmd mode
1152
    {
1153
        const char plus = '+';
1154
        uart_send(&plus, 1);
1155
        _delay_ms(1000);
1156
    }
1157
 
1158
    comm_mode = BT_CMD;
1159
    EchoAnswerOn = true;
1160
    uart1_flush();                              // noch nicht verarbeitete Daten löschen
1161
    fifo_clear(&in_fifo);
1162
 
1163
 
1164
    if( !send_cmd(BT_DISCONNECT, NULL) )
1165
        RetVal = false;
1166
 
1167
    if( BT_CMD == update_comm_mode(2000) )      // neue Position um Disconnect-Antwort zu finden
1168
    {
1169
        fifo_clear(&in_fifo);
1170
        RetVal = true;
1171
    }
1172
 
1173
    if( !send_cmd(BT_CLEAR_ADDRESS, NULL) )
1174
        RetVal = false;
1175
 
1176
 
1177
//    if( BT_CMD == update_comm_mode(2000) )      // 31.7.2015 CB ursprüngliche Position, wurde verschoben
1178
//    {
1179
//        fifo_clear(&in_fifo);
1180
//        return true;
1181
//    }
1182
 
1183
    #ifdef DEBUG
1184
    debug_pgm(PSTR("Still in DATA??"));
1185
    #endif
1186
 
1187
    return RetVal;
1188
}
1189
 
1190
 
1191
 
1192
/*
1193
BTM-222 Softwareversion 4.35
1194
Inquiry Results:
1195
           111111111222222222233333333334
1196
01234567890123456789012345678901234567890
1197
 
1198
1  LE091452          0024-2C-BEB0CA
1199
2  E71 c             0024-7C-3EC9B9
1200
 
1201
BTM-222 Softwareversion 6.26
1202
Inquiry Results:
1203
1  E71 c  0024-7C-3EC9B9 N.A.
1204
2  LE091452  0024-2C-BEB0CA N.A.
1205
 
1206
*/
1207
 
1208
 
1209
//--------------------------------------------------------------
1210
//--------------------------------------------------------------
1211
void copy_mac(const char *src, char *dst)
1212
{
1213
    uint8_t off = 0;
1214
 
1215
    for (uint8_t i = 0; i < 40; i++)
1216
    {
1217
        if (src[i] == '-')
1218
            if (src[i+3] == '-')// MAC Adresse suchen
1219
            {
1220
                off = i-4;
1221
                break;
1222
            }
1223
    }
1224
 
1225
    for (uint8_t i = 0; i < 14; i++)
1226
    {
1227
        if (src[i + off] == '-')
1228
            off++;
1229
 
1230
        dst[i] = src[i + off];
1231
    }
1232
}
1233
 
1234
 
1235
 
1236
//--------------------------------------------------------------
1237
//--------------------------------------------------------------
1238
void copy_localID(const char *src, char *dst)
1239
{
1240
    uint8_t off = 0;
1241
 
1242
    //for (uint8_t i = 0; i < 40; i++)
1243
    //{
1244
    //    if (src[i] == '-') if (src[i+3] == '-')// MAC Adresse suchen
1245
    //                        {
1246
    //                          off = i-4;
1247
    //                          break;
1248
    //                        }
1249
    //}
1250
 
1251
    for (uint8_t i = 0; i < 14; i++)
1252
    {
1253
        if (src[i + off] == '-')
1254
            off++;
1255
 
1256
        dst[i] = src[i + off];
1257
    }
1258
}
1259
 
1260
 
1261
 
1262
//--------------------------------------------------------------
1263
//--------------------------------------------------------------
1264
void copy_DevName(const char *src, char *dst)
1265
{
1266
    uint8_t off = 0;
1267
    uint8_t i = 0;
1268
 
1269
    for (i = 0; i < 18; i++)
1270
    {
1271
        if (src[i] == ' ' && src[i+1] == ' ')
1272
        {
1273
            dst[i] = 0;  //Stringende
1274
            break; // nach zwei Leerzeichen ist der Name zuende
1275
        }
1276
        dst[i] = src[i + off];
1277
    }
1278
}
1279
 
1280
 
1281
 
1282
//--------------------------------------------------------------
1283
//--------------------------------------------------------------
1284
uint8_t bt_discover( void )
1285
{
1286
    char buffer[255];           //oversized, but who cares?
1287
    char *bufferhead  = buffer;
1288
    uint16_t devcount = 0;
1289
    uint16_t Timeout  = 10000;
1290
    uint8_t progress  = 0;
1291
    uint8_t  keyesc   = false;
1292
 
1293
 
1294
    #define BT_SEARCHTIME   7500        // = 75 Sekunden BT-Geraete suchen (60 werden vom BTM222 min. dafuer veranschlagt + 15 Sekunden Sicherheit)
1295
    #define PROGRESSUPDATE   100        // = 1 Sekunde - jede Sekunde die Progressbar updaten bei BT-Inquiry
1296
 
1297
 
1298
    //PKT_Progress_Init( max, yoffs,  width,height)
1299
    PKT_Progress_Init( 4 + ((BT_SEARCHTIME/PROGRESSUPDATE)+1), 0,  0,0);    // Anzahl der gesamten Progress Steps
1300
 
1301
 
1302
    PKT_Progress_Next();                                        // Progressbar: Extra 1
1303
 
1304
    //------------------------
1305
    //------------------------
1306
    if( !bt_set_mode( BLUETOOTH_MASTER) )
1307
        return 0;
1308
 
1309
    PKT_Progress_Next();                                        // Progressbar: Extra 2
1310
 
1311
    send_cmd( BT_TEST, NULL);
1312
 
1313
    fifo_clear( &in_fifo );
1314
 
1315
    if( !send_cmd( BT_FIND_DEVICES, NULL) )
1316
        return 0;
1317
 
1318
    PKT_Progress_Next();                                        // Progressbar: Extra 3
1319
 
1320
    //------------------------
1321
    //------------------------
1322
    do
1323
    {
1324
        uart_receive();
1325
        Timeout--;
1326
 
1327
        _delay_ms(1);
1328
 
1329
        //write_ndigit_number_u( 0,7,fifo_getcount( &in_fifo)  ,5,0,0);
1330
 
1331
        if( fifo_is_full(&in_fifo) )
1332
            break;
1333
 
1334
        if( get_key_short(1 << KEY_ESC) )       // 3. Taste
1335
        {
1336
            keyesc = true;
1337
            break;
1338
        }
1339
 
1340
    } while( (Timeout != 0) && (!fifo_strstr_pgm(&in_fifo, PSTR("Inquiry Results:\r\n"))) );
1341
 
1342
 
1343
    fifo_clear( &in_fifo );
1344
 
1345
 
1346
    //------------------------
1347
    //------------------------
1348
    if( Timeout!=0 && !keyesc )
1349
    {
1350
        // 25.06.2014 OG: umgestellt auf zeitliches Timeout anstatt einer Zaehlschleife
1351
 
1352
        timer3 = BT_SEARCHTIME;                                 // 75 Sekunden BT Device suchen
1353
 
1354
        while( timer3 && (!fifo_search( &in_fifo, PSTR("Inquiry End"))))
1355
        {
1356
            if( (timer3 / PROGRESSUPDATE) != progress )         // Progressbar: next step
1357
            {
1358
                progress = (timer3 / PROGRESSUPDATE);
1359
                PKT_Progress_Next();
1360
            }
1361
 
1362
            uart_receive();
1363
            _delay_ms(2);                                       // 25.06.2014 OG: diese Verzoegerung muss aktuell sein - sonst werden scheinbar keine Geraete gefunden :-(
1364
 
1365
            if( get_key_short(1 << KEY_ESC) )                   // 3. Taste
1366
            {
1367
                //keyesc = true;
1368
                break;
1369
            }
1370
        }
1371
    }
1372
 
1373
 
1374
    PKT_Progress_Next();                                        // Progressbar: Extra 4
1375
 
1376
 
1377
    //set_beep( 300, 0xffff, BeepNormal );  _delay_ms(3000);    // Test um das Ende der Progressbar zu ueberpruefen
1378
 
1379
 
1380
    //------------------------
1381
    //------------------------
1382
    while( !fifo_is_empty( &in_fifo) )
1383
    {
1384
        while( !fifo_cmp_pgm( &in_fifo, PSTR("\r\n")) )         // Get next line
1385
        {
1386
            fifo_read( &in_fifo, bufferhead);
1387
            bufferhead++;
1388
        }
1389
 
1390
        *bufferhead = 0;                                        // terminate string
1391
 
1392
        bufferhead = buffer;                                    // reset bufferhead
1393
 
1394
//        write_ndigit_number_u( 0,6,devcount ,5,0,0);
1395
//        set_beep( 300, 0xffff, BeepNormal );
1396
//        _delay_ms(1000);
1397
 
1398
 
1399
        if( strlen(buffer) == 0 )                               // the empty line before end of inquiry
1400
            continue;
1401
 
1402
        if( strstr_P( buffer, PSTR("Inquiry End")))
1403
        {
1404
            break;
1405
        }
1406
 
1407
        copy_DevName( &buffer[3], device_list[devcount].DevName);
1408
        copy_mac(     &buffer[3], device_list[devcount].mac);
1409
 
1410
        devcount++;
1411
 
1412
 
1413
        if( devcount >= NUTS_LIST )
1414
        {
1415
            break;
1416
        }
1417
 
1418
    } // end: while( !fifo_is_empty(&in_fifo) )
1419
 
1420
    clear_key_all();
1421
 
1422
    return devcount;
1423
}
1424
 
1425
 
1426
//--------------------------------------------------------------
1427
uint8_t bt_showsettings( void )
1428
{
1429
    char buffer[512];           //oversized, but who cares?
1430
    char *bufferhead  = buffer;
1431
    uint16_t Timeout  = 100;
1432
    bt_rxerror = 0;
1433
 
1434
    fifo_init( &in_fifo, bt_buffer, IN_FIFO_SIZE);
1435
    uart1_flush();
1436
 
1437
    PKT_Progress_Init( 4 , 0,  0,0);    // Anzahl der gesamten Progress Steps
1438
    PKT_Progress_Next();                                        // Progressbar: Extra 1
1439
 
1440
    if( !bt_set_mode( BLUETOOTH_MASTER) )
1441
        return 0;
1442
 
1443
//    if (EchoAnswerOn) ScrollBox_Push_P( MNORMAL, PSTR("1 E A on") ); else ScrollBox_Push_P( MNORMAL, PSTR("1 E A off") );
1444
 
1445
    PKT_Progress_Next();                                        // Progressbar: Extra 2
1446
 
1447
    EchoAnswerOn = true;
1448
    if( !send_cmd( BT_SHOW_SETTINGS, NULL) )
1449
      ScrollBox_Push_P( MNORMAL, PSTR("Command Error") );
1450
 
1451
    PKT_Progress_Next();                                        // Progressbar: Extra 3
1452
 
1453
    do
1454
    {
1455
        uart_receive();
1456
//        write_ndigit_number_u( 0,6,fifo_getcount( &in_fifo)  ,5,0,0);
1457
        Timeout--;
1458
        _delay_ms(2);
1459
        if( fifo_is_full(&in_fifo) )
1460
          {
1461
            ScrollBox_Push_P( MNORMAL, PSTR("Fifo is full") );
1462
            break;
1463
          }
1464
 
1465
    } while(Timeout != 0 );
1466
// } while( (Timeout != 0) && (!fifo_search(&in_fifo, PSTR("+++"))) );
1467
 
1468
    PKT_Progress_Next();                                        // Progressbar: Extra 4
1469
 
1470
    while( !fifo_is_empty( &in_fifo) )
1471
    {
1472
        while( !fifo_cmp_pgm( &in_fifo, PSTR("\r\n")) )         // Get next line
1473
        {
1474
            fifo_read( &in_fifo, bufferhead);
1475
            bufferhead++;
1476
        }
1477
        *bufferhead = 0;                                        // terminate string
1478
        bufferhead = buffer;                                    // reset bufferhead
1479
        if( strlen(buffer) == 0 )                               // the empty line before end of inquiry
1480
            continue;
1481
        ScrollBox_Push( MNORMAL, buffer );
1482
 
1483
    } // end: while( !fifo_is_empty(&in_fifo) )
1484
    clear_key_all();
1485
//    if (EchoAnswerOn) ScrollBox_Push_P( MNORMAL, PSTR("2 E A on") ); else ScrollBox_Push_P( MNORMAL, PSTR("2 E A off") );
1486
 
1487
    return true;
1488
}
1489
 
1490
 
1491
 
1492
 
1493
 
1494
//--------------------------------------------------------------
1495
//--------------------------------------------------------------
1496
/*
1497
uint16_t bt_discover_OLD( char result[8][12] )
1498
{
1499
    char buffer[255];   //oversized, but who cares?
1500
    char *bufferhead = buffer;
1501
    uint16_t pos = 0;
1502
    uint16_t Timeout = 10000;
1503
    uint16_t posC = 0;
1504
    uint8_t Result=false;
1505
 
1506
    PKT_Progress_Init( 25, 0,  0,0);                                    // 25 Progress Steps
1507
 
1508
    if (!bt_set_mode(BLUETOOTH_MASTER)) Result = false;
1509
    PKT_Progress_Next();                                                // Progressbar
1510
 
1511
    send_cmd(BT_TEST, NULL);
1512
    if( !send_cmd(BT_FIND_DEVICES, NULL)) Result = false;
1513
    PKT_Progress_Next();                                                // Progressbar
1514
 
1515
    do
1516
    {
1517
        uart_receive();
1518
        Timeout--;
1519
 
1520
        posC++;
1521
        _delay_ms(1);
1522
        write_ndigit_number_u(0,5,fifo_getcount(&in_fifo),5,0,0);
1523
        if (posC ==1000)
1524
        {
1525
            PKT_Progress_Next();                                        // Progressbar
1526
            posC = 0;
1527
        }
1528
 
1529
        if (fifo_is_full(&in_fifo)) break;
1530
 
1531
    } while( (Timeout != 0) && (!fifo_strstr_pgm(&in_fifo, PSTR("Inquiry Results:\r\n"))) );
1532
 
1533
 
1534
    if (Timeout==0)
1535
    {
1536
        Result = false;
1537
    }
1538
    else
1539
    {
1540
        for (uint16_t i = 0; i < 40000; i++)
1541
        {
1542
            if( (i % 2000) == 0 )
1543
                PKT_Progress_Next();                                    // Progressbar
1544
            uart_receive();
1545
            write_ndigit_number_u( 0,5,fifo_getcount(&in_fifo),5,0,0);
1546
            _delay_ms(1);
1547
        }
1548
    }
1549
 
1550
 
1551
    while( !fifo_is_empty(&in_fifo) )
1552
    {
1553
        // Get next line
1554
        while (!fifo_cmp_pgm(&in_fifo, PSTR("\r\n")))
1555
        {
1556
            fifo_read(&in_fifo, bufferhead);
1557
            bufferhead++;
1558
        }
1559
        // terminate string
1560
        *bufferhead = 0;
1561
 
1562
        //reset bufferhead
1563
        bufferhead = buffer;
1564
 
1565
        if (strlen(buffer) == 0)
1566
            continue;   //the empty line before end of inquiry
1567
 
1568
        if( strstr_P(buffer, PSTR("Inquiry End")))
1569
        //if (searchend)
1570
        {
1571
            fifo_clear(&in_fifo);
1572
            return true;
1573
        }
1574
 
1575
        copy_DevName(&buffer[3],device_list[pos].DevName);
1576
        copy_mac(&buffer[3], device_list[pos].mac);
1577
        pos++;
1578
 
1579
    } // end: while( !fifo_is_empty(&in_fifo) )
1580
 
1581
    return Result;
1582
}
1583
*/
1584
 
1585
 
1586
 
1587
 
1588
 
1589
 
1590
//--------------------------------------------------------------
1591
// ermittelt die MAC vom BTM2222
1592
//--------------------------------------------------------------
1593
uint8_t bt_getID( void )
1594
{
1595
    //char buffer[255];         //oversized, but who cares?
1596
    char buffer[80];        //oversized, but who cares?
1597
    char *bufferhead = buffer;
1598
    uint8_t i;
1599
 
1600
 
1601
    while_timeout( !fifo_strstr_pgm(&in_fifo, PSTR("r\n")), 2000)
1602
        uart_receive();
1603
 
1604
 
1605
    while( !fifo_is_empty(&in_fifo) )
1606
    {
1607
        while( !fifo_cmp_pgm( &in_fifo, PSTR("\r\n")) )     // Get next line
1608
        {
1609
              fifo_read( &in_fifo, bufferhead);
1610
              bufferhead++;
1611
        }
1612
 
1613
        *bufferhead = 0;                                    // terminate string
1614
 
1615
        bufferhead = buffer;                                // reset bufferhead
1616
 
1617
        if( strlen(buffer) == 0 )
1618
            continue;                                       // the empty line before end of inquiry
1619
 
1620
        copy_localID( &buffer[0], &localID[0] );
1621
 
1622
        for( i = 0; i < 13; i++)
1623
        {
1624
            //lcd_putc (i, 6, localID[i],0);
1625
            Config.bt_Mac[i] = localID[i];
1626
        }
1627
 
1628
 
1629
        if( fifo_strstr_pgm(&in_fifo, PSTR("OK")) )
1630
        {
1631
            fifo_clear( &in_fifo );
1632
            return true;
1633
        }
1634
        else
1635
        {
1636
            return false;
1637
        }
1638
 
1639
    } // end: if( !fifo_is_empty(&in_fifo) )
1640
 
1641
    return false;
1642
}
1643
 
1644
 
1645
 
1646
//--------------------------------------------------------------
1647
//--------------------------------------------------------------
1648
void bt_downlink_init(void)
1649
{
1650
    fifo_init( &in_fifo, bt_buffer, IN_FIFO_SIZE);
1651
    _delay_ms(100);
1652
    //      debug_pgm(PSTR("bt_downlink_init"));
1653
    uart_receive();
1654
    fifo_clear(&in_fifo);
1655
    //        send_cmd(BT_TEST, NULL);
1656
 
1657
    #ifdef DEBUG
1658
    debug_pgm(PSTR("downlink_init Start"));
1659
    #endif
1660
 
1661
    if( Config.BTIsSlave == true )          // nur Init wenn BT ist Slave
1662
    {
1663
        #ifdef DEBUG
1664
        debug_pgm(PSTR("downlink_init:try to set Master"));
1665
        #endif
1666
 
1667
        //comm_mode = BT_NOECHO;
1668
        //
1669
        //if (!send_cmd (BT_SET_ECHO,NULL))
1670
        //{
1671
        //  #ifdef DEBUG
1672
        //  debug_pgm(PSTR("downlink_init:Couldn't set Echo!"));
1673
        //  #endif
1674
        //}
1675
        //
1676
        //comm_mode = BT_NOANSWER;
1677
        //if (!send_cmd(BT_SET_ANSWER,NULL))
1678
        //{
1679
        //  #ifdef DEBUG
1680
        //  debug_pgm(PSTR("downlink_init:Couldn't set Answer!"));
1681
        //  #endif
1682
        //
1683
        //}
1684
 
1685
        comm_mode = BT_CMD;
1686
        //send_cmd(BT_TEST, NULL);
1687
 
1688
        bt_set_EchoAnswer(true);
1689
 
1690
        if( !bt_set_mode(BLUETOOTH_MASTER) )
1691
        {
1692
            #ifdef DEBUG
1693
            debug_pgm(PSTR("downlink_init:Couldn't set master!"));
1694
            #endif
1695
 
1696
            return;
1697
        }
1698
 
1699
        #ifdef DEBUG
1700
        debug_pgm(PSTR("downlink_init:master is set "));
1701
        #endif
1702
 
1703
        //WriteBTMasterFlag();  // Master merken
1704
        comm_mode = BT_CMD;
1705
    }
1706
    else
1707
    {
1708
        #ifdef DEBUG
1709
        debug_pgm(PSTR("downlink_init:Master was set"));
1710
        #endif
1711
 
1712
        comm_mode = BT_CMD;
1713
    }
1714
}
1715
 
1716
 
1717
 
1718
//--------------------------------------------------------------
1719
//--------------------------------------------------------------
1720
void bt_searchDevice( void )                              //Bluetoothgeräte suchen
1721
{
1722
    bt_devicecount = bt_discover();
1723
}
1724
 
1725
 
1726
 
1727
 
1728
//
1729
////--------------------------------------------------------------
1730
//uint16_t bt_receiveNMEA2(void)
1731
//{
1732
//
1733
//        char received;
1734
//        static uint8_t line_flag = 1;
1735
//        static char* ptr_write = bt_rx_buffer;
1736
//        uint16_t timeout_ms=2000;
1737
//
1738
////        while_timeout(true, timeout_ms) {
1739
//while(1){
1740
//
1741
//        uart_receive();
1742
//           if (fifo_is_empty(&in_fifo))
1743
//               continue;
1744
//        fifo_read(&in_fifo, &received);
1745
//        USART_putc(received);
1746
////        _delay_ms(1);
1747
//    }
1748
////
1749
//return true;
1750
//}
1751
 
1752
 
1753
 
1754
#endif
1755
#endif
1756
//#endif
1757
 
1758
 
1759
#endif  // end: #ifdef USE_BLUETOOTH
1760