for baska
Dependencies: HeptaBattery HeptaTemp SDFileSystem mbed
Fork of Lab3-05_model_program_2 by
main.cpp@19:62ab01e42129, 2018-08-22 (annotated)
- Committer:
- ponpoko1939
- Date:
- Wed Aug 22 01:12:24 2018 +0000
- Revision:
- 19:62ab01e42129
- Parent:
- 17:f4fd402f8e93
1stQuestion
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
mbed_official | 0:bdbd3d6fc5d5 | 2 | #include "SDFileSystem.h" |
umeume | 2:1c5cdb2c3e0f | 3 | #include "HeptaBattery.h" |
ponpoko1939 | 19:62ab01e42129 | 4 | #include "HeptaTemp.h" |
umeume | 2:1c5cdb2c3e0f | 5 | |
umeume | 2:1c5cdb2c3e0f | 6 | Serial pc(USBTX,USBRX); |
HEPTA | 9:942eb6e8c117 | 7 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
HEPTA | 13:a1fa75a002f6 | 8 | HeptaBattery battery(p16,p26); |
ponpoko1939 | 19:62ab01e42129 | 9 | HeptaTemp temper(p17); |
umeume | 2:1c5cdb2c3e0f | 10 | |
HEPTA | 17:f4fd402f8e93 | 11 | DigitalOut myleds[] = {LED1,LED2,LED3,LED4};; |
HEPTA | 17:f4fd402f8e93 | 12 | |
HEPTA | 17:f4fd402f8e93 | 13 | int rcmd = 0,cmdflag = 0; |
HEPTA | 17:f4fd402f8e93 | 14 | |
HEPTA | 17:f4fd402f8e93 | 15 | void commandget() |
HEPTA | 17:f4fd402f8e93 | 16 | { |
HEPTA | 17:f4fd402f8e93 | 17 | rcmd=pc.getc(); |
HEPTA | 17:f4fd402f8e93 | 18 | cmdflag = 1; |
HEPTA | 17:f4fd402f8e93 | 19 | } |
HEPTA | 17:f4fd402f8e93 | 20 | void receive(int *xrcmd, int *xcmdflag) |
HEPTA | 17:f4fd402f8e93 | 21 | { |
HEPTA | 17:f4fd402f8e93 | 22 | pc.attach(commandget,Serial::RxIrq); |
HEPTA | 17:f4fd402f8e93 | 23 | *xrcmd = rcmd; |
HEPTA | 17:f4fd402f8e93 | 24 | *xcmdflag = cmdflag; |
HEPTA | 17:f4fd402f8e93 | 25 | } |
HEPTA | 17:f4fd402f8e93 | 26 | |
HEPTA | 17:f4fd402f8e93 | 27 | void initialize() |
HEPTA | 17:f4fd402f8e93 | 28 | { |
HEPTA | 17:f4fd402f8e93 | 29 | rcmd = 0; |
HEPTA | 17:f4fd402f8e93 | 30 | cmdflag = 0; |
HEPTA | 17:f4fd402f8e93 | 31 | } |
umeume | 5:c5ccb1b07e8f | 32 | |
umeume | 2:1c5cdb2c3e0f | 33 | int main() |
umeume | 2:1c5cdb2c3e0f | 34 | { |
HEPTA | 17:f4fd402f8e93 | 35 | pc.baud(9600); |
HEPTA | 17:f4fd402f8e93 | 36 | float bt; |
ponpoko1939 | 19:62ab01e42129 | 37 | float temp; |
HEPTA | 17:f4fd402f8e93 | 38 | int flag = 0; |
ponpoko1939 | 19:62ab01e42129 | 39 | int flagtmp = 0; |
HEPTA | 17:f4fd402f8e93 | 40 | char str[100]; |
HEPTA | 17:f4fd402f8e93 | 41 | while(1) { |
HEPTA | 17:f4fd402f8e93 | 42 | battery.vol(&bt); |
ponpoko1939 | 19:62ab01e42129 | 43 | temper.temp_sense(&temp); |
HEPTA | 17:f4fd402f8e93 | 44 | if(bt <= 3.5) { |
HEPTA | 17:f4fd402f8e93 | 45 | battery.shut_down_regulator(); |
HEPTA | 17:f4fd402f8e93 | 46 | flag = 1; |
HEPTA | 17:f4fd402f8e93 | 47 | } else { |
HEPTA | 17:f4fd402f8e93 | 48 | battery.turn_on_regulator(); |
HEPTA | 17:f4fd402f8e93 | 49 | flag = 0; |
HEPTA | 17:f4fd402f8e93 | 50 | } |
ponpoko1939 | 19:62ab01e42129 | 51 | if(temp > 30){ |
ponpoko1939 | 19:62ab01e42129 | 52 | flagtmp = 1; |
ponpoko1939 | 19:62ab01e42129 | 53 | } |
ponpoko1939 | 19:62ab01e42129 | 54 | else { |
ponpoko1939 | 19:62ab01e42129 | 55 | flagtmp = 0; |
ponpoko1939 | 19:62ab01e42129 | 56 | } |
HEPTA | 17:f4fd402f8e93 | 57 | pc.printf("Vol = %.2f [V]\r\n",bt); |
ponpoko1939 | 19:62ab01e42129 | 58 | pc.printf("temp = %f\r\n",temp); |
HEPTA | 17:f4fd402f8e93 | 59 | if(flag == 1) { |
HEPTA | 17:f4fd402f8e93 | 60 | pc.printf("Power saving mode ON\r\n"); |
HEPTA | 17:f4fd402f8e93 | 61 | } |
ponpoko1939 | 19:62ab01e42129 | 62 | if(flagtmp == 1){ |
ponpoko1939 | 19:62ab01e42129 | 63 | pc.printf("High Temperture\r\n"); |
ponpoko1939 | 19:62ab01e42129 | 64 | } |
ponpoko1939 | 19:62ab01e42129 | 65 | if((flag == 1) &(flagtmp == 1)){ |
ponpoko1939 | 19:62ab01e42129 | 66 | pc.printf("Low Battery and High Temperture\r\n"); |
ponpoko1939 | 19:62ab01e42129 | 67 | } |
HEPTA | 17:f4fd402f8e93 | 68 | wait(0.5); |
HEPTA | 17:f4fd402f8e93 | 69 | receive(&rcmd,&cmdflag); |
HEPTA | 17:f4fd402f8e93 | 70 | if (cmdflag == 1) { |
HEPTA | 17:f4fd402f8e93 | 71 | pc.printf("Command Get = %d\r\n",rcmd); |
HEPTA | 17:f4fd402f8e93 | 72 | if (rcmd == 'a') { |
HEPTA | 17:f4fd402f8e93 | 73 | for(int i = 0; i < 10; i++) { |
HEPTA | 17:f4fd402f8e93 | 74 | pc.printf("Count = %d\r\n",i); |
HEPTA | 17:f4fd402f8e93 | 75 | wait(1.0); |
HEPTA | 17:f4fd402f8e93 | 76 | } |
HEPTA | 17:f4fd402f8e93 | 77 | } |
HEPTA | 17:f4fd402f8e93 | 78 | if (rcmd == 'b') { |
ponpoko1939 | 19:62ab01e42129 | 79 | FILE *fp = fopen("/sd/mydir/test.txt","w"); |
ponpoko1939 | 19:62ab01e42129 | 80 | if(fp == NULL) { |
ponpoko1939 | 19:62ab01e42129 | 81 | error("Could not open file for write\r\n"); |
ponpoko1939 | 19:62ab01e42129 | 82 | } |
ponpoko1939 | 19:62ab01e42129 | 83 | for(int i = 0; i <= 10;i++){ |
ponpoko1939 | 19:62ab01e42129 | 84 | battery.vol(&bt); |
ponpoko1939 | 19:62ab01e42129 | 85 | pc.printf("Vol = %.2f\r\n",bt); |
ponpoko1939 | 19:62ab01e42129 | 86 | wait(1.0); |
ponpoko1939 | 19:62ab01e42129 | 87 | fprintf(fp,"%f\r\n",bt); |
ponpoko1939 | 19:62ab01e42129 | 88 | } |
ponpoko1939 | 19:62ab01e42129 | 89 | fclose(fp); |
ponpoko1939 | 19:62ab01e42129 | 90 | fp = fopen("/sd/mydir/test.txt","r"); |
ponpoko1939 | 19:62ab01e42129 | 91 | for(int j = 0; j < 10; j++) { |
ponpoko1939 | 19:62ab01e42129 | 92 | fgets(str,100,fp); |
ponpoko1939 | 19:62ab01e42129 | 93 | puts(str); |
ponpoko1939 | 19:62ab01e42129 | 94 | } |
ponpoko1939 | 19:62ab01e42129 | 95 | fclose(fp); |
ponpoko1939 | 19:62ab01e42129 | 96 | } |
HEPTA | 17:f4fd402f8e93 | 97 | } |
HEPTA | 17:f4fd402f8e93 | 98 | initialize(); |
HEPTA | 17:f4fd402f8e93 | 99 | } |
HEPTA | 17:f4fd402f8e93 | 100 | } |