Yu-Cheng MBED

Dependencies:   SLCD TSI mbed-src

Fork of FRDM-KL46Z-Clock by T Morgan

Files at this revision

API Documentation at this revision

Comitter:
19921202
Date:
Tue Dec 16 04:24:54 2014 +0000
Parent:
2:46d52fa160b2
Commit message:
Test Code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 46d52fa160b2 -r 586be3d0341d main.cpp
--- a/main.cpp	Mon Feb 24 20:48:50 2014 +0000
+++ b/main.cpp	Tue Dec 16 04:24:54 2014 +0000
@@ -4,230 +4,166 @@
  */
 #include "mbed.h"
 #include "SLCD.h"
-#include "TSISensor.h"
-
-// Peripherals
-SLCD        sLCD;
-Serial      pc(USBTX, USBRX);
-TSISensor   tsi;
-DigitalOut  greenLED(LED1);
-DigitalOut  redLED(LED2);
-InterruptIn setModeSwitch (SW3);
 
-// FUNCTION-MODES
-// In SET_HOURS mode, wait for slider touch to set hours, or SW3 press to enter SET_MINUTES mode.
-// In SET_MINUTES mode, wait for slider touch to set minutes, or SW3 press to enter SET_SECONDS mode.
-// In SET_SECONDS mode, wait for slider touch to set seconds, or SW3 press to enter SHOW_TIME mode.
-// In SHOW_TIME mode, advance display state every seconds, or, on SW3 press, enter SET_HOURS mode.
-#define SET_HOURS   0
-#define SET_MINUTES 1
-#define SET_SECONDS 2
-#define SHOW_TIME   3
-int function_mode;
+// Peripherals/Global Objects/Hardware Initialization
+SLCD sLCD;                              //LCD Object Initialization
+Timer t;                                //Initialize Timer object for measuring time stamp. 
+Ticker light_sensor_ticker;             //Set up a timer for light sensor sampling
+Ticker adc_ticker;                      //Set up a timer to be used for adc sampling
+AnalogIn light_measurement(PTE22);      //Light Sensor Input Pin. 
+InterruptIn event_0_to_1(PTD3);         //Initialize Interrupt for 0 to 1 event
+PwmOut PwmPin (LED_RED);                   //Initialize PWM Output Pin
+AnalogOut DAC(A4);                    //Initialize Analog Sine Wave Output Pin
+Ticker Sample_Period;                   //Set up a timer to be used for sample rate interrupts
 
-// TIME-DISPLAY STATES
-// Show HH:MM from state SHOW_HHMM to state SHOW_SS,
-// Show   :SS from state SHOW_SS to state SHOW_LOOP
-#define SHOW_HHMM   0
-#define SHOW_SS     5
-#define SHOW_LOOP   10
-int display_state;
-
+//Need these global counter 
+volatile int i = 0;
+float Analog_out_data[128];
 char LCDbuffer[32];
 
-// Scroll instructions across the LCD
-void scroll_message(void)
+// Read Real-time Clock MM:SS, for testing. 
+void get_HHMMSS(int *minutes, int *seconds)
 {
-    char message[60] = "    Press SW3 and touch slider to set time    ";
-    
-    for (int start = 0; start < strlen(message) - 4; start++)
-    {
-        for (int digit = 0; digit < 4; digit++)
-            sLCD.putc(message[start + digit]);
-        wait(0.25);
-    }
+    time_t rtcTime = time(NULL);
+    struct tm *t = localtime(&rtcTime);
+    *minutes = t->tm_min;
+    *seconds = t->tm_sec;
 }
 
-// Set Real-time Clock HH:MM:SS
-void set_HHMMSS(int hours, int minutes, int seconds)
+// Set Real-time Clock HH:MM:SS, HH is set to 0. 
+void set_MMSS(int minutes, int seconds)
 {
     struct tm t;
-    
-    if(hours > 23) hours = 0;
     if(minutes > 59) minutes = 0;
     t.tm_sec = seconds;
     t.tm_min = minutes;
-    t.tm_hour = hours;
+    t.tm_hour = 0;
     t.tm_mday = 23;
     t.tm_mon = 2;
     t.tm_year = 114;
     set_time (mktime(&t));
 }
 
-// Read Real-time Clock HH:MM:SS
-void get_HHMMSS(int *hours, int *minutes, int *seconds)
+//Show time
+void showTime(void)
 {
     time_t rtcTime = time(NULL);
-    struct tm *t = localtime(&rtcTime);
-    *hours = t->tm_hour;
-    *minutes = t->tm_min;
-    *seconds = t->tm_sec;
+    strftime(LCDbuffer, 4, "%M%S", localtime(&rtcTime));// display HH:MM 
+    sLCD.printf(LCDbuffer);     // Send to LCD
 }
 
