summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2015-12-20 20:09:21 +0100
committerFlorian Baumann <derflob@derflob.de>2015-12-21 00:39:27 +0100
commitf813305068a3be01d4f16fa65815e5ee71fcd16d (patch)
treee4e634b75dd41aac6729868128a362d7ed15ee95
parent6607b1f84bd9a5b75dfbdb1b1b2e1eabf7339739 (diff)
downloadmFeTAp-f813305068a3be01d4f16fa65815e5ee71fcd16d.tar.gz
mFeTAp-f813305068a3be01d4f16fa65815e5ee71fcd16d.tar.bz2
SMS reading support
-rw-r--r--mFeTAp.ino190
1 files changed, 167 insertions, 23 deletions
diff --git a/mFeTAp.ino b/mFeTAp.ino
index 2542bf2..4b8371b 100644
--- a/mFeTAp.ino
+++ b/mFeTAp.ino
@@ -3,6 +3,8 @@
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
+#include "morse_code.h"
+
#define FONA_RX A4
#define FONA_TX A5
#define FONA_RST A3
@@ -37,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;
@@ -49,6 +53,8 @@ volatile int number_dialed = 0;
char number[NUMBER_LEN] = { '\0' };
int number_len = 0;
+unsigned long sms_available = 0;
+
enum adv_states {
IDLE,
WAIT_FOR_NUMBER,
@@ -88,10 +94,7 @@ void setup() {
fonaSerial->begin(9600);
fona.begin(*fonaSerial);
- fonaSerial->println("AT+STTONE=1,1,1000");
-
enableGSMBusy(false);
-
}
void loop() {
@@ -134,6 +137,7 @@ void process_input_RI() {
} else {
if (pin_down > 0 && millis() - pin_down < SMS_SIGNAL) {
Serial.println("SMS incoming");
+ sms_available = millis();
// handle SMS
pin_down = 0;
}
@@ -159,10 +163,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;
@@ -183,6 +193,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:
@@ -253,6 +271,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;
@@ -262,25 +300,39 @@ void process_output_bell() {
inc_time = millis();
}
- int p = ((millis() - inc_time) / 1000) % 5;
- if (p > 0){
- digitalWrite(PIN_LED, LOW);
+ uint8_t p = ((millis() - inc_time) / 1000) % 5;
+ ring_bell(!p);
- digitalWrite(PIN_A_BELL, LOW);
- digitalWrite(PIN_B_BELL, LOW);
- } else {
- digitalWrite(PIN_LED, HIGH);
+ } else if (adv_state == IDLE) {
+ if (sms_available > 0) {
+ static char *sms = "sms";
+ uint16_t sms_units = units_in_str(sms, 3);
+
+ uint16_t cunit = ((millis() - sms_available) / SMS_UNIT_LEN) % sms_units;
- p = (millis() / RINGING_PERIOD) % 2;
- digitalWrite(PIN_A_BELL, !p);
- digitalWrite(PIN_B_BELL, !!p);
+ for (char *c = sms; *c != '\0'; 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);
+ ring_bell(on);
+ } else {
+ ring_bell(0);
+ }
+ break;
+ } else {
+ cunit -= (uic + MORSE_INTER_CHAR_UNITS);
+ continue;
+ }
+ }
+
+ } else {
+ ring_bell(0);
}
+
} else {
+ ring_bell(0);
inc_time = 0;
- digitalWrite(PIN_LED, LOW);
-
- digitalWrite(PIN_A_BELL, LOW);
- digitalWrite(PIN_B_BELL, LOW);
}
}
@@ -307,6 +359,10 @@ void process_output_speaker() {
}
break;
+ case READING_SMS:
+ read_sms();
+ break;
+
default:
play_dial_tone(false);
break;
@@ -319,6 +375,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;
@@ -336,7 +481,6 @@ boolean play_dial_tone(boolean enable) {
void delete_number() {
- Serial.println("Nummer loeschen");
memset(number, '\0', NUMBER_LEN * sizeof(char));
digit = 0;