Updated OCE 360 Assignment 4

Dependencies:   4DGL-uLCD-SE MMA8452Q mbed physics_ball

main.cpp

Committer:
Kaitlyn_Barros
Date:
2017-11-17
Revision:
2:8085bd89612b
Parent:
0:2067ba6c5e74
Child:
4:241bf0f0e626

File content as of revision 2:8085bd89612b:

//Kaitlyn Barros
//OCE 360 Fall 2017 

//Last Revised: Nov 16 2017 

//Software Exercise 4:
//1. Update the software system by creating a new library, which includes 
//   a class that defines the behavior of a bouncing ball on the LCD screen.
//2. Demonstrate the system with two or more bouncing balls on the screen.
//3. Show that the system resets the ball locations when you hold the 
//   accelerometer upside down. 

//Software Exercise 5:
//1. Update the ball postion using the accelerometer's data at 50Hz. Update the LCD display w/ ball postions at 11HZ. 
//2. Use a buttom interrupt to turn data recording on/off. Use an onboard LED to indicate recording. 

#include "mbed.h"          //Mbed Header
#include "physics_ball.h"  // Physics Ball Behavior Header
#include "uLCD_4DGL.h"     // LCD Header
#include "MMA8452Q.h"      // Accelerometer Header
#include "SDFileSystem.h"  // SD Card Header

AnalogIn ain(p15);                     // Analog Input (pin 15) For Temp Sensor 

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

SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card (SPI pins)

DigitalOut led1(LED1);                 //Onboard LED 1
DigitalOut led2(LED2);                 //Onboard LED 2


DigitalIn  switchinput(p12);            //Digital switch set in Pin 12

// Call out classes (For Ball 1 & 2)
physics_ball ball1;
physics_ball ball2; 
    
// Graphic LCD - TX, RX, and RES pins
uLCD_4DGL uLCD(p9,p10,p11);

// Accelerometer - SDA, SCL, and I2C address
MMA8452Q accel(p28, p27, 0x1D);


// Call Out Variables
int status = 0;    //"Logging" Status, i.e. 1 = Logging 0 - Not Logging
float voltage_in;  //Temp Sensor 
float degrees_c;   //Temp Sensor 
int i;             //Temp Sensor 
int c;             //Temp Sensor 

// Timestamp Timer 
Timer timer;

// Call out Tickers 
Ticker BallUpdate;
Ticker LCDUpdate;
Ticker Record;

//Ticker Function 1: Ball_Update_Pos
void BUP(){
    //Update Ball Postion
     ball1.update(accel);
     ball2.update(accel);
    }

//Ticker Function 2: LCD_Update_Pos
void LCDUP(){
     // Draw new circle 
        uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLUE); 
        uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, RED); 
        led1 = 1;
    // Erase old circle (For Ball 1 & 2)
        uLCD.filled_circle(ball1.old_posx, ball1.old_posy, ball1.radius, BLACK);
        uLCD.filled_circle(ball2.old_posx, ball2.old_posy, ball2.radius, BLACK);
        led1= 0;

        }

// Ticker Function 3: Record 
void REC (){
    
//If the switch is pressed and the logging status is not logging, set the status to logging and turn the indicating LED on
     if ((switchinput == 1) && (status == 0)) {
        status = 1;
        led2 = 1;
        }
        
//If the switch is pressed and the logging status is logging, set the status to not logging and turn the indicating LED off    
     if ((switchinput == 1) && (status == 1)) {
        status = 0;
        led2 = 0;
        }
   
    if (status == 1){     
    FILE *file;  //file descriptor

    // Start our timer
    timer.start();

    // Open file for writing, if there is not a file with such a name, create one
    file = fopen("/sd/BallData.txt", "w"); 
    if ( file == NULL ) {
        error("ERROR: Could not open file for writing!\n\r");
    }

    // Tell the user we need to wait while we collect some data
    pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r");

    // Collect Temperature
        voltage_in = ain * 3.3;
        degrees_c = (voltage_in - 0.5) * 100.0;
        fprintf(file, "%2.2fs: %3.1f deg C\n\r", timer.read(), degrees_c);        
    
    // Collect Ball Data

    
    if (status == 0){
    // Close file and re-open it for reading
    fclose(file);
    file = fopen("/sd/BallData.txt", "r");
    if ( file == NULL ) {
        error("ERROR: Could not open file for reading!\n\r");
    }

    // Print results to console
    pc.printf("Temperature data:\n\r");
    while(1) {
       c = fgetc(file);
       if ( c == EOF ) {
           break;
       }
       pc.putc(c);
    }

    // Close the file and finish
    fclose(file);
    pc.printf("Done! Safe to remove SD card\n\r");
    }
        }
            }

int main() {
   // pc.printf("I Print");     //Test: Make sure I can print to PC
   // led1 = 1;                 //Test: Make sure led can turn on
     
    // Initialize uLCD
    uLCD.baudrate(115200);
    uLCD.background_color(BLACK);
    uLCD.cls();

    // Initialize accelerometer
    accel.init();
     
   //Intial Ball Conditions & Reset Conditiosn
    ball1.define_space(ball1.width, ball1.height);
    ball2.define_space(ball2.width, ball2.height);
    
    ball1.set_param(ball1.radius, BLUE);
    ball2.set_param(ball2.radius, RED);    
    
    //Tickers
    Record.attach(&REC, 0.1);
    BallUpdate.attach(&BUP, 0.02); //50 Hz
    LCDUpdate.attach(&LCDUP, 0.0909);//11 Hz 

    while (1) { }
      }