-// On SW3 press, cycle between function-modes
-void setMode(void)
+//Interrupt on pin PTD3, when it goes from 0 to 1
+void trigger(void)
 {
-    if (function_mode == SET_HOURS)
-        function_mode = SET_MINUTES;
-    else if (function_mode == SET_MINUTES)
-        function_mode = SET_SECONDS;
-    else if (function_mode == SET_SECONDS)
-        function_mode = SHOW_TIME;
-    else if (function_mode == SHOW_TIME)
-        function_mode = SET_HOURS;
-}
-
-// Wait for slider to be pressed (or mode change)
-void waitForTouch(int mode)
-{
-    redLED = 0;
-    while(tsi.readDistance() == 0 && function_mode == mode)
-    {
-        wait(0.2);
-        redLED = !redLED;
-    }
-    redLED = 1;
+    t.stop();
+    //Send data back to pi, not implemented yet. 
+    printf("Rising Event: The time taken was %f seconds\n", t.read());
+    t.start();
 }
 
-// Wait for slider to be released and return slider value between 0 and <scale>
-int waitForRelease(char *label, int scale)
+//
+void light_sensor()
 {
-    float pct;
-    int return_value = 0;
-    
-    greenLED = 0;
-    while((pct = tsi.readPercentage()) != 0)
-    {
-        // In practice, readPercentage returns a number in the range 0.06..0.99
-        // Stretch it to 0.00..0.99 before applying scale.
-        pct = (pct - 0.09)/ 0.90;
-        if (pct < 0.0) pct = 0.0;
-        return_value = scale * pct;
-        sLCD.printf("%2s%2i", label, return_value);
-        wait(0.2);
-        greenLED = !greenLED;
-    }
-    greenLED = 1;
-    return return_value;
+    t.stop();
+    //Send light-sensor data, and time stamp back to pi, not implemented yet. 
+    printf("Light Sensor Time: The time taken was %f seconds\n", t.read());
+    printf("Light Sensor Measurement: %f\n", light_measurement.read()); //Darkness is 1, Light is 0. 
+    t.start();
+}
+//Measure ADC
+void adc_measurement()
+{
+     t.stop();
+    //Send ADC measurement data, and time stamp back to pi, not implemented yet.
+    printf("ADC Time: The time taken was %f seconds\n", t.read());
+    printf("ADC Measurement: %f\n", PTB0); //A0
+    t.start();
 }
 
