Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of UtilityLib by
Diff: Utility.h
- Revision:
- 9:f71dc1b426da
- Parent:
- 8:e79637fbb035
--- a/Utility.h Wed Mar 12 19:47:18 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -/* 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 pointer to a DigitalOut for green LED blinking - * @param red_led pointer to a DigitalOut for green LED blinking - * @param serial_tx_pin pin for the UART TX - * @param serial_rx_pin pin for the UART RX - * @param enableSerialInterrupts if true, allows interrupt handlers on the serial port - */ - Utility(DigitalOut *green_led, DigitalOut *red_led, PinName serial_tx_pin, PinName serial_rx_pin, int baudrate, bool enableSerialInterrupts); - - /** - * Utility destructor. - */ - ~Utility(); - - /** - * Attaches a user-specified callback function that is called each time serial data is received. - * @param fptr the user callback function - */ - void attach(void (*fptr)(void)); - - /** - * Attaches a user-specified callback member function that is called each time serial data is received. - * @param tptr the object - * @param mptr method to call on the object - */ - template<typename T> void attach(T *tptr, void (T::*mptr)(void)); - - /** - * Detaches the user-specified callback function, if any. - */ - void detach(); - - /** - * 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(); - - /** - * Flushes the console serial RX buffer. - */ - void flushConsole(); - - 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(); - - inline void __disable_uart_irq(); - inline void __enable_uart_irq(); - - DigitalOut *__green_led; - DigitalOut *__red_led; - - bool __valid_green; - bool __valid_red; - - bool __interrupts_en; - FunctionPointer *__user_fptr; //User callback function that is invoked in __console_rx_ISR() - - Ticker __green_led_interrupt; - 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 \ No newline at end of file