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.
Fork of 08-timer_alarm_state by
Diff: main.cpp
- Revision:
- 1:eddde0361c0a
- Parent:
- 0:425c87a33e63
- Child:
- 2:48c29b52144d
--- a/main.cpp Mon Jan 15 18:05:34 2018 +0000
+++ b/main.cpp Mon Feb 05 18:22:34 2018 +0000
@@ -1,86 +1,347 @@
-
#include "mbed.h"
#include "BtnEventM0.h"
Serial pc(USBTX, USBRX);
// LSB MSB
-BusOut lb(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
+BusOut lb(/*P1_13,P1_12,P1_7,*/P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
BtnEventM0 sw4(P1_16), sw3(P0_23), sw1(P0_10), sw2(P0_15);
+// Zustandsangebe
+BusOut stLED(P1_13,P1_12,P1_7);
+
+
+const int st_run = 1;
+const int st_editH = 2;
+const int st_editM = 3;
+const int st_editalarmH = 4;
+const int st_editalarmM = 5;
+
int blinkIdx = 0;
int hh=0, mm=0;
+int alarmhh=0, alarmmm=0;
+bool alarm = false;
-void ShowMode();
-void ShowTime();
+
+class Timmer
+{
+public:
+ void Init();
+ void Run();
+ void EditH();
+ void EditM();
+ void EditalarmH();
+ void EditalarmM();
+public:
+ int state;
+private:
+ Timer t1;
+};
+
+Timmer tim1;
int main()
{
- pc.baud(500000);
- sw4.Init(); sw3.Init(); sw1.Init(); sw2.Init();
- ShowMode();
- ShowTime();
-
- Timer t1; t1.start();
- while(1)
- {
- if( sw4.CheckFlag() ) {
- if( blinkIdx==-1 )
- blinkIdx=0;
- else if( blinkIdx==0 )
- blinkIdx=3;
- else if( blinkIdx==3 )
- blinkIdx=-1;
- ShowMode();
+ pc.baud(500000);
+ sw4.Init(); // Switch state
+ sw3.Init(); // Count UP
+ sw1.Init(); // Count DOWN
+ sw2.Init(); // Set Alarm
+ tim1.Init();
+
+ while(1) {
+ if(tim1.state == st_run) {
+ tim1.Run();
+ blinkIdx = 0;
+ }
+ if(tim1.state == st_editH) {
+ tim1.EditH();
+ blinkIdx = 3;
+ }
+ if(tim1.state == st_editM) {
+ tim1.EditM();
+ blinkIdx = -1;
+ }
+ if(tim1.state == st_editalarmH) {
+ tim1.EditalarmH();
+ blinkIdx = 3;
+ }
+ if(tim1.state == st_editalarmM) {
+ tim1.EditalarmM();
+ blinkIdx = -1;
+ }
}
- if( t1.read_ms()>150 ) {
- t1.reset();
- if( blinkIdx==-1 ) {
- mm++;
- if( mm>29 )
- { mm=0; hh++; }
- ShowTime();
+}
+
+void Timmer::Init()
+{
+ state= st_run;
+ t1.start();
+}
+
+void Timmer::Run()
+{
+ // 1..in die erste Zeile schreiben
+ pc.printf("1 Clock running\n");
+ pc.printf("3 %d\n", blinkIdx);
+ stLED = 1;
+
+ while(1) {
+ if(sw4.CheckFlag()) {
+ state = st_editH;
+ return;
+ }
+ if(sw2.CheckFlag()) {
+ state = st_editalarmH;
+ alarm = true;
+ return;
+ }
+
+ if(t1.read_ms() > 50) { // Ausgabe mit 20Hz,aber dafür hate 1h 60min!!!
+ t1.reset();
+ mm++;
+ if(mm > 59) {
+ mm = 0;
+ hh++;
}
+ if(hh > 23) {
+ hh = 0;
+ }
+
+ // Alram
+ if(alarm == true) {
+ if(hh == alarmhh && mm == alarmmm) {
+ pc.printf("1 !!!ALARM!!!\n");
+ alarmhh=0;
+ alarmmm=0;
+ alarm = false;
+ }
+ }
+ pc.printf("2 %02d:%02d\n",hh,mm);
+ }
}
- if( blinkIdx==0 ) {
- if( sw3.CheckFlag() ) {
- hh++;
- if( hh>20 ) hh=0;
- ShowTime();
- }
+}
+
+/*********** SET TIME **************/
+
+void Timmer::EditH()
+{
+ pc.printf("1 Edit hh\n");
+ pc.printf("2 %02d:%02d\n", hh, mm);
+ pc.printf("3 %d\n", blinkIdx);
+ stLED = 2;
+
+ while(1) {
+ if(sw4.CheckFlag()) {
+ state = st_editM;
+ return;
+ }
+
+ // Count UP
+ if(sw3.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ hh++;
+ if(hh > 23)
+ hh = 0;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+
+ wait_ms(300);
+ if(sw3.CheckButton())
+ while(sw3.CheckButton()) {
+ // ContinousPress erkannt
+ hh++;
+ if(hh > 23)
+ hh = 0;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+ wait_ms(100);
+ }
+ }
+
+ // Count DOWN
+ if(sw1.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ hh--;
+ if(hh < 0)
+ hh = 23;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+
+ wait_ms(300);
+ if(sw1.CheckButton())
+ while(sw1.CheckButton()) {
+ // ContinousPress erkannt
+ hh--;
+ if(hh < 0)
+ hh = 23;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+ wait_ms(100);
+ }
+ }
}
- if( blinkIdx==3 ) {
- if( sw3.CheckFlag() ) {
- mm++;
- if( mm>30 ) mm=0;
- ShowTime();
- }
- }
- }
}
-void ShowMode()
+void Timmer::EditM()
{
- if( blinkIdx==-1 )
- // 1..in die erste Zeile schreiben
- pc.printf("1 Clock running\n");
- if( blinkIdx==0 )
- pc.printf("1 Edit hh\n");
- if( blinkIdx==3 )
pc.printf("1 Edit mm\n");
- // 3..BlinkIndex setzen
- // es blinken immer 2 Zeichen ( Spalten ) beginnend mit blinkIdx
- pc.printf("3 %d\n", blinkIdx);
+ pc.printf("2 %02d:%02d\n", hh, mm);
+ pc.printf("3 %d\n", blinkIdx);
+ stLED = 3;
+
+ while(1) {
+ if(sw4.CheckFlag()) {
+ state = st_run;
+ return;
+ }
+
+ // Count UP
+ if(sw3.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ mm++;
+ if(mm > 59 )
+ mm = 0;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+
+ wait_ms(300);
+ if(sw3.CheckButton())
+ while(sw3.CheckButton()) {
+ // ContinousPress erkannt
+ mm++;
+ if(mm > 59)
+ mm = 0;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+ wait_ms(100);
+ }
+ }
+
+ // Count DOWN
+ if(sw1.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ mm--;
+ if(mm < 0)
+ mm = 59;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+
+ wait_ms(300);
+ if(sw1.CheckButton())
+ while(sw1.CheckButton()) {
+ // ContinousPress erkannt
+ mm--;
+ if(mm < 0)
+ mm = 59;
+ pc.printf("2 %02d:%02d\n", hh, mm);
+ wait_ms(100);
+ }
+ }
+ }
}
-void ShowTime()
+/*********** SET ALARM **************/
+
+void Timmer::EditalarmH()
{
- // 2..in die 2te Zeile schreiben
- pc.printf("2 %02d:%02d\n",hh,mm);
+ pc.printf("1 Edit alarm hh\n");
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ pc.printf("3 %d\n", blinkIdx);
+ stLED = 4;
+
+ while(1) {
+ if(sw2.CheckFlag()) {
+ state = st_editalarmM;
+ return;
+ }
+
+ // Count UP
+ if(sw3.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ alarmhh++;
+ if(alarmhh > 23)
+ alarmhh = 0;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+
+ wait_ms(300);
+ if(sw3.CheckButton())
+ while(sw3.CheckButton()) {
+ // ContinousPress erkannt
+ alarmhh++;
+ if(alarmhh > 23)
+ alarmhh = 0;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ wait_ms(100);
+ }
+ }
+
+ // Count DOWN
+ if(sw1.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ alarmhh--;
+ if(alarmhh < 0)
+ alarmhh = 23;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+
+ wait_ms(300);
+ if(sw1.CheckButton())
+ while(sw1.CheckButton()) {
+ // ContinousPress erkannt
+ alarmhh--;
+ if(alarmhh < 0)
+ alarmhh = 23;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ wait_ms(100);
+ }
+ }
+ }
}
+void Timmer::EditalarmM()
+{
+ pc.printf("1 Edit alarm mm\n");
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ pc.printf("3 %d\n", blinkIdx);
+ stLED = 5;
+ while(1) {
+ if(sw2.CheckFlag()) {
+ state = st_run;
+ return;
+ }
+ // Count UP
+ if(sw3.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ alarmmm++;
+ if(alarmmm > 59 )
+ alarmmm = 0;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ wait_ms(300);
+ if(sw3.CheckButton())
+ while(sw3.CheckButton()) {
+ // ContinousPress erkannt
+ alarmmm++;
+ if(alarmmm > 59)
+ alarmmm = 0;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ wait_ms(100);
+ }
+ }
+ // Count DOWN
+ if(sw1.CheckFlag()) {
+ // Aktion für EinfachClick z.B. cnt++ ausführen
+ alarmmm--;
+ if(alarmmm < 0)
+ alarmmm = 59;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ wait_ms(300);
+ if(sw1.CheckButton())
+ while(sw1.CheckButton()) {
+ // ContinousPress erkannt
+ alarmmm--;
+ if(alarmmm < 0)
+ alarmmm = 59;
+ pc.printf("2 %02d:%02d\n", alarmhh, alarmmm);
+ wait_ms(100);
+ }
+ }
+ }
+}
\ No newline at end of file
