PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

source/main.cpp

Committer:
aeschsim
Date:
2018-04-06
Revision:
76:53a25f1dc2b6
Parent:
71:3d0a4dbff536
Parent:
74:a4dacab0d3dd
Child:
79:c825bd2dea81

File content as of revision 76:53a25f1dc2b6:

#include "main.h"
#define     FADESPEED   5
#define     DEMOTIME    5

int state = 44;

int oldState;

Serial pc(USBTX, USBRX); // tx, rx

DigitalIn   lichtschranke(PC_5);
InterruptIn userButton1(PB_5);
InterruptIn userButton2(PB_4);
DigitalIn   glasDetection(PA_10);
PwmOut      summer(PC_8);

/* PWM Test Tool */
int channel;
int dutyCycle;
int day;
int moment;
s_user prescription[2];

/* Demo Tool */
uint32_t color = 0;
int colorTime = 0;
int r,b,g;

/* SD write Test*/

s_time medicationTime;
s_time outputTime;
s_medContainer medication;
s_medContainer medInventory;
s_time currentTime;

int success = 1;

char user = 0;

char med1[] = "A";
char med2[] = "B";
char med3[] = "C";
char med4[] = "D";
char med5[] = "E";
char med6[] = "F";

char fail[] = "shit happens";
/*********/

void summ(void)
{
    if(summer.read()>0) {
        summer.write(0.0f);
    } else {
        summer.write(0.5f);
    }
}


