backup

Dependencies:   MODSERIAL Motordriver QEI Servo mbed HIDScope

Fork of The_Claw_Back-up_Buttons by Meike Froklage

main.cpp

Committer:
meikefrok
Date:
2016-10-19
Revision:
2:ad4b181a6422
Parent:
1:1d2208dce484
Child:
3:3a671d01bcb8

File content as of revision 2:ad4b181a6422:

#include "mbed.h"

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
// Serial communication using MODSERIAL
#define SERIAL_BAUD 115200  // baud rate for serial communication
#include "MODSERIAL.h"
MODSERIAL pc(USBTX,USBRX);

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
// Timing
const float kTimeLedToggle = 0.25f; // period with which to toggle LED
const float kTimePrintSerial = 1.0f;// period with which data is printed

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
// constants
const int kLedOn = 0;               // LED on if 0

// LEDs
DigitalOut led_r(LED_RED);
DigitalOut led_g(LED_GREEN);
DigitalOut led_b(LED_BLUE);

// ID of led that should blink; *volatile* because changed by interrupt
volatile int part_id = 0;

// counters
int num_turned_on_0 = 0;            // count number of times red LED turned on
int num_turned_on_1 = 0;            // count number of times green LED turned on
int num_turned_on_2 = 0;            // count number of times blue LED turned on



/**
 * Toggle / Switch / Blink the state of the LED.
 * Count the number of times the LED was turned ON.
 * We have quite some duplicate code here; this is not ideal and could be solved
 * using more 'advanced' concepts such as arrays and enums. 
 * However, this is outside the scope of this course for now.
 */
void ToggleLed()
{
    switch (part_id) {
        case 0: {
            // LED 0 to toggle and count:
            led_r = kLedOn;
            if (led_r == kLedOn) {
                num_turned_on_0++;
            }
            
            // LEDs to turn off:
            led_g = not kLedOn;
            led_b = not kLedOn;
            
            break;
        }
        case 1: {
            // LED 1 to toggle and count:
            led_g = kLedOn;
            if (led_g == kLedOn) {
                num_turned_on_1++;
            }
            
            // LEDs to turn off:
            led_r = not kLedOn;
            led_b = not kLedOn;
            
            break;
        }
        case 2: {
            // LED 2 to toggle and count:
            led_b = kLedOn;
            if (led_b == kLedOn) {
                num_turned_on_2++;
            }
            
            // LEDs to turn off:
            led_r = not kLedOn;
            led_g = not kLedOn;
            
            break;
        }
    }
}

/**
 * Print the number of times each LED was turned on through serial communication
 */
void PrintSerial()
{
    pc.printf("*Status*\r\n\tn_r = %d\r\n\tn_g = %d\r\n\tn_b = %d\r\n\r\n",
        num_turned_on_0, num_turned_on_1, num_turned_on_2);
}

/**
 * Switch the led id that blinks 
 * led_blink_id goes from 0 -> 1 -> 2 -> 0 -> ...
 * @ensure led_blink_id = ++led_blink_id % kNumStates
 */
void SetValue2() {
    part_id = 0;
   }

void SetValue3() {
    part_id = 1;
   }


void SetValue4() {
    part_id = 2;
}


/**
 * Main loop.
 */
int main()
{
    // Serial comm baud rate
    pc.baud(SERIAL_BAUD);
    pc.printf("\r\n**RESET**\r\n");
    
    // Turn off all LEDs initially
    led_r = not kLedOn;
    led_g = not kLedOn;
    led_b = not kLedOn;
    
    // Create ticker and attach LED toggle function
    Ticker tick_toggle_led;
    tick_toggle_led.attach(&ToggleLed,kTimeLedToggle);
    
    // Create ticker and attach Print function
    Ticker tick_print_serial;
    tick_print_serial.attach(&PrintSerial,kTimePrintSerial);
    
    // Create interrupt and attach switch function
    InterruptIn btn_base(D2);
    btn_base.fall(&SetValue2);
    
    InterruptIn btn_cart(D3);
    btn_cart.fall(&SetValue3);

    InterruptIn btn_arm(D4);
    btn_arm.fall(&SetValue4);

    while (true);
}