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

Dependencies:   MPR121 NokiaLCD SDFileSystem mbed-rtos mbed wave_player

Committer:
mia_0032
Date:
Fri Jul 10 09:19:15 2015 +0000
Revision:
0:01f630b61d0f
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mia_0032 0:01f630b61d0f 1 #include "mbed.h"
mia_0032 0:01f630b61d0f 2 #include "rtos.h"
mia_0032 0:01f630b61d0f 3 #include "SDFileSystem.h"
mia_0032 0:01f630b61d0f 4 #include "MPR121.h"
mia_0032 0:01f630b61d0f 5 #include "wave_player.h"
mia_0032 0:01f630b61d0f 6 #include "MARMEX_OB_oled.h"
mia_0032 0:01f630b61d0f 7 #include <string>
mia_0032 0:01f630b61d0f 8
mia_0032 0:01f630b61d0f 9 // for debug
mia_0032 0:01f630b61d0f 10 DigitalOut mbed_leds[] = {
mia_0032 0:01f630b61d0f 11 DigitalOut(LED1),
mia_0032 0:01f630b61d0f 12 DigitalOut(LED2),
mia_0032 0:01f630b61d0f 13 DigitalOut(LED3),
mia_0032 0:01f630b61d0f 14 DigitalOut(LED4)
mia_0032 0:01f630b61d0f 15 };
mia_0032 0:01f630b61d0f 16 Serial pc(USBTX, USBRX);
mia_0032 0:01f630b61d0f 17
mia_0032 0:01f630b61d0f 18 // sensors
mia_0032 0:01f630b61d0f 19 AnalogIn accelerometer[] = {
mia_0032 0:01f630b61d0f 20 AnalogIn(p17),
mia_0032 0:01f630b61d0f 21 AnalogIn(p16),
mia_0032 0:01f630b61d0f 22 AnalogIn(p15)
mia_0032 0:01f630b61d0f 23 };
mia_0032 0:01f630b61d0f 24 float x_value, y_value, z_value;
mia_0032 0:01f630b61d0f 25
mia_0032 0:01f630b61d0f 26 InterruptIn irq(p26);
mia_0032 0:01f630b61d0f 27 I2C i2c(p9, p10);
mia_0032 0:01f630b61d0f 28 MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
mia_0032 0:01f630b61d0f 29
mia_0032 0:01f630b61d0f 30 // sd card
mia_0032 0:01f630b61d0f 31 SDFileSystem sd(p5, p6, p7, p8, "sd");
mia_0032 0:01f630b61d0f 32
mia_0032 0:01f630b61d0f 33 // LED
mia_0032 0:01f630b61d0f 34 DigitalOut leds[6] = {
mia_0032 0:01f630b61d0f 35 DigitalOut(p22),
mia_0032 0:01f630b61d0f 36 DigitalOut(p23),
mia_0032 0:01f630b61d0f 37 DigitalOut(p24),
mia_0032 0:01f630b61d0f 38 DigitalOut(p25),
mia_0032 0:01f630b61d0f 39 DigitalOut(p27),
mia_0032 0:01f630b61d0f 40 DigitalOut(p28)
mia_0032 0:01f630b61d0f 41 };
mia_0032 0:01f630b61d0f 42 int counter_num = 6;
mia_0032 0:01f630b61d0f 43
mia_0032 0:01f630b61d0f 44 // speaker
mia_0032 0:01f630b61d0f 45 AnalogOut aout(p18);
mia_0032 0:01f630b61d0f 46 wave_player waver(&aout);
mia_0032 0:01f630b61d0f 47
mia_0032 0:01f630b61d0f 48 void play_sound(string file_path) {
mia_0032 0:01f630b61d0f 49 pc.printf("Play : %s\r\n", file_path.c_str());
mia_0032 0:01f630b61d0f 50 FILE *wave_file = fopen(file_path.c_str(), "r");
mia_0032 0:01f630b61d0f 51 if(wave_file == NULL) {
mia_0032 0:01f630b61d0f 52 pc.printf("Could not open wave file.\r\n");
mia_0032 0:01f630b61d0f 53 }
mia_0032 0:01f630b61d0f 54 waver.play(wave_file);
mia_0032 0:01f630b61d0f 55 fclose(wave_file);
mia_0032 0:01f630b61d0f 56 }
mia_0032 0:01f630b61d0f 57
mia_0032 0:01f630b61d0f 58 // oled
mia_0032 0:01f630b61d0f 59 MARMEX_OB_oled oled_s(p11, p13, p30, p14, p29); // mosi, sclk, cs, rst, power_control
mia_0032 0:01f630b61d0f 60 uint8_t c[3];
mia_0032 0:01f630b61d0f 61 string current_image;
mia_0032 0:01f630b61d0f 62
mia_0032 0:01f630b61d0f 63 void display_bmp(string file_path) {
mia_0032 0:01f630b61d0f 64 if(current_image == file_path) {
mia_0032 0:01f630b61d0f 65 pc.printf("Specify same bmp file.\r\n");
mia_0032 0:01f630b61d0f 66 return;
mia_0032 0:01f630b61d0f 67 }
mia_0032 0:01f630b61d0f 68 pc.printf("Display : %s\r\n", file_path.c_str());
mia_0032 0:01f630b61d0f 69 current_image = file_path;
mia_0032 0:01f630b61d0f 70 FILE *f = fopen(file_path.c_str(), "rb");
mia_0032 0:01f630b61d0f 71 if(f == NULL) {
mia_0032 0:01f630b61d0f 72 pc.printf("Could not open bmp file.\r\n");
mia_0032 0:01f630b61d0f 73 }
mia_0032 0:01f630b61d0f 74 fseek(f, 54, 1);
mia_0032 0:01f630b61d0f 75 int color;
mia_0032 0:01f630b61d0f 76 for(int y=127; y >= 0; y--){
mia_0032 0:01f630b61d0f 77 for(int x=127; x >= 0; x--){
mia_0032 0:01f630b61d0f 78 for(int i=0;i < 3;i++){
mia_0032 0:01f630b61d0f 79 c[i] = fgetc(f);
mia_0032 0:01f630b61d0f 80 }
mia_0032 0:01f630b61d0f 81 color = c[0] | (c[1] << 8) | (c[2] << 16);
mia_0032 0:01f630b61d0f 82 oled_s.pixel(x, y, color);
mia_0032 0:01f630b61d0f 83 }
mia_0032 0:01f630b61d0f 84 }
mia_0032 0:01f630b61d0f 85 fclose(f);
mia_0032 0:01f630b61d0f 86 }
mia_0032 0:01f630b61d0f 87
mia_0032 0:01f630b61d0f 88 // switch
mia_0032 0:01f630b61d0f 89 DigitalIn sw(p21);
mia_0032 0:01f630b61d0f 90
mia_0032 0:01f630b61d0f 91 void update_acceleration() {
mia_0032 0:01f630b61d0f 92 mbed_leds[1] = !mbed_leds[1];
mia_0032 0:01f630b61d0f 93 float x, y, z;
mia_0032 0:01f630b61d0f 94 x = y = z = 0.0;
mia_0032 0:01f630b61d0f 95 for (int i=0 ; i < 100 ; i++) {
mia_0032 0:01f630b61d0f 96 x = x + accelerometer[0].read();
mia_0032 0:01f630b61d0f 97 y = y + accelerometer[1].read();
mia_0032 0:01f630b61d0f 98 z = z + accelerometer[2].read();
mia_0032 0:01f630b61d0f 99 }
mia_0032 0:01f630b61d0f 100 x_value = x / 100;
mia_0032 0:01f630b61d0f 101 y_value = y / 100;
mia_0032 0:01f630b61d0f 102 z_value = z / 100;
mia_0032 0:01f630b61d0f 103 }
mia_0032 0:01f630b61d0f 104
mia_0032 0:01f630b61d0f 105 bool is_drinking() {
mia_0032 0:01f630b61d0f 106 if(z_value > 0.35){
mia_0032 0:01f630b61d0f 107 if(touch_pad.isPressed()) {
mia_0032 0:01f630b61d0f 108 uint16_t button_val = touch_pad.buttonPressed();
mia_0032 0:01f630b61d0f 109 pc.printf("button = 0x%04x\r\n", button_val);
mia_0032 0:01f630b61d0f 110 if(button_val > 0){
mia_0032 0:01f630b61d0f 111 mbed_leds[0] = 1;
mia_0032 0:01f630b61d0f 112 pc.printf("Drinking...\r\n");
mia_0032 0:01f630b61d0f 113 return true;
mia_0032 0:01f630b61d0f 114 }
mia_0032 0:01f630b61d0f 115 }
mia_0032 0:01f630b61d0f 116 }
mia_0032 0:01f630b61d0f 117 mbed_leds[0] = 0;
mia_0032 0:01f630b61d0f 118 return false;
mia_0032 0:01f630b61d0f 119 }
mia_0032 0:01f630b61d0f 120
mia_0032 0:01f630b61d0f 121 void update_leds() {
mia_0032 0:01f630b61d0f 122 for(int i=0; i < 6; i++){
mia_0032 0:01f630b61d0f 123 leds[i] = (i < counter_num) ? 1 : 0;
mia_0032 0:01f630b61d0f 124 }
mia_0032 0:01f630b61d0f 125 }
mia_0032 0:01f630b61d0f 126
mia_0032 0:01f630b61d0f 127 void decreament_counter(void const *args) {
mia_0032 0:01f630b61d0f 128 mbed_leds[2] = !mbed_leds[2];
mia_0032 0:01f630b61d0f 129 counter_num--;
mia_0032 0:01f630b61d0f 130 if(counter_num < 0) {
mia_0032 0:01f630b61d0f 131 counter_num = 0;
mia_0032 0:01f630b61d0f 132 }
mia_0032 0:01f630b61d0f 133 update_leds();
mia_0032 0:01f630b61d0f 134 }
mia_0032 0:01f630b61d0f 135
mia_0032 0:01f630b61d0f 136 void reset_counter() {
mia_0032 0:01f630b61d0f 137 if(counter_num != 6) {
mia_0032 0:01f630b61d0f 138 pc.printf("Reset...\r\n");
mia_0032 0:01f630b61d0f 139 display_bmp("/sd/default.bmp");
mia_0032 0:01f630b61d0f 140 counter_num = 6;
mia_0032 0:01f630b61d0f 141 update_leds();
mia_0032 0:01f630b61d0f 142 }
mia_0032 0:01f630b61d0f 143 }
mia_0032 0:01f630b61d0f 144
mia_0032 0:01f630b61d0f 145 void notify_drinking() {
mia_0032 0:01f630b61d0f 146 pc.printf("Notify...\r\n");
mia_0032 0:01f630b61d0f 147 display_bmp("/sd/sad.bmp");
mia_0032 0:01f630b61d0f 148 play_sound("/sd/elephant.wav");
mia_0032 0:01f630b61d0f 149 }
mia_0032 0:01f630b61d0f 150
mia_0032 0:01f630b61d0f 151 int main() {
mia_0032 0:01f630b61d0f 152 sw.mode(PullUp);
mia_0032 0:01f630b61d0f 153
mia_0032 0:01f630b61d0f 154 oled_s.background( 0xFFFFFF );
mia_0032 0:01f630b61d0f 155 oled_s.cls();
mia_0032 0:01f630b61d0f 156 display_bmp("/sd/default.bmp");
mia_0032 0:01f630b61d0f 157
mia_0032 0:01f630b61d0f 158 touch_pad.init();
mia_0032 0:01f630b61d0f 159 touch_pad.enable();
mia_0032 0:01f630b61d0f 160
mia_0032 0:01f630b61d0f 161 RtosTimer counter_timer(decreament_counter, osTimerPeriodic, NULL);
mia_0032 0:01f630b61d0f 162 counter_timer.start(60000 * 5); // 5min * 6LEDs = 30min
mia_0032 0:01f630b61d0f 163 update_leds();
mia_0032 0:01f630b61d0f 164
mia_0032 0:01f630b61d0f 165 while(1) {
mia_0032 0:01f630b61d0f 166 update_acceleration();
mia_0032 0:01f630b61d0f 167 pc.printf("X:%f Y:%f Z:%f\r\n", x_value, y_value, z_value);
mia_0032 0:01f630b61d0f 168 if(is_drinking()) {
mia_0032 0:01f630b61d0f 169 reset_counter();
mia_0032 0:01f630b61d0f 170 play_sound("/sd/drinking.wav");
mia_0032 0:01f630b61d0f 171 }
mia_0032 0:01f630b61d0f 172 if(sw == 0) { // for demo
mia_0032 0:01f630b61d0f 173 decreament_counter(NULL);
mia_0032 0:01f630b61d0f 174 }
mia_0032 0:01f630b61d0f 175 if(counter_num <= 0) {
mia_0032 0:01f630b61d0f 176 notify_drinking();
mia_0032 0:01f630b61d0f 177 }
mia_0032 0:01f630b61d0f 178 Thread::wait(500);
mia_0032 0:01f630b61d0f 179 }
mia_0032 0:01f630b61d0f 180 }