ECE3872 HW/SW Project Code

Dependencies:   mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1

main.cpp

Committer:
nnguyen99
Date:
2020-04-07
Revision:
7:f5f265b139fe
Parent:
5:d34d14d33a89

File content as of revision 7:f5f265b139fe:

#include "mbed.h"
#include <iostream>
#include "audio_out.h"
#include "PinDetect.h"
#include "uLCD_4DGL.h"
DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4);
// LCD init
uLCD_4DGL guLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
// Push buttons init
PinDetect pb1(p16); 
PinDetect pb2(p17);  
PinDetect pb3(p18); 
PinDetect pb4(p19); 
// States 

//  State Machine control global variables
bool X;     //reset
bool P;     //play
bool S;     //stop
bool R;     //record
bool E;     //erase

enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE};


void reset(){ 
    /* reset state:
        Initial state upon powering up the device
        1. Cease all motion
        2. Reset all motors to initial position
        3. Mutes all audio
        4. Goes to erase state if reset button is held down for at least 3 seconds
        5. Goes to stop state according to rotary switch input
        6. LED goes from green to red
        NOTE: ONLY exits to stop or erase state 
    */
}
void stop(){
    /* stop state:
        Initiated by rotary switch
        1. Cease all motion
        2. Stop recording
        3. Mute all audio
    */
}
void record(){
    /* record state:
        Initiated by rotary switch
        1. Cease all motion
        2. Begin recording ToF inputs (distance and time)
        3. Convert distances to corresponding audio frequencies
        4. Append to list of frequencies
        5. 
    */
}
void play(){
    /* play state:
        Initiated by rotary switch
        1. wait a few seconds
        2. begin reading list of frequencies while concurrently:
            - moving servo motors accordingly
            - playing corresponding sounds
    */
}
void erase(){
    /* erase state:
        erases entire audio recording
    */
}


void pb1_hit_callback (void) 
{ 
    guLCD.printf("REEST");
    X = 1;
}

void pb2_hit_callback (void)
{
    guLCD.printf("STOP");
    P = 1;
}

void pb3_hit_callback (void) 
{
    guLCD.printf("RECORD");
    S = 1;
}

void pb4_hit_callback (void) 
{
    guLCD.printf("PLAY");
    R = 1;
}

void hardware_init(){ 
    // Push buttons init
    pb1.mode(PullUp); 
    pb2.mode(PullUp); 
    pb3.mode(PullUp);
    pb4.mode(PullUp);
    // Delay for initial pullup to take effect
    wait(.01);
    pb1.attach_deasserted(&pb1_hit_callback);
    pb2.attach_deasserted(&pb2_hit_callback); 
    pb3.attach_deasserted(&pb3_hit_callback);
    pb4.attach_deasserted(&pb4_hit_callback);
    
    guLCD.display_control(LANDSCAPE);
    guLCD.background_color(BLACK);
    guLCD.cls();
    guLCD.baudrate(BAUD_1000000); //jack up baud rate to max for fast display
    wait(1.0);
    pb1.setSampleFrequency(); 
    pb2.setSampleFrequency(); 
    pb3.setSampleFrequency();
    pb4.setSampleFrequency();
}
void state_machine_mgr(){
    sm_state curr_state =  sRESET;
    X = !X;
    while(1) {
        switch(curr_state){
            case sRESET:
                S = 0;
                R = 0;
                E = 0;
                P = 0;
                if(S) curr_state = sSTOP;
                break;
            case sSTOP:
                X = 0;
                R = 0;
                E = 0;
                P = 0;
                if(X){
                    curr_state = sRESET;
                }else if(S&R){
                    curr_state = sRECORD;
                }else if(S&P){
                    curr_state = sPLAY;
                }
                break;
            case sRECORD:
                S = 0;
                X = 0;
                E = 0;
                P = 0;
                if(X){
                    curr_state = sRESET;
                }else if(R&S){
                    curr_state = sSTOP;
                }else if(R&P){
                    curr_state = sPLAY;
                }
                break;
            case sPLAY:
                S = 0;
                R = 0;
                E = 0;
                X = 0;
                if(X){
                    curr_state = sRESET;
                }else if(P&S){
                    curr_state = sSTOP;
                }else if(P&R){
                    curr_state = sPLAY;
                }
                break;
            case sERASE:
                if(X){
                    curr_state = sRESET;
                }
        }
    }
}

int main() {
    hardware_init();
    state_machine_mgr();
}