Mark Gottscho / UtilityLib

Fork of UtilityLib by Mark Gottscho

Utility.h

Committer:
mgottscho
Date:
2014-02-19
Revision:
1:2a3bbec22035
Parent:
0:cc61e9d1c295
Child:
2:3e09d74e5cf0

File content as of revision 1:2a3bbec22035:

/* 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:
        /**
         * Constructs a Utility object, which manages LEDs, serial communication, and other helper methods.
         * @param green_led_pin pin for green user LED
         * @param red_led_pin pin for red user LED
         * @param serial_tx_pin pin for the UART TX
         * @param serial_rx_pin pin for the UART RX
         */
        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);
        
        /**
         * Receives a line of data from the serial console, terminated by a carriage return character.
         * @param line a pointer to the buffer in which to store the incoming data
         * @param len the maximum number of bytes to receive
         * @returns number of bytes received
         */
        uint32_t receiveLine(char *line, const uint32_t len);
        
        /**
         * Sends a line of data to the serial port.
         * @param line a pointer to the beginning of the data to send
         * @param len the number of bytes to send
         * @returns number of bytes sent
         */
        //uint32_t sendLine(const char *line, const uint32_t len);
        
        /**
         * @returns true if there is data received from serial port ready to use.
         */
        bool haveRxSerialData();
        
        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();
        
        /**
         * Interrupt service routine for serial RX
         */
        void __console_rx_ISR();
        
        
        /**
         * Interrupt service routine for serial TX
         */
        //void __console_tx_ISR();
        
    
        DigitalOut __green_led;
        Ticker __green_led_interrupt;
        
        DigitalOut __red_led;
        Ticker __red_led_interrupt;
        
        //Buffers for working with the serial console
        const static uint32_t BUFFER_SIZE = 512; //For serial buffer
        volatile uint8_t __rx_buf[BUFFER_SIZE];
        volatile uint8_t __tx_buf[BUFFER_SIZE];
        volatile uint32_t __rx_head; //Head always points to the first item to read (oldest)
        volatile uint32_t __rx_tail; //Tail always points to the last item written (newest)
        volatile uint32_t __tx_head;
        volatile uint32_t __tx_tail;
        volatile bool __have_rx_serial; //Flag for the RX data
};

#endif