mbed base bard check program for BlueTooth USB dongle module (3 switches, 6 leds, I2C LCD, A/D)

Dependencies:   USBHost mbed

Fork of BTstack by Norimasa Okamoto

Revision:
3:7b7d1273e2d5
Parent:
1:b657594559be
--- a/spp_demo.cpp	Fri Mar 22 22:35:57 2013 +0000
+++ b/spp_demo.cpp	Mon Oct 17 00:25:18 2016 +0000
@@ -1,7 +1,50 @@
-#if 0
+#if 1
 /*
  * spp_demo
+ VS-02 android remotecontroler application
+ mbed LED1,LED2,LED3,LED4 on/off control
+ serial baudrate = 921600,8bit,stop 1bit,none parity
  */
+//mbed pin layout
+//1   GND
+//2   
+//3   
+//4   
+//5   SD SDI
+//6   SD SDO
+//7   SD SCK
+//8   SD CS
+//9   I2C sda
+//10  I2C sci
+//11  SPI mosi
+//12  SPI miso
+//13  SPI sck
+//14  SW0
+//15  SW1
+//16  SW2
+//17  Vr
+//18  D/A
+//19  Ain1
+//20  
+//21  PWMOut1
+//22  PWMOut2
+//23  PWMOut3
+//24  PWMOut4
+//25  PWMOut5
+//26  PWMOut6
+//27  LCD scl : ACM1602NI-4(ACM1602NI 1Vss,2Vdd,3Vo,4SCL,5SDA,6LED+,7LED-)
+//28  LCD sda : ACM1602NI-5
+//29  
+//30  
+//31  USB D+
+//32  USB D-
+//
+/*
+0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
+0   0   0   0   1       S0  S1  S2      1   0   2   3   "LeftArrow" "RightArrow"
+1                                                               
+*/
+
 #include "mbed.h"
 #include <btstack/hci_cmds.h>
 #include <btstack/run_loop.h>
@@ -18,15 +61,57 @@
 #include "debug.h"
 #include "bd_addr.h"  // class bd_addr
 
+#include "ACM1602NI.h"
+//#include "SDFileSystem.h"
+
 Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1), led2(LED2), led3(LED3);
+
+DigitalOut brd_led1(LED1), brd_led2(LED2), brd_led3(LED3), brd_led4(LED4);
+
+DigitalOut led1(p21);
+DigitalOut led2(p22);
+DigitalOut led3(p23);
+DigitalOut led4(p24);
+DigitalOut led5(p25);
+DigitalOut led6(p26);
+DigitalIn  sw1(p14);
+DigitalIn  sw2(p15);
+DigitalIn  sw3(p16);
+AnalogIn   vr(p17);
+
+/*
+SDFileSystem *Sd;
+FILE *fp;
+*/
 
-#define HEARTBEAT_PERIOD_MS 500
+static Timer timer;
+static int VSC2btn[20];//VS-C2 button push then 1, other 0
+//0:left arrow up  
+//1:left arrow right
+//2:left arrow down
+//3:left arrow left
+//4:right arrow up
+//5:right arrow right
+//6:right arrow down
+//7:right arrow left
+//8:select
+//9:start
+    static int module_status = 0;//0 timer set mode, 1 timer run mode, 2 timer complete
+    static int time_cnt = 0;//use run mode timer counter
+    static int time_set = 0;//use set mode timer counter
+
+
+//LCD class
+ACM1602NI lcd(p28, p27); //sda scl
+
+#define HEARTBEAT_PERIOD_MS 100
 
 static uint8_t   rfcomm_channel_nr = 1;
 static uint16_t  rfcomm_channel_id = 0;
 static uint8_t   spp_service_buffer[128];
 
+static void data_recv(uint8_t *packet, uint16_t size);
+
 // Bluetooth logic
 static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
     bd_addr_t event_addr;
@@ -40,7 +125,7 @@
                 case BTSTACK_EVENT_STATE:
                     // bt stack activated, get started - set local name
                     if (packet[2] == HCI_STATE_WORKING) {
-                        hci_send_cmd(&hci_write_local_name, "mbed");
+                        hci_send_cmd(&hci_write_local_name, "mbed seminar");
                     }
                     break;
                 
@@ -107,27 +192,348 @@
                     log_info("rfcomm_send_internal -> error %d", err);
                 }
             }
-            led3 = !led3;
+            
+            data_recv(packet, size);
+            
+//            led3 = !led3;
             break;
         default:
             break;
     }
 }
 
