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.
Dependencies: TextLCD nRF24L01P mbed
Revision 12:752ad129f385, committed 2017-01-23
- Comitter:
- jackmax
- Date:
- Mon Jan 23 19:15:02 2017 +0000
- Parent:
- 11:c94b9db597e9
- Commit message:
- Par? zmian, raczej kosmetyczne
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| melodies.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r c94b9db597e9 -r 752ad129f385 main.cpp
--- a/main.cpp Mon Jan 23 13:07:47 2017 +0000
+++ b/main.cpp Mon Jan 23 19:15:02 2017 +0000
@@ -1,4 +1,9 @@
+/*
+I know the quality of the code is terrible, but we had to throw everything together
+quickly because of technical difficulties...
+*/
#include "mbed.h"
+//Pro tip: The TextLCD.h file contains #defines for choosing I2C adapter mappings.
#include "TextLCD.h"
#include "custom-chars.h"
#include "melodies.h"
@@ -24,9 +29,122 @@
void lightSensor(){
light_sum += light_sensor.read();
light_samples++;
+ //TODO: setting up a callback to do this would be a better idea
next_light = time(NULL) + LIGHT_INTERVAL;
}
+//Handling display
+void backlightTimeout();
+Timeout backlightTimer;
+
+class Display{
+public:
+ int wifi_on;
+ int alarm_on;
+ int sync_in_progress;
+ int sync_failed;
+ int wireless_in_progress;
+ int frame;
+
+ time_t seconds;
+ char time_str[9];
+ char date_str[11];
+
+ static const float backlightTime = 5.0;
+ int backlightState;
+ Display()
+ {
+ wifi_on = 0;
+ alarm_on = 0;
+ sync_in_progress = 0;
+ sync_failed = 0;
+ wireless_in_progress = 0;
+ frame = 0;
+ backlightState = 0;
+
+ lcd.setCursor(TextLCD::CurOff_BlkOff);
+ lcd.setUDC(C_ALRM, cc_dzwonek);
+ lcd.setUDC(C_WIFI, cc_wifi);
+ lcd.setUDC(C_WLC , cc_wireless);
+ }
+
+ void backlightOff(){
+ lcd.setBacklight(TextLCD::LightOff);
+ backlightState = 0;
+ }
+
+ void backlightOn(bool permanent = false){
+ lcd.setBacklight(TextLCD::LightOn);
+ backlightState = 1;
+ if (!permanent){
+ backlightTimer.attach(&backlightTimeout, backlightTime);
+ }
+
+ }
+
+ void update(){
+ //TODO: refresh the screen only if something changes, there's no reason to do it all the time
+ //Top row of display
+ char ico1 = ' ';
+ char ico2 = sync_in_progress ? C_WIFI : ' ';
+ char ico3 = ' ';
+ char alarm_time[6];
+ if (wireless_in_progress){
+ ico1 = frame % 2 ? C_WLC : ' ';
+ wireless_in_progress--;
+ }
+
+ time_t seconds_now = time(NULL);
+ if (seconds_now != seconds) {
+ seconds = seconds_now;
+ strftime(time_str, 9, "%X", localtime(&seconds));
+ strftime(date_str, 11, "%F", localtime(&seconds));
+ }
+
+ if (next_alarm != 0){
+ ico3 = C_ALRM;
+ if (next_alarm > seconds_now && next_alarm - seconds_now > 24*60*60){
+ //If the alarm is later than 24 hours from now, display date
+ strftime(alarm_time, 6, "%m-%d", localtime(&next_alarm));
+ }
+ else {
+ //If the alarm is within 24 hours, display hour and minute
+ strftime(alarm_time, 6, "%H:%M", localtime(&next_alarm));
+ }
+ }
+ else {
+ strcpy(alarm_time, " ");
+ }
+
+ lcd.locate(0,0); //Put in top row
+ lcd.printf("%8s%c%c%c%5s",time_str,ico1,ico2,ico3,alarm_time);
+
+ //Bottom row of display
+ lcd.locate(0,1); //Put in bottom row
+ if (sync_in_progress) {
+ lcd.printf("Synchronizacja..");
+ }
+ else if (sync_failed){
+ lcd.printf("Blad synchroniz.");
+ sync_failed--;
+ }
+ else if (alarm_on){
+ lcd.printf("Wstawaj! ");
+ }
+ else {
+ if (frame % 60 < 30) {
+ lcd.printf(" SmartAlarm Pro ");
+ }
+ else {
+ lcd.printf(" %10s ", date_str);
+ }
+ }
+ frame++;
+ }
+};
+
+Display disp;
+
//2.4 GHz radio
#define RX_ADDRESS ((unsigned long long) 0xABCDEF00)
#define TX_ADDRESS ((unsigned long long) 0xABCDEF01)
@@ -73,7 +191,7 @@
std::vector<std::string> last_wireless_data;
time_t last_wireless_time;
-//TODO: usredniac dane, w tym momencie wysyla tylko ostatnia wartosc!
+//TODO: average the data, not just keep the last value!
double last_min = 0, last_max = 0, last_avg = 0, last_temp = 0;
void processWirelessData(char* data_string){
@@ -98,6 +216,7 @@
last_max = atof(data[1].c_str());
last_avg = atof(data[2].c_str());
+ //TODO: Hardcoded values based on educated guessing
if (last_max > 0.6 && last_avg > 0.2){
pc.printf("moved\r\n");
movement_detected = true;
@@ -117,6 +236,7 @@
int rx_bytes=0;
if(radio.readable(NRF24L01P_PIPE_P1)){
+ disp.wireless_in_progress = 10; //Message will be displayed for 10 frames
rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, TRANSFER_SIZE);
rxData[TRANSFER_SIZE] = '\0';
pc.printf("Received %d >>%s<<\r\n",rx_bytes, rxData);
@@ -124,97 +244,6 @@
}
}
-//Handling display
-void backlightTimeout();
-//RtosTimer backlightTimer(&backlightTimeout, osTimerPeriodic, (void*)0);
-Timeout backlightTimer;
-
-class Display{
-public:
- int wifi_on;
- int alarm_on;
- int sync_in_progress;
- int wireless_in_progress;
- int frame;
-
- time_t seconds;
- char time_str[9];
- char date_str[9];
-
- static const float backlightTime = 5.000;
- int backlightState;
- Display()
- {
- wifi_on = 0;
- alarm_on = 0;
- sync_in_progress = 0;
- wireless_in_progress = 0;
- frame = 0;
- backlightState = 0;
-
- lcd.setCursor(TextLCD::CurOff_BlkOff);
- lcd.setUDC(C_ALRM, cc_dzwonek);
- lcd.setUDC(C_WIFI, cc_wifi);
- lcd.setUDC(C_WLC , cc_wireless);
- }
-
- void backlightOff(){
- lcd.setBacklight(TextLCD::LightOff);
- backlightState = 0;
- }
-
- void backlightOn(){
- lcd.setBacklight(TextLCD::LightOn);
- backlightState = 1;
- backlightTimer.attach(&backlightTimeout, backlightTime);
-
- }
-
- void update(){
- //Top row of display
- char ico1 = ' ';
- char ico2 = sync_in_progress ? (frame % 2 ? C_WIFI : ' ' ) : ' ';
- char ico3 = ' ';
- char alarm_time[6];
- if (next_alarm != 0){
- ico3 = C_ALRM;
- strftime(alarm_time, 6, "%H:%M", localtime(&next_alarm));
- }
- else {
- strcpy(alarm_time, " ");
- }
-
- time_t seconds_now = time(NULL);
- if (seconds_now != seconds) {
- seconds = seconds_now;
- strftime(time_str, 9, "%X", localtime(&seconds));
- strftime(date_str, 9, "%x", localtime(&seconds));
- }
-
- lcd.locate(0,0); //Put in top row
- lcd.printf("%s%c%c%c%s",time_str,ico1,ico2,ico3,alarm_time);
-
- lcd.locate(0,1); //Put in bottom row
- if (sync_in_progress) {
- lcd.printf("Synchronizacja..");
- }
- if (alarm_on){
- lcd.printf("Wstawaj! ");
- }
- else {
- if (frame % 60 < 30) {
- lcd.printf("SmartAlarm+ Pro ");
- }
- else {
- lcd.printf(" %08s ", date_str);
- }
- }
- frame++;
- }
-};
-
-Display disp;
-
//Handling user button presses
InterruptIn button(D6);
int userButtonLongPress = 300; //Time in ms; threshold for long press
@@ -242,6 +271,8 @@
bool readInProgress = false;
int currentReadIndex = 0;
+//Expects a response to start with "*" and end with another "*"
+//Ignores characters that are not between them
void wifiCallback() {
char c = wifi.getc();
@@ -265,6 +296,8 @@
return result;
}
+//TODO: There has to be a better way to make those requests,
+//but I don't have time to figure it out
std::string httpGETWithRetry(const char* query, float timeout, int retries){
Timer t;
for (int retry = 0; retry<retries; retry++){
@@ -288,10 +321,10 @@
bool sync_in_progress = false;
time_t getNextSync(){
- //TODO: zmiennej czasu nie powinno sie chyba tak modyfikowac
- return time (NULL) + 45;
+ return time (NULL) + 45; //TODO: synchronization hardcoded to every 45 seconds
}
+//TODO: hardcoded HTTP requests, argh
int syncFunction(){
disp.sync_in_progress = 1;
last_sync = time(NULL);
@@ -300,7 +333,7 @@
"HTTP/1.1\r\n"
"Host: 10.1.8.202:8080\r\n\r\n", 2.0, 5);
if (time_string == ""){
- //TODO: błąd synchronizacji
+ disp.sync_failed = 10;
}
else {
set_time(atoi(time_string.c_str()));
@@ -311,16 +344,12 @@
"HTTP/1.1\r\n"
"Host: 10.1.8.202:8080\r\n\r\n", 2.0, 5);
if (time_string == ""){
+ disp.sync_failed = 10;
}
else {
next_alarm = atoi(time_string.c_str());
}
- /*
- time_t now;
- time(&now);
- char time_str[50];
- strftime(time_str, 50, "%FT%TZ", gmtime(&now));
- */
+
//Get light sensor data
double light_sensor_avg;
light_sensor_avg = light_samples == 0 ? 0 : light_sum / light_samples ;
@@ -367,6 +396,7 @@
void start_alarm(){
disp.alarm_on = 1;
+ disp.backlightOn(true); //Set backlight to permanently on
alarm_playing = true;
next_note = 0;
next_note_timeout.attach(&play_next_note, 0.01);
@@ -380,15 +410,16 @@
wifi.printf("DELETE "
"/alarm/delete/nearest/58362c0bd0d41f4f91240f29 "
"HTTP/1.1\r\n"
- "Host: 10.1.8.202:8080\r\n\r\n");
+ "Host: 10.1.8.202:8080\r\n\r\n");
+ disp.backlightOn(); //Set backlight to normal again
}
-//TODO: won't work if melody contains -1 per at the beginning, has to have at least one note
+//TODO: won't work if melody contains -1 at the beginning, it has to have at least one note
void play_next_note(){
if (!alarm_playing)
return;
- const float bpm = 120.0f; //tempo
+ const float bpm = 120.0f; //TODO: it's not actually tempo, figure out how to do this
const float base_period = 0.25f;
const float base_time = bpm / 60.0f;
@@ -417,6 +448,9 @@
pc.printf("Waiting for time\r\n");
+ lcd.locate(0,0); //Put in top row
+ lcd.printf("Synchronizacja..");
+
std::string time_string = httpGETWithRetry("GET "
"/time/current "
"HTTP/1.1\r\n"
@@ -450,7 +484,7 @@
userButtonReleased = 0;
userButtonTimer.stop();
if (userButtonTimer.read_ms() > userButtonLongPress){
- pc.printf("User button long pressed");
+ pc.printf("Long press\r\n");
if (alarm_playing){
stop_alarm();
}
@@ -460,7 +494,7 @@
}
}
else {
- pc.printf("User button short pressed");
+ pc.printf("Short press\r\n");
disp.backlightOn();
}
}
@@ -486,11 +520,9 @@
//Check if alarm should be played
if (next_alarm != 0 && !alarm_playing){
- //pc.printf("alarm0\r\n");
- if (time(NULL) >= next_alarm-(15*60) ){ //Hardcoded to 15 minutes before set time
+ if (time(NULL) >= next_alarm-(15*60) ){ //TODO: Hardcoded to 15 minutes before set time
pc.printf("alarm1\r\n");
if (movement_detected || time(NULL) >= next_alarm){
- //pc.printf("alarm2\r\n");
start_alarm();
}
}
diff -r c94b9db597e9 -r 752ad129f385 melodies.h
--- a/melodies.h Mon Jan 23 13:07:47 2017 +0000
+++ b/melodies.h Mon Jan 23 19:15:02 2017 +0000
@@ -1,2 +1,3 @@
+//Converted from some old Nokia ringtone I found on the internet
const int axelf_freq[28]={98,116,98,0,98,131,98,87,98,147,98,0,98,155,147,116,98,147,196,98,87,0,87,73,110,98,0,-1};
const int axelf_per [28]={4,8,16,16,16,8,8,8,4,8,16,16,16,8,8,8,8,8,8,16,16,16,16,8,8,2,1,-1};
\ No newline at end of file

