CLTP統合検証プログラム_気圧とxbeeのみで,圧電スピーカの実装が必要

Dependencies:   mbed HEPTA_CDH_lite LPS25HB_I2C

main.cpp

Committer:
wasabimal
Date:
2022-08-16
Revision:
2:bc6e512b58ac
Parent:
1:ddac5ec89167
Child:
3:08260b6b0828

File content as of revision 2:bc6e512b58ac:

#include "mbed.h"
#include "LPS.h"
#include "HEPTA_CDH.h"
Timer t;
Serial pc(USBTX, USBRX); // tx, rx
Serial xbee(D1, D0); // tx, rx
I2C i2c(D4,D5);
LPS ps(i2c);
HEPTA_CDH cdh(PB_5, PB_4, PB_3, PA_8, "sd");
Ticker press;
int i;
int ct = 0;
float pressure = 0;
float altitude = 0;
float temperature = 0;
char cmd1,cmd2,cmd3;
void pre() {
    xbee.printf("%.2f %.2f %.2f \r\n",pressure,altitude,temperature);
}
int main() {//1
    //Display from satellite to PC via xbee.
    pc.printf("settig start\r\n");
    xbee.printf("settig start\r\n");
    
    //Create a file to save the data acquired from the sensor to a microSD card
    //Also, if there is no microSD card, this error statement will be displayed
    mkdir("/sd/mydir", 0777);
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\r\n");
    }
    
    //If there is no Barometric Pressure Sensors, this error statement will appear in teraterm
    if (!ps.init()){//2
        printf("Failed to autodetect pressure sensor!\r\n");
        while (1);
    }//2
    ps.enableDefault();
    
    //Display "begin" in teraterm and save it to SDcard
    pc.printf("begin\r\n");
    xbee.printf("begin\r\n");
    fprintf(fp,"begin\r\n");
    for (;;) {//2
        //if (pc.readable()) xbee.putc(pc.getc());
        if (xbee.readable()) {//3
            pc.putc(xbee.getc());
            cmd1 = xbee.getc();
        //char cmd1 = xbee.getc();//cmd共有
        
        //Input "m", the sensor data is displayed on the teraterm.
        //And the acquired data is saved to the SD card.
            if(cmd1 == 'm'){//4
                pc.printf("start\r\n");
                fprintf(fp,"start\r\n");
                xbee.printf("start\r\n");
                t.start();
                
                cmd1 = '\0';
                
                pc.printf("p [mbar]\ta [m]\tdeg [C]\r\n");
                xbee.printf("p [mbar]\ta [m]\tdeg [C]\r\n");
                fprintf(fp,"p [mbar]\ta [m]\tdeg [C]\r\n");
                fclose(fp);
                press.attach(&pre, 0.1);
                //Save sensor acquisition data to SD card every 10 times
                while(1){//5
                    FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
                    for(i = 0;i<10;i++){//6
                        pressure = ps.readPressureMillibars();
                        altitude = ps.pressureToAltitudeMeters(pressure);
                        temperature = ps.readTemperatureC();
                        fprintf(fp,"%d %.2f\t \t%.2f \t%.2f \r\n",ct,pressure,altitude,temperature);
                        ct = ct + 1;
                        //if (pc.readable()) xbee.putc(pc.getc());
                        if (xbee.readable()){//7
                            pc.putc(xbee.getc());
                            cmd2 = xbee.getc();
                            //char cmd2 = xbee.getc();
                            //xbee.printf("%c\r\n",cmd);
                            if(cmd2 == 'n'){//8
                                press.detach();
                                pc.printf("end\r\n");
                                pc.printf("time = %f",t.read());
                                fprintf(fp,"time = %f",t.read());
                                t.stop();
                                fclose(fp);
                                cmd2 = '\0';
                                return 0;
                                            }//8
                                        }//7
                        wait_us(1);
                    }//6
                    fclose(fp);
                }//5
            }//4
        }//3
    }//2
}//1