aboutsummaryrefslogtreecommitdiff
path: root/ds1307
diff options
context:
space:
mode:
Diffstat (limited to 'ds1307')
-rw-r--r--ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde173
1 files changed, 158 insertions, 15 deletions
diff --git a/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde b/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde
index 53b2abe..224706a 100644
--- a/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde
+++ b/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde
@@ -21,6 +21,7 @@ int st = 0; // alarm status (minutes from alarm - TIN)
char alarms[NALARMS][5]; // alarm settings
char cmin; // current minute
int a = -1; // current alarm
+char dbg = 0; // print debug informations
void setup () {
Serial.begin(9600);
@@ -50,10 +51,6 @@ void setup () {
}
st = 0;
a = -1;
- // DEBUG: we don't want to set the time until we receive
- // it from serial, but serial is not implemented yet
- alarms[0] = {16,0,0,13,40};
- set_time(11,9,2,5,13,39,30);
} else {
st = RTC.readData(0x08);
a = RTC.readData(0x09);
@@ -72,11 +69,20 @@ void setup () {
void loop () {
// read commands from serial
+ check_serial();
// read time, check alarms
check_time();
- //DEBUG
- Serial.println(st);
+
+ if ( dbg ) {
+ s_print_time();
+ Serial.print("st: ");
+ Serial.print(st,DEC);
+ Serial.print(", a: ");
+ Serial.print(a,DEC);
+ Serial.print(", cmin: ");
+ Serial.println(cmin,DEC);
+ }
// act on status: LEDs and buzzer
if ( st > 0 ) {
@@ -89,6 +95,149 @@ void loop () {
}
+// ****** Serial interface management *************************************** //
+
+void check_serial() {
+ int rec = Serial.read();
+ switch (rec) {
+ case 'a':
+ s_set_alarm();
+ break;
+ case 's':
+ s_set_time();
+ break;
+ case 'p':
+ s_print_alarms();
+ break;
+ case 't':
+ s_print_time();
+ break;
+ case 'd':
+ s_toggle_debug();
+ break;
+ case 'h':
+ s_print_help();
+ break;
+ }
+}
+
+void s_set_alarm() {
+ int i = s_read_dig();
+ for ( int j = 0; j < 5 ; j++) {
+ alarms[i][j] = s_read_2hex();
+ }
+ for ( int j = 0; j < 5 ; j++ ) {
+ RTC.writeData(0x0b + i*5 + j,alarms[i][j]);
+ }
+ Serial.print("Alarm ");
+ Serial.print(i,DEC);
+ Serial.println(" set.");
+}
+
+void s_set_time() {
+ RTC.setYear(s_read_2dig());
+ RTC.setMonth(s_read_2dig());
+ RTC.setDate(s_read_2dig());
+ RTC.setDayOfWeek(s_read_dig());
+ RTC.setHours(s_read_2dig());
+ RTC.setMinutes(s_read_2dig());
+ RTC.setSeconds(s_read_2dig());
+ RTC.setClock();
+ Serial.print("Time set: ");
+ s_print_time();
+}
+
+int s_read_dig() {
+ int rec;
+ rec = Serial.read();
+ while ( rec == -1 ) {
+ rec = Serial.read();
+ }
+ return rec - 48;
+}
+
+int s_read_2dig() {
+ int n;
+ n = s_read_dig() * 10;
+ n = n + s_read_dig();
+ return n;
+}
+
+int s_read_hex() {
+ int rec;
+ rec = Serial.read();
+ while ( rec == -1 ) {
+ rec = Serial.read();
+ }
+ if ( rec >= 48 && rec < 58 ) {
+ return rec - 48;
+ } else if ( rec >= 65 && rec < 71 ) {
+ return rec - 55;
+ } else if ( rec > 97 && rec < 102 ) {
+ return rec - 87;
+ } else {
+ return 0;
+ }
+}
+
+int s_read_2hex() {
+ int n;
+ n = s_read_hex() * 16;
+ n = n + s_read_hex();
+ return n;
+}
+
+void s_print_alarms() {
+ for ( int i = 0; i < NALARMS ; i++) {
+ Serial.print(i,DEC);
+ Serial.print(" - ");
+ for ( int j = 0; j < 5 ; j++) {
+ Serial.print(alarms[i][j],DEC);
+ Serial.print(" ");
+ }
+ Serial.println("");
+ }
+}
+
+void s_print_time() {
+ RTC.readClock();
+ Serial.print(RTC.getYear(),DEC);
+ Serial.print("/");
+ Serial.print(RTC.getMonth(),DEC);
+ Serial.print("/");
+ Serial.print(RTC.getDate(),DEC);
+ Serial.print(" (");
+ Serial.print(RTC.getDayOfWeek(),DEC);
+ Serial.print(") ");
+ Serial.print(RTC.getHours(),DEC);
+ Serial.print(":");
+ Serial.print(RTC.getMinutes(),DEC);
+ Serial.print(":");
+ Serial.println(RTC.getSeconds(),DEC);
+}
+
+void s_toggle_debug() {
+ if ( dbg ) {
+ dbg = 0;
+ } else {
+ dbg = 1;
+ }
+}
+
+void s_print_help() {
+ Serial.println("");
+ Serial.println(" a<s> - set an alarm");
+ Serial.println(" <s> is");
+ Serial.println(" s<s> - set the clock");
+ Serial.println(" <s> is yymmgguHHMMSS");
+ Serial.println(" p - print the alarms");
+ Serial.println(" t - print the clock");
+ Serial.println(" d - toggle printing of debug informations");
+ Serial.println(" h - print this help");
+}
+
+// ****** Time management *************************************************** //
+
// Set the current time
void set_time(int y,int m,int d, int w, int hh, int mm, int ss) {
RTC.setYear(y);
@@ -111,14 +260,6 @@ void check_time() {
int day = RTC.getDate();
int month = RTC.getMonth();
- // DEBUG
- Serial.print("Time: ");
- Serial.print(hour,DEC);
- Serial.print(":");
- Serial.print(mm,DEC);
- Serial.print(":");
- Serial.println(RTC.getSeconds());
-
if ( a < 0 ) {
for ( int i = 0; i < NALARMS ; i ++ ) {
// check alarm i
@@ -150,6 +291,8 @@ void check_time() {
}
+// ****** LED management **************************************************** //
+
void set_leds() {
if ( st > 0 && st <= TIN) {
int y = int(float(st*255)/TIN);
@@ -160,7 +303,7 @@ void set_leds() {
analogWrite(RPIN,0);
analogWrite(YPIN,255);
analogWrite(BPIN,0);
- }else if (st >= TDAY && st < TOUT) {
+ } else if (st >= TDAY && st < TOUT) {
analogWrite(RPIN,0);
analogWrite(YPIN,0);
analogWrite(BPIN,255);