Feng Hong / Mbed OS Nucleo_rtos_basic
Revision:
12:5cb359f981f3
Parent:
11:8702316d7fc8
Child:
13:9f581a090e53
diff -r 8702316d7fc8 -r 5cb359f981f3 main.cpp
--- a/main.cpp	Sat Apr 13 05:55:40 2019 +0000
+++ b/main.cpp	Sat Apr 20 09:31:30 2019 +0000
@@ -42,6 +42,11 @@
 DigitalOut led2(LED2);  // only one LED PA_5
 #endif
 
+DigitalOut output1(PC_3);
+DigitalOut output2(PC_2);
+DigitalOut output3(PB_7);
+DigitalIn input1(PC_6);
+DigitalIn input2(PC_8);
 
 EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
 extern void eeprom_test(void);
@@ -64,14 +69,64 @@
 MemoryPool<CANMessage, 16> can_mpool;
 Queue<CANMessage, 16> can_queue;
 
-InterruptIn button1(USER_BUTTON);
+InterruptIn button0(USER_BUTTON);
+volatile bool button0_pressed = false; // Used in the main loop
+volatile bool button0_enabled = true; // Used for debouncing
+Timeout button0_timeout; // Used for debouncing
+InterruptIn button1(PB_4);
 volatile bool button1_pressed = false; // Used in the main loop
 volatile bool button1_enabled = true; // Used for debouncing
 Timeout button1_timeout; // Used for debouncing
+InterruptIn button2(PC_1);
+volatile bool button2_pressed = false; // Used in the main loop
+volatile bool button2_enabled = true; // Used for debouncing
+Timeout button2_timeout; // Used for debouncing
+InterruptIn button3(PC_7);
+volatile bool button3_pressed = false; // Used in the main loop
+volatile bool button3_enabled = true; // Used for debouncing
+Timeout button3_timeout; // Used for debouncing
+InterruptIn button4(PA_9);
+volatile bool button4_pressed = false; // Used in the main loop
+volatile bool button4_enabled = true; // Used for debouncing
+Timeout button4_timeout; // Used for debouncing
+
 
 // Enables button when bouncing is over
+//button0
+void button0_enabled_cb(void)
+{
+    int button_status;
+    button_status = button0.read();
+    if (button_status == 0)
+    {    
+        printf("button0 down\r\n");
+    }    
+    else
+        printf("button0 released\r\n");
+    button0_enabled = true;
+}
+
+// ISR handling button pressed event
+void button0_onpressed_cb(void)
+{
+    if (button0_enabled) { // Disabled while the button is bouncing
+        button0_enabled = false;
+        button0_pressed = true; // To be read by the main loop
+        button0_timeout.attach(callback(button0_enabled_cb), 0.3); // Debounce time 300 ms
+    }
+}
+//button0--
+//button1
 void button1_enabled_cb(void)
 {
+    int button_status;
+    button_status = button1.read();
+    if (button_status == 0)
+    {    
+        printf("button1 down\r\n");
+    }    
+    else
+        printf("button1 released\r\n");
     button1_enabled = true;
 }
 
@@ -84,7 +139,79 @@
         button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
     }
 }
+//button1--
+//button2
+void button2_enabled_cb(void)
+{
+    int button_status;
+    button_status = button2.read();
+    if (button_status == 0)
+    {    
+        printf("button2 down\r\n");
+    }    
+    else
+        printf("button2 released\r\n");
+    button2_enabled = true;
+}
 
+// ISR handling button pressed event
+void button2_onpressed_cb(void)
+{
+    if (button2_enabled) { // Disabled while the button is bouncing
+        button2_enabled = false;
+        button2_pressed = true; // To be read by the main loop
+        button2_timeout.attach(callback(button2_enabled_cb), 0.3); // Debounce time 300 ms
+    }
+}
+//button2--
+//button3
+void button3_enabled_cb(void)
+{
+    int button_status;
+    button_status = button3.read();
+    if (button_status == 0)
+    {    
+        printf("button3 down\r\n");
+    }    
+    else
+        printf("button3 released\r\n");
+    button3_enabled = true;
+}
+
+// ISR handling button pressed event
+void button3_onpressed_cb(void)
+{
+    if (button3_enabled) { // Disabled while the button is bouncing
+        button3_enabled = false;
+        button3_pressed = true; // To be read by the main loop
+        button3_timeout.attach(callback(button3_enabled_cb), 0.3); // Debounce time 300 ms
+    }
+}
+//button3--
+//button4
+void button4_enabled_cb(void)
+{
+    int button_status;
+    button_status = button4.read();
+    if (button_status == 0)
+    {    
+        printf("button4 down\r\n");
+    }    
+    else
+        printf("button4 released\r\n");
+    button4_enabled = true;
+}
+
+// ISR handling button pressed event
+void button4_onpressed_cb(void)
+{
+    if (button4_enabled) { // Disabled while the button is bouncing
+        button4_enabled = false;
+        button4_pressed = true; // To be read by the main loop
+        button4_timeout.attach(callback(button4_enabled_cb), 0.3); // Debounce time 300 ms
+    }
+}
+//button4--
 void can_rxthread()
 {
     int loop;
@@ -144,7 +271,8 @@
     lcd.printf(0, 0, "pressed!" );      
 #endif   
 
-    
+    input1.mode(PullUp);
+    input2.mode(PullUp);
     can_receivethread.start(can_rxthread);  
     can_handlethread.start(analyzePayload);   
 //    flashIAP.init();
@@ -153,9 +281,18 @@
 //    can2.reset();
     can1.frequency(100000);
 //    can2.frequency(100000);
-    //button1.mode(PullUp); // Activate pull-up
+    //button0.mode(PullUp); // Activate pull-up
 
+    button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
+    button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
+    button1.rise(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
+    button2.fall(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
+    button2.rise(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
+    button3.fall(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
+    button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
+    button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event
+    button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event    
 //    eeprom_test();
 
 #if 1  
@@ -166,7 +303,7 @@
     int idx = 0; // Just for printf below
     can_tx_data[0] = 0;
     while(1) {
-        if (button1_pressed) { // Set when button is pressed
+        if (button0_pressed) { // Set when button is pressed
 #if 0        
             printf("scale value %f. \r\n", hx711.getGram());
 #endif
@@ -177,7 +314,7 @@
             can_tx_data[5] = can_tx_data[4]+1;
             can_tx_data[6] = can_tx_data[5]+1;
             can_tx_data[7] = can_tx_data[6]+1; 
-            button1_pressed = false;
+            button0_pressed = false;
             printf("Button pressed %d\r\n", idx++);
             printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]);
 #ifdef LCD_1602