int main()
{
    /* SD write Test*/
    medicationTime.hour = 15;
    medicationTime.minute = 30;

    outputTime.day = getTime().day;
    outputTime.month = getTime().month;
    outputTime.year = getTime().year;
    outputTime.hour = getTime().hour;
    outputTime.minute = getTime().minute;


    medication.container[0] = 1;
    medication.container[1] = 2;
    medication.container[2] = 2;
    medication.container[3] = 1;
    medication.container[4] = 1;
    medication.container[5] = 2;


    medInventory.container[0] = 5;
    medInventory.container[1] = 10;
    medInventory.container[2] = 15;
    medInventory.container[3] = 20;
    medInventory.container[4] = 25;
    medInventory.container[5] = 30;


    currentTime.day = getTime().day;
    currentTime.month = getTime().month;
    currentTime.year = getTime().year;
    currentTime.hour = getTime().hour;
    currentTime.minute = getTime().minute;
    /************/

    pc.baud(460800);
    printf("start...\r\n");

    while(1) {
        if(state != oldState) {
            printf("\r\nstate: %d\r\n",state);
            oldState = state;
        }
        switch (state) {
            case 0:
                printf("Hello World");
                state = 47;
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;

            case 10:
                break;
            case 11:
                break;

            case 15:
                break;
            case 16:
                break;
            case 17:
                break;

            case 25:
                break;
            case 26:
                break;
            case 27:
                break;
            case 28:
                break;


            case 35:
                break;
            case 36:
                break;
            case 37:
                break;
            case 38:
                break;


                /**************************************************************/
                /* Test Zone */

                /* init PWM board */
            case 44:
                printf("Init PWM\r\n");
                initPWM(50);
                enableOutput();
                state = 47;
                break;

                /* Time test tool: set Time */
            case 45:
                s_time time;
                time.second     =   30;
                time.minute     =   53;
                time.hour       =   12;
                time.day        =   27;
                time.weekday    =   2;
                time.month      =   3;
                time.year       =   18;
                printf("set now time\r\n");
                setTime(&time);
                printf("read time: %02d:%02d:%02d, %02d.%02d.20%02d, weekday: %d\r\n",time.hour, time.minute, time.second, time.day, time.month, time.year, time.weekday);
                state = 47;
                printf("change state to: %d\r\n",state);
                break;

                /* Time test tool: get Time */
            case 46:
                wait(5);
                time = getTime();
                printf("read time: %02d:%02d:%02d, %02d.%02d.20%02d, weekday: %d\r\n",time.hour, time.minute, time.second, time.day, time.month, time.year, time.weekday);
                state = 47;
                break;

                /* Console State Switch */
            case 47:
                printf("\n\rState Switch: Enter next State: \n\r");
                printf("45\tset Time\n\r46\tread Time\n\r47\tState Switch\n\r48\tServo Board Test Tool\n\r");
                printf("49\tenable servo output\n\r50\tdisable servo output\n\r");
                printf("51\ttest SD Card\n\r52\tread medication\n\r53\tMedication display test tool\n\r");
                printf("54\tWrite Medication\n\r55\tDemo Tool for Summer and RGB LED\n\r");
                printf("60\t        \n\r");
                printf("61\t        \n\r");
                printf("62\t        \n\r");
                pc.scanf("%d", &state);
                printf("Switch to State %d\n\r",state);
                fflush(stdin);
                break;

                /* Servo-Board Test Tool */
            case 48:
                printf("\n\r\n\rServo-Board Test Tool\n\rEnter Channel Number and then Duty Cycle\n\r");
                printf("for exit, enter Channel > 15\n\r");
                channel = 0;
                while (channel < 16) {
                    printf("Channel Number: ");
                    pc.scanf("%d", &channel);
                    printf("\r\nchannel: %d\n\rDuty Cycle 0..4095: ",channel);
                    pc.scanf("%d", &dutyCycle);
                    printf("\n\r%d\n\r",dutyCycle);
                    setPWM((char)channel, (int)dutyCycle);
                    fflush(stdin);
                }
                printf("leave Servo-Board Test Tool\n\r");
                state = 47;
                break;

            case 49:
                enableOutput();
                printf("Servo output enabled\r\n\n");
                state = 47;
                break;

            case 50:
                disableOutput();
                printf("Servo output disabled\r\n\n");
                state = 47;
                break;

            case 51:
                testSd();
                printf("\rSD Card test finished\r\n\n");
                state = 47;
                break;


            case 52:
                prescription[1] = readMedication(1);
                if(prescription[1].valid) {
                    printf("\rread successful\r\n\n");
                } else {
                    printf("\rread failed\r\n\n");
                }
                state = 47;
                break;

            case 53:
                if(prescription[1].valid == false) {
                    printf("\r\nNo valid data in struct, try to run case 52\r\n\n");
                } else {
                    printf("\r\nMedication display test tool:\r\n");
                    printf("Enter Day (0..6) and Moment (0..4)\r\nTo exit enter Day > 6:\r\n");
                    day = 0;
                    while (day < 7) {
                        printf("\r\nDay Number: ");
                        pc.scanf("%d", &day);
                        if(day < 7) {
                            printf("\r\nDay: %d\n\rMoment 0..3: ",day);
                            pc.scanf("%d", &moment);
                            printf("\r\nMedication for weekday %d Moment %d is ",day,moment);
                            for(int i=0; i<6; i++) {
                                printf("%d", prescription[1].medication.day[day].moment[moment].medContainer.container[i]);
                            }
                            printf("\r\nThe alarm clock is set for %02d:%02d",prescription[1].medication.day[day].moment[moment].time.hour,prescription[1].medication.day[day].moment[moment].time.minute);
                        }
                        printf("\r\n");
                        fflush(stdin);
                    }
                    printf("exit Tool\n\r");
                }
                state = 47;
                break;
            case 54:
                writeMedication(0,prescription[1]);
                state = 47;
                break;
            case 55:
                /* Demo Tool */
                enableOutput();
                printf("Servo output enabled\r\n\n");
                summer.period_us(500);
                summer.write(0.5f);
                wait(3);
                summer.write(0.0f);

                userButton1.fall(&summ);

                printf("Summer Interrupt on Button 0 enabled\n\r");

                color = 0xFFFFFF;
                colorTime = 0;
                setLED(0,color);
                wait(5);

                printf("start fading loop for DEMOTIME loops\n\r");

                while(colorTime < DEMOTIME) {
                    printf("fade1\n\r");
                    for(r = 0; r <= 255; r++) {
                        color &= 0x00FFFF;
                        color |= (r<<16);
                        setLED(0,color);
                        wait_ms(FADESPEED);
                    }
                    printf("fade2\n\r");
                    for(b = 255; b >= 0; b--) {
                        color &= 0xFFFF00;
                        color |= b;
                        setLED(0,color);
                        wait_ms(FADESPEED);
                    }
                    printf("fade3\n\r");
                    for(g = 0; g <= 255; g++) {
                        color &= 0xFF00FF;
                        color |= (g<<8);
                        setLED(0,color);
                        wait_ms(FADESPEED);
                    }
                    printf("fade4\n\r");
                    for(r = 255; r > 0; r--) {
                        color &= 0x00FFFF;
                        color |= (r<<16);
                        setLED(0,color);
                        wait_ms(FADESPEED);
                    }
                    printf("fade5\n\r");
                    for(b = 0; b <= 255; b++) {
                        color &= 0xFFFF00;
                        color |= b;
                        setLED(0,color);
                        wait_ms(FADESPEED);
                    }
                    printf("fade6\n\r");
                    for (g = 255; g > 0; g--) {
                        color &= 0xFF00FF;
                        color |= (g<<8);
                        setLED(0,color);
                        wait_ms(FADESPEED);
                    }
                    colorTime++;
                }
                printf("exit demo tool\n\r");

                break;

            case 60:
                printf("write_medProtocol\n\r");
                write_medProtocol(medicationTime, outputTime, medication, success, user);
                printf("done\n\r");
                state = 47;

                break;

            case 61:
                printf("write_medInventory\n\r");
                write_medInventory(medInventory,currentTime,med1,med2,med3,med4,med5,med6);
                printf("done\n\r");
                state = 47;

                break;

            case 62:
                printf("write_medError\n\r");
                write_medError(fail, time);
                printf("done\n\r");
                state = 47;

                break;

            default:
                state = 47; /* Go to State Switch */
                break;
        }
    }
    printf("exit while 1 loop");
}