Mark Gottscho / UtilityLib

Fork of UtilityLib by Mark Gottscho

Utility.h

Committer:
mgottscho
Date:
2014-02-18
Revision:
0:cc61e9d1c295
Child:
1:2a3bbec22035

File content as of revision 0:cc61e9d1c295:

/* Utility.h
 * Tested with mbed board: FRDM-KL46Z
 * Author: Mark Gottscho
 * mgottscho@ucla.edu
 */
 
#ifndef UTILITY_H
#define UTILITY_H
 
#include "mbed.h"
#include <string>

class Utility {
    public:
        Utility(PinName green_led_pin, PinName red_led_pin, PinName serial_tx_pin, PinName serial_rx_pin);
        ~Utility();
        
        /**
         * Infinitely loop in a panic condition, disabling the green user LED, enabling the red user LED and printing an error message to the serial console.
         * This method never returns.
         * @param errorMessage the string to print
         * @param errorCode the accompanying error code to print
         */
        void panic(string errorMessage, int errorCode);
        
        /**
         * Print a warning message to the serial console.
         * @param errorMessage the string to print
         * @param errorCode the accompanying error code to print
         */
        void warn(string errorMessage, int errorCode);
        
        /**
         * Object-oriented assert statement.
         * @param condition if false, calls panic() with results of the assertion, and never returns.
         * @param file string representing the source file where the assertion is called
         * @param line line number of the source file where the assertion is called
         */
        void myAssert(bool condition, const char *file, const unsigned long line);
        
        /**
         * Blink the green user LED. This is a non-blocking call. The LED will blink until it is disabled.
         * @param enable if true, enables the blinking LED
         * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle).
         * If non-positive, the LED will stay on. 
         * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect.
         */
        void blinkGreen(bool enable, float half_period);
        
        /**
         * Blink the red user LED. This is a non-blocking call. The LED will blink until it is disabled.
         * @param enable if true, enables the blinking LED
         * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle).
         * If non-positive, the LED will stay on. 
         * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect.
         */
        void blinkRed(bool enable, float half_period);
        
        Serial console;
                
    private:
        /**
         * Interrupt service routine for blinking the green user LED.
         */
        void __greenLED_ISR();
        
        /**
         * Interrupt service routine for blinking the red user LED.
         */
        void __redLED_ISR();
    
        DigitalOut __green_led;
        Ticker __green_led_interrupt;
        
        DigitalOut __red_led;
        Ticker __red_led_interrupt;
};

#endif