summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2015-12-26 10:30:55 +0100
committerFlorian Baumann <derflob@derflob.de>2015-12-26 10:30:55 +0100
commitef4544026a81836b8f8a29eeea636585d3603d3c (patch)
tree6afa7cd213406f70eba6959f3e387ec4b90ed396
parent20c2c49095c65377eed31fcca9b7153c78b8b8cd (diff)
parentb49cfe8195e76408ef7170c0ccdf2f8d81ee7f45 (diff)
downloadmFeTAp-ef4544026a81836b8f8a29eeea636585d3603d3c.tar.gz
mFeTAp-ef4544026a81836b8f8a29eeea636585d3603d3c.tar.bz2
Merge remote-tracking branch 'origin/master'
-rw-r--r--mFeTAp.ino217
-rw-r--r--morse_code.h117
2 files changed, 212 insertions, 122 deletions
diff --git a/mFeTAp.ino b/mFeTAp.ino
index 1f00167..1415b1a 100644
--- a/mFeTAp.ino
+++ b/mFeTAp.ino
@@ -39,7 +39,9 @@ Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
#define RINGING_PERIOD 20
-#define SMS_SIGNAL 150
+#define SMS_SIGNAL 200
+#define SMS_UNIT_LEN 150
+#define SMS_READ_DELAY 1500
volatile int digit = 0;
volatile int digit_dialed = 0;
@@ -92,13 +94,7 @@ void setup() {
fonaSerial->begin(9600);
fona.begin(*fonaSerial);
- fonaSerial->println("AT+STTONE=1,1,1000");
-
enableGSMBusy(false);
-
- //sms_available = millis();
-
-
}
void loop() {
@@ -169,10 +165,16 @@ void process_input_hangup() {
switch (adv_state) {
case IDLE:
if (digitalRead(PIN_HANGUP) == LOW) {
- Serial.println("WAIT_FOR_NUMBER: Picked up");
- adv_state = WAIT_FOR_NUMBER;
- enableGSMBusy(true);
- delete_number();
+ if (sms_available > 0) {
+ Serial.println("READ_SMS: Picked up");
+ adv_state = READING_SMS;
+ enableGSMBusy(true);
+ } else {
+ Serial.println("WAIT_FOR_NUMBER: Picked up");
+ adv_state = WAIT_FOR_NUMBER;
+ enableGSMBusy(true);
+ delete_number();
+ }
}
break;
@@ -193,6 +195,14 @@ void process_input_hangup() {
}
break;
+ case READING_SMS:
+ if (digitalRead(PIN_HANGUP) == HIGH) {
+ sms_available = 0;
+ Serial.println("IDLE: SMS reading abort");
+ adv_state = IDLE;
+ }
+ break;
+
case WAIT_FOR_NUMBER:
case DIALING:
case BUSY:
@@ -263,6 +273,26 @@ void process_input_number() {
}
}
+void ring_bell(uint8_t enable) {
+
+ if (enable) {
+ // LED
+ digitalWrite(PIN_LED, HIGH);
+
+ // bell
+ uint8_t p = (millis() / RINGING_PERIOD) % 2;
+ digitalWrite(PIN_A_BELL, !p);
+ digitalWrite(PIN_B_BELL, !!p);
+ } else {
+ // LED
+ digitalWrite(PIN_LED, LOW);
+
+ // bell
+ digitalWrite(PIN_A_BELL, LOW);
+ digitalWrite(PIN_B_BELL, LOW);
+ }
+}
+
void process_output_bell() {
static unsigned long inc_time = 0;
@@ -272,73 +302,38 @@ void process_output_bell() {
inc_time = millis();
}
- int p = ((millis() - inc_time) / 1000) % 5;
- if (p > 0){
- digitalWrite(PIN_LED, LOW);
-
- digitalWrite(PIN_A_BELL, LOW);
- digitalWrite(PIN_B_BELL, LOW);
- } else {
- digitalWrite(PIN_LED, HIGH);
+ uint8_t p = ((millis() - inc_time) / 1000) % 5;
+ ring_bell(!p);
- p = (millis() / RINGING_PERIOD) % 2;
- digitalWrite(PIN_A_BELL, !p);
- digitalWrite(PIN_B_BELL, !!p);
- }
- } else {
+ } else if (adv_state == IDLE) {
if (sms_available > 0) {
- static char *sms = "sos";
+ static char *sms = "sms";
uint16_t sms_units = units_in_str(sms, 3);
- uint16_t cunit = ((millis() - sms_available) / 150) % sms_units;
-
- //Serial.print("units: ");
- //Serial.print(sms_units);
- //Serial.print("; ");
- Serial.print("cunit: ");
- Serial.print(cunit);
- Serial.print("; ");
+ uint16_t cunit = ((millis() - sms_available) / SMS_UNIT_LEN) % sms_units;
for (char *c = sms; *c != '\0'; c++) {
- if (cunit >= units_in_char(c) + 3) {
- cunit -= (units_in_char(c) + 3);
- continue;
- } else {
- Serial.print(*c);
- Serial.print("; ");
- Serial.print(cunit);
- if (cunit < units_in_char(c)) {
+ uint16_t uic = units_in_char(c);
+ if (cunit < uic + MORSE_INTER_CHAR_UNITS) {
+ if (cunit < uic) {
uint16_t on = morse_unit(c, cunit);
- Serial.print("; on: ");
- Serial.print(on);
- if (on != 0) {
- digitalWrite(PIN_LED, HIGH);
-
- unsigned int p = (millis() / RINGING_PERIOD) % 2;
- digitalWrite(PIN_A_BELL, !p);
- digitalWrite(PIN_B_BELL, !!p);
- } else {
- digitalWrite(PIN_LED, LOW);
-
- digitalWrite(PIN_A_BELL, LOW);
- digitalWrite(PIN_B_BELL, LOW);
- }
+ ring_bell(on);
} else {
- digitalWrite(PIN_LED, LOW);
-
- digitalWrite(PIN_A_BELL, LOW);
- digitalWrite(PIN_B_BELL, LOW);
+ ring_bell(0);
}
break;
+ } else {
+ cunit -= (uic + MORSE_INTER_CHAR_UNITS);
+ continue;
}
}
- Serial.println();
- } else {
- digitalWrite(PIN_LED, LOW);
- digitalWrite(PIN_A_BELL, LOW);
- digitalWrite(PIN_B_BELL, LOW);
+ } else {
+ ring_bell(0);
}
+
+ } else {
+ ring_bell(0);
inc_time = 0;
}
}
@@ -366,6 +361,10 @@ void process_output_speaker() {
}
break;
+ case READING_SMS:
+ read_sms();
+ break;
+
default:
play_dial_tone(false);
break;
@@ -378,6 +377,95 @@ void play_DTMF_number(char *numberDTMF) {
}
}
+void read_sms() {
+
+ static char sms[128] = { '\0' };
+ static uint8_t sms_len = 0;
+ static uint16_t sms_units = 0;
+ static unsigned long sms_start = 0;
+
+ if (!sms_start && !sms_available) {
+ sms_start = 0;
+ return;
+ } else if (sms_available && !sms_start) {
+ if (fona.getNumSMS() > 0) {
+ sms_start = millis();
+ sms_len = 128;
+ get_sms(sms, &sms_len);
+ if (sms_len > 0) {
+ sms_units = units_in_str(sms, sms_len);
+ Serial.print("Reading SMS: ");
+ Serial.println(sms);
+ } else {
+ sms_available = 0;
+ Serial.println("IDLE: No SMS available");
+ adv_state = IDLE;
+ return;
+ }
+ } else {
+ sms_available = 0;
+ Serial.println("IDLE: No SMS available");
+ adv_state = IDLE;
+ return;
+ }
+ }
+
+ if ((millis() - sms_start) < SMS_READ_DELAY) {
+ return;
+ }
+
+ // current morse unit
+ uint16_t cunit = ((millis() - (sms_start + SMS_READ_DELAY)) / SMS_UNIT_LEN);
+ if (!(cunit < sms_units)) {
+ Serial.println("READING_SMS: Reading next");
+ sms_start = 0;
+ return;
+ }
+
+ for (uint8_t i = 0; i < sms_len; i++) {
+ uint16_t uic = units_in_char(sms + i);
+ // Search character corresponding tu current unit
+ if (cunit < uic + MORSE_INTER_CHAR_UNITS) {
+ // In character or post-character delay
+ if (cunit < uic) {
+ uint16_t on = morse_unit(sms+i, cunit);
+ play_dial_tone(!!on);
+ } else {
+ play_dial_tone(0);
+ }
+ break;
+ } else {
+ // current unit is further than this character
+ cunit -= (uic + MORSE_INTER_CHAR_UNITS);
+ continue;
+ }
+ }
+}
+
+void get_sms(char *sms, uint8_t *len) {
+
+ uint8_t slot = 1;
+ uint16_t l = 0;
+
+ memset(sms, '\0', *len * sizeof(char));
+
+ // Search first slot, that has a SMS available
+ while (slot <= 20 && !fona.readSMS(slot, sms, (*len-1), &l)) {
+ slot++;
+ }
+
+
+ if (slot <= 20) {
+ fona.deleteSMS(slot);
+ // actual length of read SMS should fit in an uint8_t
+ *len = l;
+ } else {
+ memset(sms, '\0', *len * sizeof(char));
+ *len = 0;
+ }
+
+}
+
boolean play_dial_tone(boolean enable) {
static boolean enabled = false;
@@ -395,7 +483,6 @@ boolean play_dial_tone(boolean enable) {
void delete_number() {
- Serial.println("Nummer loeschen");
memset(number, '\0', NUMBER_LEN * sizeof(char));
digit = 0;
diff --git a/morse_code.h b/morse_code.h
index 6018e38..ffb7b65 100644
--- a/morse_code.h
+++ b/morse_code.h
@@ -1,60 +1,45 @@
-uint16_t morse_code[26] = {
-(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 8),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6),
-(1 << 0),
-(1 << 0 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8),
-(1 << 0 | 1 << 2 | 1 << 4 | 1 << 6),
-(1 << 0 | 1 << 2),
-(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10 | 1 << 11 | 1 << 12),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8),
-(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 8),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4),
-//o
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8 | 1 << 9 | 1 << 10),
-(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8 | 1 << 10 | 1 << 11 | 1 << 12),
-(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6),
-(1 << 0 | 1 << 2 | 1 << 4),
-(1 << 0 | 1 << 1 | 1 << 2),
-(1 << 0 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6),
-(1 << 0 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8),
-(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 8 | 1 << 9 | 1 << 10),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10 | 1 << 11 | 1 << 12),
-(1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8 | 1 << 10),
+#ifndef __MORSE_CODE_H__
+#define __MORSE_CODE_H__
+
+#define MORSE_INTER_CHAR_UNITS 3
+#define MORSE_INTRA_CHAR_UNITS 1
+#define MORSE_DASH_UNITS 3
+#define MORSE_DOT_UNITS 1
+#define MORSE_INTER_WORD_UNITS 7
+
+uint16_t morse_code[27] = {
+ (1 << 0 | 1 << 2 | 1 << 3 | 1 << 4),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 8),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6),
+ (1 << 0),
+ (1 << 0 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8),
+ (1 << 0 | 1 << 2 | 1 << 4 | 1 << 6),
+ (1 << 0 | 1 << 2),
+ (1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10 | 1 << 11 | 1 << 12),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8),
+ (1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 8),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4),
+ //o
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8 | 1 << 9 | 1 << 10),
+ (1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8 | 1 << 10 | 1 << 11 | 1 << 12),
+ (1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6),
+ (1 << 0 | 1 << 2 | 1 << 4),
+ (1 << 0 | 1 << 1 | 1 << 2),
+ (1 << 0 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6),
+ (1 << 0 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8),
+ (1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 8 | 1 << 9 | 1 << 10),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 10 | 1 << 11 | 1 << 12),
+ (1 << 0 | 1 << 1 | 1 << 2 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 8 | 1 << 10),
+ 0,
};
-uint8_t morse_code_len[26] = {
-5,
-9,
-11,
-7,
-1,
-9,
-9,
-7,
-3,
-13,
-9,
-9,
-7,
-5,
-11,
-11,
-13,
-7,
-5,
-3,
-7,
-9,
-9,
-11,
-13,
-11};
+uint8_t morse_code_len[27] = { 5, 9, 11, 7, 1, 9, 9, 7, 3, 13, 9, 9, 7, 5, 11, 11, 13, 7, 5, 3, 7, 9, 9, 11, 13, 11, 4 };
+
uint16_t units_in_str(char *s, uint8_t l);
uint16_t units_in_char(char *s);
uint8_t morse_unit(char *s, uint16_t u);
@@ -71,9 +56,27 @@ uint16_t units_in_str(char *s, uint8_t l) {
}
uint16_t units_in_char(char *s) {
- return morse_code_len[*s - 97];
+ if (*s > 64 && *s < 91) {
+ return morse_code_len[*s - 65];
+ } else if (*s > 96 && *s < 122) {
+ return morse_code_len[*s - 97];
+ } else if (*s == ' ') {
+ return morse_code_len[26];
+ } else {
+ return 0;
+ }
}
uint8_t morse_unit(char *s, uint16_t u) {
- return morse_code[*s - 97] & (1 << u);
+ if (*s > 64 && *s < 91) {
+ return morse_code[*s - 65] & (1 << u);
+ } else if (*s > 96 && *s < 122) {
+ return morse_code[*s - 97] & (1 << u);
+ } else if (*s == ' ') {
+ return 0;
+ } else {
+ return 0;
+ }
}
+
+#endif