David Li / Mbed 2 deprecated final_projec_4180

Dependencies:   mbed 4DGL-uLCD-SE

main.cpp

Committer:
datyvk
Date:
2019-12-09
Revision:
1:fc3efaef8625
Parent:
0:ce3fa8a3840c

File content as of revision 1:fc3efaef8625:

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "Speaker.h"
#include "SongPlayer.h"

AnalogIn light1(p19);
AnalogIn light2(p18);
DigitalIn button(p8);
DigitalOut led(p26);
//PwmOut speaker(p25);
SongPlayer spearker(p25);

DigitalOut mag2(p20);
DigitalOut mag1(p21);
Serial blue(p28,p27);
BusOut myled(LED1,LED2,LED3,LED4);
uLCD_4DGL uLCD(p9,p10,p11);
Serial pc(USBTX, USBRX);
typedef struct Locker {
    int used;
    int code;
} Locker;

struct Locker l1 = {0, 1234};
struct Locker l2 = {0, 4321};
void wait_loop() {
    int j = 0;
    for (int i = 0; i < 50000; i++) {
     j++;
    }
}
void fill() {
    uLCD.filled_rectangle(0, 0, 128, 128, 0x000000);
    uLCD.locate(0,0);
}
int read_code() {
    char bnum=0;
    int ans = 0;
    int len = 0;
    uLCD.printf("\nEnter code\n");
    while(len < 8) {
        if (blue.getc()=='!') {
            if (blue.getc()=='B') { //button data packet
                bnum = blue.getc(); //button number
                myled = bnum - '0';
                len++;
                if (len % 2 == 1) {
                    ans = ans * 10 + (bnum - '0');
                    uLCD.printf("%d", bnum - '0');
                }
            }
        }
    }
    fill();
    wait(1);
    return ans;
}

int read_which_locker() {
    char bnum=0;
    int ans = 0;
    int len = 0;
    uLCD.printf("\nEnter Locker");
    while(len < 2) {
        if (blue.getc()=='!') {
            if (blue.getc()=='B') { //button data packet
                bnum = blue.getc(); //button number
                ans = bnum - '0';
                fill();
                uLCD.printf("%d",ans);
                len++;
            }
        }
    }
    fill();
    wait(1);
    return ans;
}

int rent_or_open() {
    char bnum=0;
    int ans = 0;
    int len = 0;
    uLCD.printf("\n1.Rent a locker\n2.Unlock an locker");
    while(len < 2) {
        if (blue.getc()=='!') {
            if (blue.getc()=='B') { //button data packet
                bnum = blue.getc(); //button number
                ans = bnum - '0';
                uLCD.printf("%d",ans);
                len++;
            }
        }
    }
    fill();
    wait(1);
    return ans;
}

void open_used() {    
    int locker = read_which_locker();
    wait_loop();
    
    if (locker == 1 && l1.used == 0) {
        uLCD.printf("\nThe locker is empty");
        wait(1);
        return;
    }
    
    if (locker == 2 && l2.used == 0) {
        uLCD.printf("\nThe locker is empty");
        wait(1);
        return;
    }
    int code = read_code();
    fill();    
    if (locker == 1 && l1.code == code) {
        uLCD.printf("\nLocker 1 is now open");
        mag1 = 1;
        int lock = 0;
        while(light1.read() > .05 || lock == 0) {
            lock = lock | button.read();
        }
        mag1 = 0;
        l1.used = 0;
    } else if (locker == 2 && l2.code == code) {
        uLCD.printf("\nLocker 2 is now open");
        mag2 = 1;
        int lock = 0;
        while(light2.read() > .01 || lock == 0) {
            lock = lock | button.read();
        }
        mag2 = 0;
        l2.used = 0;
    } else {
        uLCD.printf("\nInvalid locker and code combination");
        float note[18]= {1568.0};
        float duration[18]= {0.5};
        led = 1;
        spearker.PlaySong(note, duration);
//        speaker = .5;
        wait(.5);
        led = 0;
//        speaker = 0;
        wait(.5);
        led = 1;
        
        spearker.PlaySong(note, duration);
//        speaker = .5;
        wait(.5);
        led = 0;
//        speaker = 0;
        wait(.5);
        
    }    
}

void rent_unused(){
    if (l1.used == 0) {
        uLCD.printf("\nPlease place item in locker 1, create a code, and close the door");
        mag1 = 1;    
        int code = read_code();
        l1.code = code;
        l1.used = 1;
        mag1 = 0;
        
    } else if (l2.used == 0) {
        uLCD.printf("\nPlease place item in locker 2, create a code, and close the door");
        mag2 = 1;    
        int code = read_code();
        l2.code = code;
        l2.used = 1;
        mag2 = 0;
        
    } else {
        uLCD.printf("\nSorry all lockers are taken");
        wait(1);
    }
}

int main() {
    mag1 = 0;
    mag2 = 0;
//    speaker.period(1.0/20000.0);
    while(1){
        uLCD.locate(0,0);
        uLCD.printf("Welcome to\n our locker"); //Default Green on black text
        int option = rent_or_open();
        if (option == 1) {
            rent_unused();
        } else if (option == 2) {
            open_used();
        } else {
            uLCD.printf("\nPlease enter valid option");
        }
        uLCD.reset();
        
    }
}