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
Utility.h@0:cc61e9d1c295, 2014-02-18 (annotated)
- Committer:
- mgottscho
- Date:
- Tue Feb 18 22:19:16 2014 +0000
- Revision:
- 0:cc61e9d1c295
- Child:
- 1:2a3bbec22035
Globally useful utilities on the mbed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgottscho | 0:cc61e9d1c295 | 1 | /* Utility.h |
mgottscho | 0:cc61e9d1c295 | 2 | * Tested with mbed board: FRDM-KL46Z |
mgottscho | 0:cc61e9d1c295 | 3 | * Author: Mark Gottscho |
mgottscho | 0:cc61e9d1c295 | 4 | * mgottscho@ucla.edu |
mgottscho | 0:cc61e9d1c295 | 5 | */ |
mgottscho | 0:cc61e9d1c295 | 6 | |
mgottscho | 0:cc61e9d1c295 | 7 | #ifndef UTILITY_H |
mgottscho | 0:cc61e9d1c295 | 8 | #define UTILITY_H |
mgottscho | 0:cc61e9d1c295 | 9 | |
mgottscho | 0:cc61e9d1c295 | 10 | #include "mbed.h" |
mgottscho | 0:cc61e9d1c295 | 11 | #include <string> |
mgottscho | 0:cc61e9d1c295 | 12 | |
mgottscho | 0:cc61e9d1c295 | 13 | class Utility { |
mgottscho | 0:cc61e9d1c295 | 14 | public: |
mgottscho | 0:cc61e9d1c295 | 15 | Utility(PinName green_led_pin, PinName red_led_pin, PinName serial_tx_pin, PinName serial_rx_pin); |
mgottscho | 0:cc61e9d1c295 | 16 | ~Utility(); |
mgottscho | 0:cc61e9d1c295 | 17 | |
mgottscho | 0:cc61e9d1c295 | 18 | /** |
mgottscho | 0:cc61e9d1c295 | 19 | * 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. |
mgottscho | 0:cc61e9d1c295 | 20 | * This method never returns. |
mgottscho | 0:cc61e9d1c295 | 21 | * @param errorMessage the string to print |
mgottscho | 0:cc61e9d1c295 | 22 | * @param errorCode the accompanying error code to print |
mgottscho | 0:cc61e9d1c295 | 23 | */ |
mgottscho | 0:cc61e9d1c295 | 24 | void panic(string errorMessage, int errorCode); |
mgottscho | 0:cc61e9d1c295 | 25 | |
mgottscho | 0:cc61e9d1c295 | 26 | /** |
mgottscho | 0:cc61e9d1c295 | 27 | * Print a warning message to the serial console. |
mgottscho | 0:cc61e9d1c295 | 28 | * @param errorMessage the string to print |
mgottscho | 0:cc61e9d1c295 | 29 | * @param errorCode the accompanying error code to print |
mgottscho | 0:cc61e9d1c295 | 30 | */ |
mgottscho | 0:cc61e9d1c295 | 31 | void warn(string errorMessage, int errorCode); |
mgottscho | 0:cc61e9d1c295 | 32 | |
mgottscho | 0:cc61e9d1c295 | 33 | /** |
mgottscho | 0:cc61e9d1c295 | 34 | * Object-oriented assert statement. |
mgottscho | 0:cc61e9d1c295 | 35 | * @param condition if false, calls panic() with results of the assertion, and never returns. |
mgottscho | 0:cc61e9d1c295 | 36 | * @param file string representing the source file where the assertion is called |
mgottscho | 0:cc61e9d1c295 | 37 | * @param line line number of the source file where the assertion is called |
mgottscho | 0:cc61e9d1c295 | 38 | */ |
mgottscho | 0:cc61e9d1c295 | 39 | void myAssert(bool condition, const char *file, const unsigned long line); |
mgottscho | 0:cc61e9d1c295 | 40 | |
mgottscho | 0:cc61e9d1c295 | 41 | /** |
mgottscho | 0:cc61e9d1c295 | 42 | * Blink the green user LED. This is a non-blocking call. The LED will blink until it is disabled. |
mgottscho | 0:cc61e9d1c295 | 43 | * @param enable if true, enables the blinking LED |
mgottscho | 0:cc61e9d1c295 | 44 | * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle). |
mgottscho | 0:cc61e9d1c295 | 45 | * If non-positive, the LED will stay on. |
mgottscho | 0:cc61e9d1c295 | 46 | * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect. |
mgottscho | 0:cc61e9d1c295 | 47 | */ |
mgottscho | 0:cc61e9d1c295 | 48 | void blinkGreen(bool enable, float half_period); |
mgottscho | 0:cc61e9d1c295 | 49 | |
mgottscho | 0:cc61e9d1c295 | 50 | /** |
mgottscho | 0:cc61e9d1c295 | 51 | * Blink the red user LED. This is a non-blocking call. The LED will blink until it is disabled. |
mgottscho | 0:cc61e9d1c295 | 52 | * @param enable if true, enables the blinking LED |
mgottscho | 0:cc61e9d1c295 | 53 | * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle). |
mgottscho | 0:cc61e9d1c295 | 54 | * If non-positive, the LED will stay on. |
mgottscho | 0:cc61e9d1c295 | 55 | * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect. |
mgottscho | 0:cc61e9d1c295 | 56 | */ |
mgottscho | 0:cc61e9d1c295 | 57 | void blinkRed(bool enable, float half_period); |
mgottscho | 0:cc61e9d1c295 | 58 | |
mgottscho | 0:cc61e9d1c295 | 59 | Serial console; |
mgottscho | 0:cc61e9d1c295 | 60 | |
mgottscho | 0:cc61e9d1c295 | 61 | private: |
mgottscho | 0:cc61e9d1c295 | 62 | /** |
mgottscho | 0:cc61e9d1c295 | 63 | * Interrupt service routine for blinking the green user LED. |
mgottscho | 0:cc61e9d1c295 | 64 | */ |
mgottscho | 0:cc61e9d1c295 | 65 | void __greenLED_ISR(); |
mgottscho | 0:cc61e9d1c295 | 66 | |
mgottscho | 0:cc61e9d1c295 | 67 | /** |
mgottscho | 0:cc61e9d1c295 | 68 | * Interrupt service routine for blinking the red user LED. |
mgottscho | 0:cc61e9d1c295 | 69 | */ |
mgottscho | 0:cc61e9d1c295 | 70 | void __redLED_ISR(); |
mgottscho | 0:cc61e9d1c295 | 71 | |
mgottscho | 0:cc61e9d1c295 | 72 | DigitalOut __green_led; |
mgottscho | 0:cc61e9d1c295 | 73 | Ticker __green_led_interrupt; |
mgottscho | 0:cc61e9d1c295 | 74 | |
mgottscho | 0:cc61e9d1c295 | 75 | DigitalOut __red_led; |
mgottscho | 0:cc61e9d1c295 | 76 | Ticker __red_led_interrupt; |
mgottscho | 0:cc61e9d1c295 | 77 | }; |
mgottscho | 0:cc61e9d1c295 | 78 | |
mgottscho | 0:cc61e9d1c295 | 79 | #endif |