シカケコンテスト2015で発表した「水分補給お知らせペットボトルホルダー」

Dependencies:   MPR121 NokiaLCD SDFileSystem mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "SDFileSystem.h"
00004 #include "MPR121.h"
00005 #include "wave_player.h"
00006 #include "MARMEX_OB_oled.h"
00007 #include <string>
00008 
00009 // for debug
00010 DigitalOut  mbed_leds[]  = {
00011     DigitalOut(LED1),
00012     DigitalOut(LED2),
00013     DigitalOut(LED3),
00014     DigitalOut(LED4)
00015 };
00016 Serial pc(USBTX, USBRX);
00017 
00018 // sensors
00019 AnalogIn accelerometer[] = {
00020     AnalogIn(p17),
00021     AnalogIn(p16),
00022     AnalogIn(p15)
00023 };
00024 float x_value, y_value, z_value;
00025 
00026 InterruptIn irq(p26);
00027 I2C i2c(p9, p10);
00028 MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
00029 
00030 // sd card
00031 SDFileSystem sd(p5, p6, p7, p8, "sd");
00032 
00033 // LED
00034 DigitalOut  leds[6]  = {
00035     DigitalOut(p22),
00036     DigitalOut(p23),
00037     DigitalOut(p24),
00038     DigitalOut(p25),
00039     DigitalOut(p27),
00040     DigitalOut(p28)
00041 };
00042 int counter_num = 6;
00043 
00044 // speaker
00045 AnalogOut aout(p18);
00046 wave_player waver(&aout);
00047 
00048 void play_sound(string file_path) {
00049     pc.printf("Play : %s\r\n", file_path.c_str());
00050     FILE *wave_file = fopen(file_path.c_str(), "r");
00051     if(wave_file == NULL) {
00052         pc.printf("Could not open wave file.\r\n");
00053     }
00054     waver.play(wave_file);
00055     fclose(wave_file);
00056 }
00057 
00058 // oled
00059 MARMEX_OB_oled oled_s(p11, p13, p30, p14, p29); // mosi, sclk, cs, rst, power_control
00060 uint8_t c[3];
00061 string current_image;
00062 
00063 void display_bmp(string file_path) {
00064     if(current_image == file_path) {
00065         pc.printf("Specify same bmp file.\r\n");
00066         return;
00067     }
00068     pc.printf("Display : %s\r\n", file_path.c_str());
00069     current_image = file_path;
00070     FILE *f = fopen(file_path.c_str(), "rb");
00071     if(f == NULL) {
00072         pc.printf("Could not open bmp file.\r\n");
00073     }
00074     fseek(f, 54, 1);
00075     int color;
00076     for(int y=127; y >= 0; y--){
00077         for(int x=127; x >= 0; x--){
00078             for(int i=0;i < 3;i++){
00079                 c[i] = fgetc(f);
00080             }
00081             color = c[0] | (c[1] << 8) | (c[2] << 16);
00082             oled_s.pixel(x, y, color);
00083         }
00084     }
00085     fclose(f);
00086 }
00087 
00088 // switch
00089 DigitalIn sw(p21);
00090 
00091 void update_acceleration() {
00092     mbed_leds[1] = !mbed_leds[1];
00093     float x, y, z;
00094     x = y = z = 0.0;
00095     for (int i=0 ; i < 100 ; i++) {
00096           x = x + accelerometer[0].read();
00097           y = y + accelerometer[1].read();
00098           z = z + accelerometer[2].read();
00099     }
00100     x_value = x / 100;
00101     y_value = y / 100;
00102     z_value = z / 100;
00103 }
00104 
00105 bool is_drinking() {
00106     if(z_value > 0.35){
00107         if(touch_pad.isPressed()) {
00108             uint16_t button_val = touch_pad.buttonPressed();
00109             pc.printf("button = 0x%04x\r\n", button_val);
00110             if(button_val > 0){
00111                 mbed_leds[0] = 1;
00112                 pc.printf("Drinking...\r\n");
00113                 return true;
00114             }
00115         }
00116     }
00117     mbed_leds[0] = 0;
00118     return false;
00119 }
00120 
00121 void update_leds() {
00122     for(int i=0; i < 6; i++){
00123         leds[i] = (i < counter_num) ? 1 : 0;
00124     }
00125 }
00126 
00127 void decreament_counter(void const *args) {
00128     mbed_leds[2] = !mbed_leds[2];
00129     counter_num--;
00130     if(counter_num < 0) {
00131         counter_num = 0;
00132     }
00133     update_leds();
00134 }
00135 
00136 void reset_counter() {
00137     if(counter_num != 6) {
00138         pc.printf("Reset...\r\n");
00139         display_bmp("/sd/default.bmp");
00140         counter_num = 6;
00141         update_leds();
00142     }
00143 }
00144 
00145 void notify_drinking() {
00146     pc.printf("Notify...\r\n");
00147     display_bmp("/sd/sad.bmp");
00148     play_sound("/sd/elephant.wav");
00149 }
00150 
00151 int main() {
00152     sw.mode(PullUp);
00153 
00154     oled_s.background( 0xFFFFFF );
00155     oled_s.cls();
00156     display_bmp("/sd/default.bmp");
00157     
00158     touch_pad.init();
00159     touch_pad.enable();
00160     
00161     RtosTimer counter_timer(decreament_counter, osTimerPeriodic, NULL);
00162     counter_timer.start(60000 * 5); // 5min * 6LEDs = 30min
00163     update_leds();
00164 
00165     while(1) {
00166         update_acceleration();
00167         pc.printf("X:%f Y:%f Z:%f\r\n", x_value, y_value, z_value);
00168         if(is_drinking()) {
00169             reset_counter();
00170             play_sound("/sd/drinking.wav");
00171         }
00172         if(sw == 0) { // for demo
00173             decreament_counter(NULL);
00174         }
00175         if(counter_num <= 0) {
00176             notify_drinking();
00177         }
00178         Thread::wait(500);
00179     }
00180 }