+static void data_recv(uint8_t *packet, uint16_t size){
+    int i;
+    static int timerp[12];
+    static int timers[12];
+//    char ind[4];
+    log_info("packet(size:%d) = ", size);
+
+//recive char display mode
+//for analys recieve char
+//    for (i = 0; i < size; i++){
+//        log_info("%c", packet[i]);
+//    }
+//    log_info("\n\r");
+
+
+//VS-02 remote controler corss key
+//packet
+//  9 right arrow: 1 up(triangle), 2 right(circle), 4 down(closs), 8 left(quad)
+// 10 L R Button : 1 L2, 2 R2, 4 L1, 8 R1
+// 12 left arrow: 1 up, 2 right, 4 down, 8 left
+// 13 1 select, 2 R3, 4 L3, 8 start
+    if(packet[0] == 'w'){
+        log_info("%c", packet[9]);
+        log_info("%c", packet[10]);
+        log_info("%c", packet[12]);
+        log_info("%c", packet[13]);
+        switch (packet[9]) {
+            case '1':
+                timers[4] = timerp[4];
+                timerp[4] = timer.read_ms();
+                log_info("%d", timerp[4]-timers[4]);
+                if((timerp[4]-timers[4])>150){
+                    VSC2btn[4]=1;
+                }
+                break;
+            case '2':
+                timers[5] = timerp[5];
+                timerp[5] = timer.read_ms();
+                log_info("%d", timerp[5]-timers[5]);
+                if((timerp[5]-timers[5])>150){
+                    VSC2btn[5]=1;
+                }
+                break;
+            case '4':
+                timers[6] = timerp[6];
+                timerp[6] = timer.read_ms();
+                log_info("%d", timerp[6]-timers[6]);
+                if((timerp[6]-timers[6])>150){
+                    VSC2btn[6]=1;
+                }
+                break;
+            case '8':
+                timers[7] = timerp[7];
+                timerp[7] = timer.read_ms();
+                log_info("%d", timerp[7]-timers[7]);
+                if((timerp[7]-timers[7])>150){
+                    VSC2btn[7]=1;
+                }
+                break;
+            case '0':
+                VSC2btn[4]=0;
+                VSC2btn[5]=0;
+                VSC2btn[6]=0;
+                VSC2btn[7]=0;
+                break;
+            
+        }
+        switch (packet[12]) {
+            case '1':
+                timers[0] = timerp[0];
+                timerp[0] = timer.read_ms();
+                log_info("%d", timerp[0]-timers[0]);
+                if((timerp[0]-timers[0])>150){
+                    VSC2btn[0]=1;
+                }
+                break;
+            case '2':
+                timers[1] = timerp[1];
+                timerp[1] = timer.read_ms();
+                log_info("%d", timerp[1]-timers[1]);
+                if((timerp[1]-timers[1])>150){
+                    VSC2btn[1]=1;
+                }
+                break;
+            case '4':
+                timers[2] = timerp[2];
+                timerp[2] = timer.read_ms();
+                log_info("%d", timerp[2]-timers[2]);
+                if((timerp[2]-timers[2])>150){
+                    VSC2btn[2]=1;
+                }
+                break;
+            case '8':
+                timers[3] = timerp[3];
+                timerp[3] = timer.read_ms();
+                log_info("%d", timerp[3]-timers[3]);
+                if((timerp[3]-timers[3])>150){
+                    VSC2btn[3]=1;
+                }
+                break;
+            case '0':
+                VSC2btn[0]=0;
+                VSC2btn[1]=0;
+                VSC2btn[2]=0;
+                VSC2btn[3]=0;
+                break;
+        }
+        switch (packet[13]) {
+            case '1':
+                timers[8] = timerp[8];
+                timerp[8] = timer.read_ms();
+                log_info("%d", timerp[8]-timers[8]);
+                if((timerp[8]-timers[8])>150){
+                    VSC2btn[8]=1;
+                }
+                break;
+            case '8':
+                timers[9] = timerp[9];
+                timerp[9] = timer.read_ms();
+                log_info("%d", timerp[9]-timers[9]);
+                if((timerp[9]-timers[9])>150){
+                    VSC2btn[9]=1;
+                }
+                break;
+            case '0':
+                VSC2btn[8]=0;
+                VSC2btn[9]=0;
+                break;
+        }
+//
+//
+        
+    }
+    log_info("\n\r");
+}
+
+//switch status 0:off, 1:posEdge, 2:on, 3:negEdge
+int sw_st[3];
+int sw_cnt[3];//past counter
+
+void chksw(void)
+{
+    static int ssw[3], psw[3], cnt[3], tcnt[3];
+    int i;
+    psw[0] = sw1;
+    psw[1] = sw2;
+    psw[2] = sw3;
+//switch status 0:off, 1:posEdge, 2:on, 3:negEdge
+    for(i = 0; i < 3; i++){
+        if((ssw[i] == 0)&&(psw[i] == 0)){
+            sw_st[i] = 2;//on
+            tcnt[i] ++;
+            sw_cnt[i] = tcnt[i];
+        }
+        if((ssw[i] == 0)&&(psw[i] == 1)){
+            sw_st[i] = 3;//negEdge
+            cnt[i] = tcnt[i];
+            tcnt[i] = 0;
+            sw_cnt[i] = tcnt[i];
+        }
+        if((ssw[i] == 1)&&(psw[i] == 1)){
+            sw_st[i] = 0;//off
+            tcnt[i] ++;
+        }
+        if((ssw[i] == 1)&&(psw[i] == 0)){
+            sw_st[i] = 1;//posEdge
+            cnt[i] = tcnt[i];
+            tcnt[i] = 0;
+            sw_cnt[i] = tcnt[i];
+        }
+        ssw[i] = psw[i];
+    }
+}
+
+
 static void  heartbeat_handler(struct timer *ts){
+    static int counter = 0, tmp_counter=0;
+    static int c_pos = 2;//   4:32:10
+                         //   0:55:00
+    static char line1[20], line2[20];
+
     run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
     run_loop_add_timer(ts);
-    led2 = !led2;
+
+    brd_led2 = !brd_led2;
+    counter ++;
+//    tmp_counter = time_cnt;
+
+    if(module_status == 1){
+    }
+    
+    if(module_status == 0){//set mode
+//        int cd[5]={1,10,60,600,3600};
+//        if(VSC2btn[3] == 1){//left arrow left
+//            c_pos++;
+//            if(c_pos == 5) c_pos=0;
+//            VSC2btn[3] = 0;
+//        }
+//        if(VSC2btn[1] == 1){//left arrow right
+//            c_pos--;
+//            if(c_pos == -1) c_pos=4;
+//            VSC2btn[1] = 0;
+//        }
+/*
+        if(VSC2btn[0] == 1){//left arrow up
+            time_set = time_set + cd[c_pos];
+            VSC2btn[0] = 0;
+        }
+        if(VSC2btn[2] == 1){//left arrow down
+            if(time_set - cd[c_pos]>=0){
+                time_set = time_set - cd[c_pos];
+            }
+            VSC2btn[2] = 0;
+        }
+*/
+        if(VSC2btn[9] == 1){//start
+            time_cnt = time_set;//set time
+            //relay on
+            module_status = 1;
+            VSC2btn[9] = 0;
+        }
+//        int sec=(time_set%3600)%60, min=(time_set%3600)/60, time = time_set/3600;
+    }else if(module_status == 1){//run mode
+//        int sec=(time_cnt%3600)%60, min=(time_cnt%3600)/60, time = time_cnt/3600;
+//        strcpy(line1, "timer run mode ");
+//        sprintf(line2, "%2d:%2d:%2d       ", time, min, sec);
+//        if(time_cnt == 0){
+//            module_status = 2;
+//        }
+        if(VSC2btn[6] == 1){//closs
+            time_set = 0;
+            module_status = 0;
+            VSC2btn[6] = 0;
+        }
+                    
+    }else if(module_status == 2){//complete
+        strcpy(line1, "timer up!!      ");
+        sprintf(line2, "push start");
+        //relay off
+
+        if(VSC2btn[9] == 1){//start
+            module_status = 0;//
+            VSC2btn[9] = 0;
+        }
+    }
+    chksw();
+    if(sw_st[0] == 1){
+        led1 = 1;
+    }else{
+        led1 = 0;
+    }
+    if(sw_st[1] == 1){
+        led2 = 1;
+    }else{
+        led2 = 0;
+    }
+    if(sw_st[2] == 1){
+        led3 = 1;
+    }else{
+        led3 = 0;
+    }
+    if(sw_st[0] == 2){
+        led4 = 1;
+    }else{
+        led4 = 0;
+    }
+    if(sw_st[1] == 2){
+        led5 = 1;
+    }else{
+        led5 = 0;
+    }
+    if(sw_st[2] == 2){
+        led6 = 1;
+    }else{
+        led6 = 0;
+    }
+    sprintf(line2, "%4d:%d%d%d %4d %d%d", 
+    time_cnt, sw_st[0], sw_st[1], sw_st[2], (int)(vr.read() * 1023), 
+    VSC2btn[0]/*left up*/, VSC2btn[2]/*left down*/ );
+
+    {
+        //display LCD
+        lcd.locate(0,0);
+        lcd.printf(line1);
+        lcd.locate(0,1);
+        lcd.printf(line2);        
+    }
+/*
+    {
+        Sd = new SDFileSystem(p5, p6, p7, p8, "sd");     
+        // new sd object *** unable to draw sd card ***
+        
+        //write to SD card
+        FILE *fp = fopen("/sdtest.txt", "w");
+        if(fp == NULL) {
+            error("Could not open file for write\n\r");
+        }else{
+            fprintf(fp, line2);
+            fclose(fp); 
+        }
+        delete Sd;// delete object *** enable to draw sd card ***
+    }
+*/
 } 
 
+
+
+Ticker t1s;
+void t1scall(void)
+{
+    if(module_status == 1){
+        time_cnt ++;
+    }
+}
+
 // main
 int main(void)
 {
+    t1s.attach(&t1scall, 1);
+    timer.start();
+    
     pc.baud(921600);
-    log_info("%s\n", __FILE__);
+    pc.printf("mbed BoardChk1");    
+    lcd.printf("mbed BoardChk1");
 
+    sw1.mode(PullUp);
+    sw2.mode(PullUp);
+    sw3.mode(PullUp);
+
+    log_info("%s\n", __FILE__);
     // init LEDs
-    led1 = led2 = led3 = 1;
+    brd_led1 = brd_led2 = brd_led3 = brd_led4 = 0;
     
     /// GET STARTED with BTstack ///
     btstack_memory_init();