-// If slider is touched, update hours
-void setHours(void)
+//Rate is in Hz
+void adc_rate(int rate, bool start)
 {
-    int hours, minutes, seconds;
-
-    get_HHMMSS(&hours, &minutes, &seconds);
-    sLCD.printf("HH%2i", hours);
-    waitForTouch(SET_HOURS);
-    if (function_mode == SET_HOURS)
-    {
-        hours = waitForRelease("HH",24);
-        pc.printf("Setting hours to %i\r\n", hours);
-        set_HHMMSS(hours, minutes, seconds);
-        function_mode = SHOW_TIME;
-        display_state = SHOW_HHMM;
+    //Start the ticker with the given rate. 
+    if(start){
+        adc_ticker.attach(&adc_measurement, (1/rate));
+    }else{
+        //turn off the ticker
+        adc_ticker.detach();    
     }
 }
 
-// If slider is touched, update minutes
-void setMinutes(void)
-{
-    int hours, minutes, seconds;
-    
-    get_HHMMSS(&hours, &minutes, &seconds);
-    sLCD.printf("MM%2i", minutes);
-    waitForTouch(SET_MINUTES);
-    if (function_mode == SET_MINUTES)
-    {
-        minutes = waitForRelease("MM",60);
-        pc.printf("Setting minutes to %i\r\n", minutes);
-        set_HHMMSS(hours, minutes, seconds);
-        function_mode = SHOW_TIME;
-        display_state = SHOW_HHMM;
-    }
-}
-
-// If slider is touched, update seconds
-void setSeconds(void)
-{
-    int hours, minutes, seconds;
-    
-    get_HHMMSS(&hours, &minutes, &seconds);
-    sLCD.printf("SS%2i", seconds);
-    waitForTouch(SET_SECONDS);
-    if (function_mode == SET_SECONDS)
-    {
-        seconds = waitForRelease("SS",60);
-        pc.printf("Setting seconds to %i\r\n", seconds);
-        set_HHMMSS(hours, minutes, seconds);
-        function_mode = SHOW_TIME;
-        display_state = SHOW_SS;
+//True for driving pin to high, false for driving pin to low 
+void set_pin(bool HIGH){
+    //To turn an LED on, you give it 0, To turn an LED off, you give it ~0. 
+    DigitalOut pin(PTA12);
+    DigitalOut led_red(LED_RED);
+    if (HIGH){
+        pin = ~pin;
+        led_red = ~led_red;
+    }else{
+        pin = 0;   
+        led_red = 0;
     }
 }
 
-// Cycle between time-display states
-// In states SHOW_HHMM to SHOW_SS - 1, display HH:MM & flash green LED.
-// In states SHOW_SS to SHOW_LOOP, display :SS & flash red LED.
-void showTime(void)
+// Interrupt routine
+// used to output next analog sample whenever a timer interrupt occurs
+void Sample_timer_interrupt(void)
 {
-    DigitalOut *flashLED;
-    time_t rtcTime = time(NULL);
-    
-    if(display_state < SHOW_SS)
-    {
-        strftime(LCDbuffer, 4, "%H%M", localtime(&rtcTime));// display HH:MM 
-        flashLED = &greenLED;                               // while flashing green LED
+    // send next analog sample out to D to A
+    DAC = Analog_out_data[i];
+    // increment pointer and wrap around back to 0 at 128
+    i = (i+1) & 0x07F;
+}
+
+//Rate is in Hz
+void generate_sine_wave(int rate){
+
+    // precompute 128 sample points on one sine wave cycle 
+    // used for continuous sine wave output later
+    for(int k=0; k<128; k++) {
+        Analog_out_data[k]=((1.0 + sin((float(k)/128.0*6.28318530717959)))/2.0);
+        // scale the sine wave from 0.0 to 1.0 - as needed for AnalogOut arg 
     }
-    else
-    {
-        strftime(LCDbuffer, 4, "  %S", localtime(&rtcTime));// display :SS           
-        flashLED = &redLED;                                 // while flashing red LED
-    }
-    sLCD.printf(LCDbuffer);     // Send to LCD
-    redLED = 1; greenLED = 1;   // Both LEDs off
-    wait(0.5);
-    *flashLED = 0;              // Red or Green on.
-    wait(0.5);
-    redLED = 1; greenLED = 1;   // Both LEDs off.
-    if (function_mode == SHOW_TIME) display_state++;        // Increment display counter if no switch pressed
-    if (display_state > SHOW_LOOP) display_state = SHOW_HHMM;
+    // turn on timer interrupts to start sine wave output
+    // sample rate is 500Hz with 128 samples per cycle on sine wave
+    Sample_Period.attach(&Sample_timer_interrupt, 1.0/(rate*128));
 }
 
+//Disable sine wave
+void disable_sine_wave(){
+    Sample_Period.detach();   
+}    
+
 main()
 {
-    pc.printf("\r\n\nFRDM-KL46Z Clock\r\n");
-    
-    sLCD.All_Segments(1); wait(1);  // Flash LCD segments
-    sLCD.All_Segments(0); wait(1);    
+    //Set clock time
+    int MM = 9;
+    int SS = 19;
+    set_MMSS(MM,SS);
     
-    greenLED = 1; redLED = 1;       // Both LEDs off
+    //Set Pulse
+    PwmPin.period(1);  // Set frequency to 1 Hz (period = 5 us)
     
-    scroll_message();               // Display instructions
+    PwmPin.pulsewidth(0.5);       // Set the duty cycle to 10%, 100ms, might need some delay for getting the rising edge to be on 1s
     
-    setModeSwitch.rise(setMode);    // Enable setMode interrupt handler
+    //Asynchronous 0-1 event interrupt. 
+    event_0_to_1.rise(&trigger);
     
-    sLCD.Colon(1); sLCD.DP2(0);
-    function_mode = SHOW_TIME;
-    while(1)
+    //Light Sensor
+    light_sensor_ticker.attach(&light_sensor, 1); // setup ticker to call flip every 1 seconds
+    
+    //Start the clock for time stamp. 
+    t.start();
+    
+    generate_sine_wave(1);
+    while(true)
     {
-        if (function_mode == SET_HOURS)
-            setHours();
-        else if (function_mode == SET_MINUTES)
-            setMinutes();
-        else if (function_mode == SET_SECONDS)
-            setSeconds();
-        else if (function_mode == SHOW_TIME)
-            showTime();
+        showTime(); //Might want to use a ticker on this function instead of while loop, to get rid of unneeded writes to LCD. 
+        get_HHMMSS(&MM, &SS);
+        //printf("MM: %d\n", MM);
+        //printf("SS: %d\n", SS);
+        wait(1);
     }
 }