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

Files at this revision

API Documentation at this revision

Comitter:
tamaki
Date:
Mon Oct 17 00:25:18 2016 +0000
Parent:
2:871b41f4789e
Commit message:
mbed base bard check program
; for BlueTooth USB dongle module
; (3 switches, 6 leds, I2C LCD, A/D)

Changed in this revision

ACM1602NI.cpp Show annotated file Show diff for this revision Revisions of this file
ACM1602NI.h Show annotated file Show diff for this revision Revisions of this file
led_counter.cpp Show diff for this revision Revisions of this file
mouse_demo.cpp Show diff for this revision Revisions of this file
spp_counter.cpp Show diff for this revision Revisions of this file
spp_demo.cpp Show annotated file Show diff for this revision Revisions of this file
spp_flowcontrol.cpp Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACM1602NI.cpp	Mon Oct 17 00:25:18 2016 +0000
@@ -0,0 +1,130 @@
+/* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01
+ * Copyright 2013, Takuo WATANABE (wtakuo)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ 
+#include "mbed.h"
+#include "ACM1602NI.h"
+ 
+#define I2C_SUCCESS 0
+#define I2C_FAILURE 1
+ 
+ACM1602NI::ACM1602NI(PinName sda, PinName scl) : _i2c(sda, scl)
+{
+    init();
+}
+ 
+ACM1602NI::ACM1602NI(I2C &i2c) : _i2c(i2c)
+{
+    init();
+}
+ 
+void ACM1602NI::init()
+{
+    writeCommand(0x01);
+    wait_ms(i2c_command_wait_ms);
+    writeCommand(0x38);
+    wait_ms(i2c_command_wait_ms);
+    writeCommand(0x0f);
+    wait_ms(i2c_command_wait_ms);
+    writeCommand(0x06);
+    wait_ms(i2c_command_wait_ms);
+    locate(0, 0);
+}
+ 
+int ACM1602NI::writeBytes(const char *data, int length, bool repeated)
+{
+    wait_us(i2c_bit_wait_us);
+    _i2c.start();
+    for (int i = 0; i < length; i++) {
+        wait_us(i2c_bit_wait_us);
+        if (_i2c.write(data[i]) != 1) {
+            wait_us(i2c_bit_wait_us);
+            _i2c.stop();
+            return I2C_FAILURE;
+        }
+    }
+    if (!repeated) {
+        wait_us(i2c_bit_wait_us);
+        _i2c.stop();
+    }
+    return I2C_SUCCESS;
+}
+ 
+void ACM1602NI::character(int column, int row, int c)
+{
+    writeCommand(address(column, row));
+    writeData(c);
+}
+ 
+void ACM1602NI::cls()
+{
+    writeCommand(0x01);
+    wait_ms(i2c_command_wait_ms);
+    locate(0, 0);
+}
+ 
+void ACM1602NI::locate(int column, int row)
+{
+    _column = column;
+    _row = row;
+}
+ 
+int ACM1602NI::_putc(int value)
+{
+    if (value == '\n') {
+        _column = 0;
+        _row = (_row + 1) % rows();
+    } else {
+        character(_column, _row, value);
+        _column++;
+        if (_column >= columns()) {
+            _column = 0;
+            _row = (_row + 1) % rows();
+        }
+    }
+    return value;
+}
+ 
+int ACM1602NI::_getc()
+{
+    return -1;
+}
+ 
+void ACM1602NI::writeCommand(int command)
+{
+    char bs[3] = { i2c_addr, 0x00, command };
+    writeBytes(bs, 3);
+}
+ 
+void ACM1602NI::writeData(int data)
+{
+    char bs[3] = { i2c_addr, 0x80, data };
+    writeBytes(bs, 3);
+}
+ 
+int ACM1602NI::address(int column, int row)
+{
+    return 0x80 + row * 0x40 + column;
+}
+ 
+int ACM1602NI::columns()
+{
+    return display_columns;
+}
+ 
+int ACM1602NI::rows()
+{
+    return display_rows;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACM1602NI.h	Mon Oct 17 00:25:18 2016 +0000
@@ -0,0 +1,95 @@
+/* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01
+ * Copyright 2013, Takuo WATANABE (wtakuo)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ 
+#ifndef ACM1602NI_H
+#define ACM1602NI_H
+ 
+#include "mbed.h"
+ 
+/** An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01.
+ * The device does not work with default I2C library due to its slow I2C responce.
+ * This library adds some extra waits so that the device can answer to the I2C commands.
+ * The interface is basically the same as TextLCD by Simon Ford.
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "ACM1602NI.h"
+ *
+ * // p9: sda, p10: scl
+ * ACM1602NI lcd(p9, p10);
+ *
+ * int main() {
+ *     lcd.printf("Hello, World!\n");
+ *     lcd.printf("ACM1602NI\n");
+ * }
+ * @endcode
+ */
+class ACM1602NI : public Stream
+{
+public:
+    /** Create an ACM1602NI object connected to the specified I2C pins.
+     *
+     * @param sda   The I2C data pin
+     * @param scl   The I2C clock pin
+     */
+    ACM1602NI(PinName sda, PinName scl);
+ 
+    /** Create an ACM1602NI object connected to the specified I2C port.
+     *
+     * @param i2c   The I2C port to connect to
+     */
+    ACM1602NI(I2C &i2c);
+ 
+    /** Initialize the device and clear screen
+     */
+    void init();
+ 
+    /** Locate to a screen column and row
+     *
+     * @param column  The horizontal position from the left, indexed from 0
+     * @param row     The vertical position from the top, indexed from 0
+     */
+    void locate(int column, int row);
+ 
+    /** Clear the screen and locate to 0,0 */
+    void cls();
+ 
+    int rows();
+    int columns();
+ 
+protected:
+    virtual int _putc(int value);
+    virtual int _getc();
+ 
+    int address(int column, int raw);
+    void character(int column, int row, int c);
+    int writeBytes(const char *data, int length, bool repeated = false);
+    void writeCommand(int command);
+    void writeData(int data);
+ 
+    static const int i2c_addr = 0x50 << 1;
+    static const int i2c_bit_wait_us = 20;
+    static const int i2c_command_wait_ms = 4;
+ 
+    static const int display_columns = 16;
+    static const int display_rows = 2;
+ 
+    I2C _i2c;
+    int _column, _row;
+};
+ 
+#endif
+ 
--- a/led_counter.cpp	Fri Mar 22 22:35:57 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-#if 0
-//*****************************************************************************
-//
-// led_counter demo - uses the BTstack run loop to blink an LED
-//
-//*****************************************************************************
-#include "mbed.h"
-#include <btstack/run_loop.h>
-
-Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1), led2(LED2);
-
-#define HEARTBEAT_PERIOD_MS 1000
-
-static void  heartbeat_handler(struct timer *ts){
-
-    // increment counter
-    static int counter = 0;
-    char lineBuffer[30];
-    sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter);
-    printf(lineBuffer);
-    
-    // toggle LED
-    led2 = !led2;
-    
-    run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(ts);
-} 
-
-// main
-int main(void)
-{
-    pc.baud(921600);
-    printf("%s\n", __FILE__);
-
-    // init LEDs
-    led1 = led2 = 1;
-    
-    /// GET STARTED with BTstack ///
-    run_loop_init(RUN_LOOP_EMBEDDED);
-        
-    // set one-shot timer
-    timer_source_t heartbeat;
-    heartbeat.process = &heartbeat_handler;
-    run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(&heartbeat);
-    
-    printf("Run...\n\r");
-
-    // go!
-    run_loop_execute();    
-    
-    // happy compiler!
-    return 0;
-}
-#endif
\ No newline at end of file
--- a/mouse_demo.cpp	Fri Mar 22 22:35:57 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,118 +0,0 @@
-#if 1
-/* mouse_demo.cpp */
-#include "mbed.h"
-#include <btstack/run_loop.h>
-#include <btstack/hci_cmds.h>
-#include "hci.h"
-#include "l2cap.h"
-#include "debug.h"
-#include "bd_addr.h"  // class bd_addr
-Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1), led2(LED2), led3(LED3);
-
-#define INQUIRY_INTERVAL 15
-
-bd_addr addr;
-
-static void hid_process_packet(uint8_t* report, int size)
-{
-    if (report[0] == 0xa1 && report[1] == 0x02) {
-        led1 = report[2] & 0x01; // left
-        led2 = report[2] & 0x02; // right
-        led3 = report[2] & 0x04; // center
-    }
-    hexdump(report, size);
-}
-
-static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
-    
-    if (packet_type == HCI_EVENT_PACKET && packet[0] == L2CAP_EVENT_CHANNEL_OPENED){
-        if (packet[2]) {
-            log_info("Connection failed\n");
-            return;
-        }
-        log_info("Connected\n");
-    }
-    if (packet_type == L2CAP_DATA_PACKET){
-        // handle input
-        log_info("HID report, size %u\n", size);
-        hid_process_packet(packet, size);
-    }
-}
-
-static void packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
-    if (packet_type == HCI_EVENT_PACKET) {
-        switch (packet[0]) {
-            case BTSTACK_EVENT_STATE:
-                // bt stack activated, get started - set local name
-                if (packet[2] == HCI_STATE_WORKING) {
-                    hci_send_cmd(&hci_write_authentication_enable, 1);
-                }
-                break;
-                    
-            case HCI_EVENT_INQUIRY_RESULT:
-                // ignore none mouses
-                if ((packet[12] & 0x80) != 0x80 || packet[13] != 0x25) break;
-                addr.data(&packet[3]);
-                log_info("Mouse addr: %s\n", addr.to_str());
-                hci_send_cmd(&hci_inquiry_cancel);
-                break;
-                    
-            case HCI_EVENT_INQUIRY_COMPLETE:
-                log_info("No mouse found :(\n");
-                break;
-                
-            case HCI_EVENT_LINK_KEY_REQUEST:
-                // deny link key request
-                hci_send_cmd(&hci_link_key_request_negative_reply, addr.data());
-                break;
-                    
-            case HCI_EVENT_PIN_CODE_REQUEST:
-                // inform about pin code request
-                log_info("Enter 0000\n");
-                hci_send_cmd(&hci_pin_code_request_reply, addr.data(), 4, "0000");
-                break;
-                    
-            case HCI_EVENT_COMMAND_COMPLETE:
-                if (COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable)){
-                    log_info("Inquiry\n");
-                    hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
-                }
-                if (COMMAND_COMPLETE_EVENT(packet, hci_inquiry_cancel) ) {
-                    // inq successfully cancelled
-                    log_info("Connecting\n");
-                    l2cap_create_channel_internal(NULL, l2cap_packet_handler, addr.data(), PSM_HID_INTERRUPT, 150);
-                }
-                break;
-        }
-    }
-}
-
-int main(void){
-    pc.baud(921600);
-    log_info("%s\n", __FILE__);
-
-    // init LEDs
-    led1 = led2 = led3 = 1;
-    
-    // GET STARTED
-    run_loop_init(RUN_LOOP_EMBEDDED);
-
-    // init HCI
-    hci_transport_t* transport = hci_transport_usb_instance();
-    remote_device_db_t* remote_db = (remote_device_db_t *)&remote_device_db_memory;
-    hci_init(transport, NULL, NULL, remote_db);
-
-    // init L2CAP
-    l2cap_init();
-    l2cap_register_packet_handler(packet_handler);
-
-    // turn on!, send RESET command
-    hci_power_control(HCI_POWER_ON);
-            
-    // go!
-    run_loop_execute();    
-    
-    return 0;
-}
-#endif
\ No newline at end of file
--- a/spp_counter.cpp	Fri Mar 22 22:35:57 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,184 +0,0 @@
-#if 0
-//*****************************************************************************
-//
-// spp_counter demo - it provides a SPP and sends a counter every second
-//
-// it doesn't use the LCD to get down to a minimal memory footpring
-//
-//*****************************************************************************
-#include "mbed.h"
-#include <btstack/hci_cmds.h>
-#include <btstack/run_loop.h>
-#include <btstack/sdp_util.h>
-
-#include "hci.h"
-#include "l2cap.h"
-#include "btstack_memory.h"
-#include "remote_device_db.h"
-#include "rfcomm.h"
-extern "C" {
-#include "sdp.h"
-}
-#include "config.h"
-#include "debug.h"
-#include "bd_addr.h"  // class bd_addr
-
-Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1), led2(LED2);
-
-#define HEARTBEAT_PERIOD_MS 1000
-
-static uint8_t   rfcomm_channel_nr = 1;
-static uint16_t  rfcomm_channel_id = 0;
-static uint8_t   spp_service_buffer[128];
-
-// 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;
-    uint8_t   rfcomm_channel_nr;
-    uint16_t  mtu;
-    
-    switch (packet_type) {
-        case HCI_EVENT_PACKET:
-            switch (packet[0]) {
-                    
-                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-Demo");
-                    }
-                    break;
-                
-                case HCI_EVENT_COMMAND_COMPLETE:
-                    if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){
-                        bt_flip_addr(event_addr, &packet[6]);
-                        log_info("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr));
-                        break;
-                    }
-                    if (COMMAND_COMPLETE_EVENT(packet, hci_write_local_name)){
-                        hci_discoverable_control(1);
-                        break;
-                    }
-                    break;
-
-                case HCI_EVENT_LINK_KEY_REQUEST:
-                    // deny link key request
-                    log_info("Link key request\n\r");
-                    bt_flip_addr(event_addr, &packet[2]);
-                    hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
-                    break;
-                    
-                case HCI_EVENT_PIN_CODE_REQUEST:
-                    // inform about pin code request
-                    log_info("Pin code request - using '0000'\n\r");
-                    bt_flip_addr(event_addr, &packet[2]);
-                    hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
-                    break;
-                
-                case RFCOMM_EVENT_INCOMING_CONNECTION:
-                    // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
-                    bt_flip_addr(event_addr, &packet[2]); 
-                    rfcomm_channel_nr = packet[8];
-                    rfcomm_channel_id = READ_BT_16(packet, 9);
-                    log_info("RFCOMM channel %u requested for %s\n\r", rfcomm_channel_nr, bd_addr_to_str(event_addr));
-                    rfcomm_accept_connection_internal(rfcomm_channel_id);
-                    break;
-                    
-                case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
-                    // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
-                    if (packet[2]) {
-                        log_info("RFCOMM channel open failed, status %u\n\r", packet[2]);
-                    } else {
-                        rfcomm_channel_id = READ_BT_16(packet, 12);
-                        mtu = READ_BT_16(packet, 14);
-                        log_info("\n\rRFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n\r", rfcomm_channel_id, mtu);
-                    }
-                    break;
-                    
-                case RFCOMM_EVENT_CHANNEL_CLOSED:
-                    rfcomm_channel_id = 0;
-                    break;
-                
-                default:
-                    break;
-            }
-            break;
-                        
-        default:
-            break;
-    }
-}
-
-static void  heartbeat_handler(struct timer *ts){
-
-    if (rfcomm_channel_id){
-        static int counter = 0;
-        char lineBuffer[32];
-        snprintf(lineBuffer, sizeof(lineBuffer), "counter %04u\n\r", ++counter);
-        log_info("%s\n", lineBuffer);
-        int err = rfcomm_send_internal(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer));
-        if (err) {
-            log_info("rfcomm_send_internal -> error %d", err);
-        }
-    }
-    
-    run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(ts);
-    led2 = !led2;
-} 
-
-// main
-int main(void)
-{
-    pc.baud(921600);
-    log_info("%s\n", __FILE__);
-
-
-    // init LEDs
-    led1 = led2 = 1;
-    
-    /// GET STARTED with BTstack ///
-    btstack_memory_init();
-    run_loop_init(RUN_LOOP_EMBEDDED);
-    
-    // init HCI
-    hci_transport_t    * transport = hci_transport_usb_instance();
-    remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory;
-    hci_init(transport, NULL, NULL, remote_db);
-    
-    // init L2CAP
-    l2cap_init();
-    l2cap_register_packet_handler(packet_handler);
-    
-    // init RFCOMM
-    rfcomm_init();
-    rfcomm_register_packet_handler(packet_handler);
-    rfcomm_register_service_internal(NULL, rfcomm_channel_nr, 100);  // reserved channel, mtu=100
-
-    // init SDP, create record for SPP and register with SDP
-    sdp_init();
-    memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
-    service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer;
-    sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, 1, "SPP Counter");
-    log_info("SDP service buffer size: %u\n", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record)));
-    sdp_register_service_internal(NULL, service_record_item);
-    
-    // set one-shot timer
-    timer_source_t heartbeat;
-    heartbeat.process = &heartbeat_handler;
-    run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(&heartbeat);
-    
-    log_info("Run...\n\r");
-
-     // turn on!
-    hci_power_control(HCI_POWER_ON);
-
-    // go!
-    run_loop_execute();    
-    
-    // happy compiler!
-    return 0;
-}
-
-#endif
\ No newline at end of file
--- 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();
--- a/spp_flowcontrol.cpp	Fri Mar 22 22:35:57 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,178 +0,0 @@
-#if 0
-//*****************************************************************************
-//
-// spp_counter demo - it provides a SPP and sends a counter every second
-//
-// it doesn't use the LCD to get down to a minimal memory footpring
-//
-//*****************************************************************************
-#include "mbed.h"
-#include <btstack/hci_cmds.h>
-#include <btstack/run_loop.h>
-#include <btstack/sdp_util.h>
-#include "hci.h"
-#include "l2cap.h"
-#include "btstack_memory.h"
-#include "remote_device_db.h"
-#include "rfcomm.h"
-#include "sdp.h"
-#include "config.h"
-#include "debug.h"
-#include "bd_addr.h"  // class bd_addr
-
-Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1), led2(LED2);
-
-#define HEARTBEAT_PERIOD_MS 500
-
-static uint8_t   rfcomm_channel_nr = 1;
-static uint16_t  rfcomm_channel_id = 0;
-static uint8_t   rfcomm_send_credit = 0;
-static uint8_t   spp_service_buffer[128];
-
-// 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;
-    uint8_t   rfcomm_channel_nr;
-    uint16_t  mtu;
-    
-    switch (packet_type) {
-        case HCI_EVENT_PACKET:
-            switch (packet[0]) {
-                    
-                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-Demo");
-                    }
-                    break;
-                
-                case HCI_EVENT_COMMAND_COMPLETE:
-                    if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){
-                        bt_flip_addr(event_addr, &packet[6]);
-                        log_info("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr));
-                        break;
-                    }
-                    if (COMMAND_COMPLETE_EVENT(packet, hci_write_local_name)){
-                        hci_discoverable_control(1);
-                        break;
-                    }
-                    break;
-
-                case HCI_EVENT_LINK_KEY_REQUEST:
-                    // deny link key request
-                    log_info("Link key request\n\r");
-                    bt_flip_addr(event_addr, &packet[2]);
-                    hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
-                    break;
-                    
-                case HCI_EVENT_PIN_CODE_REQUEST:
-                    // inform about pin code request
-                    log_info("Pin code request - using '0000'\n\r");
-                    bt_flip_addr(event_addr, &packet[2]);
-                    hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
-                    break;
-                
-                case RFCOMM_EVENT_INCOMING_CONNECTION:
-                    // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
-                    bt_flip_addr(event_addr, &packet[2]); 
-                    rfcomm_channel_nr = packet[8];
-                    rfcomm_channel_id = READ_BT_16(packet, 9);
-                    log_info("RFCOMM channel %u requested for %s\n\r", rfcomm_channel_nr, bd_addr_to_str(event_addr));
-                    rfcomm_accept_connection_internal(rfcomm_channel_id);
-                    break;
-                    
-                case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
-                    // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
-                    if (packet[2]) {
-                        log_info("RFCOMM channel open failed, status %u\n\r", packet[2]);
-                    } else {
-                        rfcomm_channel_id = READ_BT_16(packet, 12);
-                        mtu = READ_BT_16(packet, 14);
-                        log_info("\n\rRFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n\r", rfcomm_channel_id, mtu);
-                    }
-                    break;
-                    
-                case RFCOMM_EVENT_CHANNEL_CLOSED:
-                    rfcomm_channel_id = 0;
-                    break;
-                
-                default:
-                    break;
-            }
-            break;
-            
-        case RFCOMM_DATA_PACKET:
-            // hack: truncate data (we know that the packet is at least on byte bigger
-            packet[size] = 0;
-            puts( (const char *) packet);
-            rfcomm_send_credit = 1;
-        default:
-            break;
-    }
-}
-
-static void  heartbeat_handler(struct timer *ts){
-    if (rfcomm_send_credit){
-        rfcomm_grant_credits(rfcomm_channel_id, 1);
-        rfcomm_send_credit = 0;
-    }
-    run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(ts);
-    led2 = !led2;
-} 
-
-// main
-int main(void)
-{
-    pc.baud(921600);
-    log_info("%s\n", __FILE__);
-
-    // init LEDs
-    led1 = led2 = 1;
-    
-    /// GET STARTED with BTstack ///
-    btstack_memory_init();
-    run_loop_init(RUN_LOOP_EMBEDDED);
-    
-    // init HCI
-    hci_transport_t* transport = hci_transport_usb_instance();
-    remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory;
-    hci_init(transport, NULL, NULL, remote_db);
-    
-    // init L2CAP
-    l2cap_init();
-    l2cap_register_packet_handler(packet_handler);
-    
-    // init RFCOMM
-    rfcomm_init();
-    rfcomm_register_packet_handler(packet_handler);
-    rfcomm_register_service_with_initial_credits_internal(NULL, rfcomm_channel_nr, 100, 1);  // reserved channel, mtu=100, 1 credit
-
-    // init SDP, create record for SPP and register with SDP
-    sdp_init();
-    memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
-    service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer;
-    sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, 1, "SPP Counter");
-    log_info("SDP service buffer size: %u\n\r", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record)));
-    sdp_register_service_internal(NULL, service_record_item);
-    
-    // set one-shot timer
-    timer_source_t heartbeat;
-    heartbeat.process = &heartbeat_handler;
-    run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(&heartbeat);
-    
-    
-    log_info("SPP FlowControl Demo: simulates processing on received data...\n\r");
-
-     // turn on!
-    hci_power_control(HCI_POWER_ON);
-
-    // go!
-    run_loop_execute();    
-    
-    // happy compiler!
-    return 0;
-}
-#endif