Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:c5769cd78933
- Parent:
- 0:05d73097e319
- Child:
- 2:d4fab8f3f21f
--- a/main.cpp Mon Sep 29 12:05:03 2014 +0000
+++ b/main.cpp Wed Oct 22 06:52:32 2014 +0000
@@ -1,4 +1,5 @@
#include "mbed.h"
+#include "DebouncedIn.h"
Ticker tick;
DigitalOut led1(LED1);
@@ -6,7 +7,7 @@
DigitalOut led3(LED3);
DigitalOut led4(LED4);
-InterruptIn button(p12);
+DebouncedIn button(p12);
// Zum Schalten: 1 Taster PINA PA0
// 1. Funktion
@@ -22,52 +23,28 @@
//volatile uint8_t count;
volatile unsigned int count;
-volatile unsigned int newEvent =0;
-volatile unsigned int event=0, state=0;
+volatile unsigned int event=0;
+volatile int led=0;
-/* Prototypen der Funktionen */
-int no_fu();
int ledsEin();
int msec300TimerOn();
-int ledsAusTimerOff();
int ledsTogglen();
-
-unsigned char nextstate_tab[2][3]=
-//present state 0 1 2
-//------------------------
-/* event 0 */{{ 1, 2, 0 }, // next
-/* event 1 */ { 0, 1, 2 }}; // state
-// state=nextstate_tab[event][state]; // gehe auf naechsten Zustand
+int startTimer();
-/*************************************************************/
-/* Tabelle fuer Aktionsroutinen fuer die Zustaende bei Input */
-/*************************************************************/
-// p r e s e n t s t a t e
-int(*action[2][3])()= // 0 1 2
-/* event 0 */ {{ ledsEin, msec300TimerOn, ledsAusTimerOff },
-/* event 1 */ { no_fu, no_fu, ledsTogglen }};
-// (*action[event][state])(); // suche in Tabelle nach Funktion
-
-
-/* Aktionsroutinen, functions */
-int no_fu() // keine function - tue nichts
-{
- return(0);
-}
int msec300TimerOn()
{
- __enable_irq();
+ startTimer();
return(1);
}
-int ledsAusTimerOff()
+int stopTimer()
{
led1 = 0;
led2 = 0;
led3 = 0;
led4 = 0;
- __disable_irq();
+ tick.detach();
return(2);
}
@@ -89,16 +66,51 @@
return(4);
}
+void switchled()
+{
+ switch(led)
+ {
+ case 0:
+ led1 = 1;
+ break;
+ case 1:
+ led1 = 0;
+ led2 = 1;
+ break;
+ case 2:
+ led2 = 0;
+ led3 = 1;
+ break;
+ case 3:
+ led3 = 0;
+ led4 = 1;
+ break;
+ case 4:
+ led4 = 0;
+ led = -1;
+ break;
+ }
+
+ led++;
+}
+
+int ledsSwitch()
+{
+ tick.attach(&switchled, 0.2);
+ return(0);
+}
+
// interrupt SR
-void timertick()
+void blink()
{
ledsTogglen();
}
// functions
-void initTimer()
+int startTimer()
{
- tick.attach(&timertick, 0.2);
+ tick.attach(&blink, 0.2);
+ return(0);
}
void debounce(PinName name, unsigned char samples)
@@ -117,37 +129,44 @@
}
}
-void buttonpress()
+void doEvent()
{
- debounce(p12, 8);
-
- event =0;
- newEvent =1;
-
- if (newEvent)
- {
- __disable_irq();
- newEvent =0;
- (*action[event][state])(); // suche in Tabelle nach Funktion
- state=nextstate_tab[event][state]; // gehe auf naechsten Zustand
- __enable_irq();
- }
+ switch(event)
+ {
+ case 0:
+ led1 = 0;
+ led2 = 0;
+ led3 = 0;
+ led4 = 0;
+ break;
+ case 1:
+ led = 0;
+ ledsSwitch();
+ break;
+ case 2:
+ stopTimer();
+ ledsTogglen();
+ break;
+ case 3:
+ msec300TimerOn();
+ break;
+ case 4:
+ stopTimer();
+ event = 0;
+ break;
+ default:
+ break;
+ }
}
int main()
-{
-
- button.rise(&buttonpress);
- led1 = 0;
- led2 = 0;
- led3 = 0;
- led4 = 0;
-
- initTimer();
-
- newEvent = 0;
- state=0;
- event=0;
+{
+ while(1) {
+ if (button.rising()) {
+ event++;
+ doEvent();
+ }
+ }
}