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@1:2a3bbec22035, 2014-02-19 (annotated)
- Committer:
- mgottscho
- Date:
- Wed Feb 19 04:04:10 2014 +0000
- Revision:
- 1:2a3bbec22035
- Parent:
- 0:cc61e9d1c295
- Child:
- 2:3e09d74e5cf0
Some enhancements and tweaks
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 | 1:2a3bbec22035 | 15 | /** |
mgottscho | 1:2a3bbec22035 | 16 | * Constructs a Utility object, which manages LEDs, serial communication, and other helper methods. |
mgottscho | 1:2a3bbec22035 | 17 | * @param green_led_pin pin for green user LED |
mgottscho | 1:2a3bbec22035 | 18 | * @param red_led_pin pin for red user LED |
mgottscho | 1:2a3bbec22035 | 19 | * @param serial_tx_pin pin for the UART TX |
mgottscho | 1:2a3bbec22035 | 20 | * @param serial_rx_pin pin for the UART RX |
mgottscho | 1:2a3bbec22035 | 21 | */ |
mgottscho | 0:cc61e9d1c295 | 22 | Utility(PinName green_led_pin, PinName red_led_pin, PinName serial_tx_pin, PinName serial_rx_pin); |
mgottscho | 0:cc61e9d1c295 | 23 | ~Utility(); |
mgottscho | 0:cc61e9d1c295 | 24 | |
mgottscho | 0:cc61e9d1c295 | 25 | /** |
mgottscho | 0:cc61e9d1c295 | 26 | * 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 | 27 | * This method never returns. |
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 panic(string errorMessage, int errorCode); |
mgottscho | 0:cc61e9d1c295 | 32 | |
mgottscho | 0:cc61e9d1c295 | 33 | /** |
mgottscho | 0:cc61e9d1c295 | 34 | * Print a warning message to the serial console. |
mgottscho | 0:cc61e9d1c295 | 35 | * @param errorMessage the string to print |
mgottscho | 0:cc61e9d1c295 | 36 | * @param errorCode the accompanying error code to print |
mgottscho | 0:cc61e9d1c295 | 37 | */ |
mgottscho | 0:cc61e9d1c295 | 38 | void warn(string errorMessage, int errorCode); |
mgottscho | 0:cc61e9d1c295 | 39 | |
mgottscho | 0:cc61e9d1c295 | 40 | /** |
mgottscho | 0:cc61e9d1c295 | 41 | * Object-oriented assert statement. |
mgottscho | 0:cc61e9d1c295 | 42 | * @param condition if false, calls panic() with results of the assertion, and never returns. |
mgottscho | 0:cc61e9d1c295 | 43 | * @param file string representing the source file where the assertion is called |
mgottscho | 0:cc61e9d1c295 | 44 | * @param line line number of the source file where the assertion is called |
mgottscho | 0:cc61e9d1c295 | 45 | */ |
mgottscho | 0:cc61e9d1c295 | 46 | void myAssert(bool condition, const char *file, const unsigned long line); |
mgottscho | 0:cc61e9d1c295 | 47 | |
mgottscho | 0:cc61e9d1c295 | 48 | /** |
mgottscho | 0:cc61e9d1c295 | 49 | * Blink the green user LED. This is a non-blocking call. The LED will blink until it is disabled. |
mgottscho | 0:cc61e9d1c295 | 50 | * @param enable if true, enables the blinking LED |
mgottscho | 0:cc61e9d1c295 | 51 | * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle). |
mgottscho | 0:cc61e9d1c295 | 52 | * If non-positive, the LED will stay on. |
mgottscho | 0:cc61e9d1c295 | 53 | * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect. |
mgottscho | 0:cc61e9d1c295 | 54 | */ |
mgottscho | 0:cc61e9d1c295 | 55 | void blinkGreen(bool enable, float half_period); |
mgottscho | 0:cc61e9d1c295 | 56 | |
mgottscho | 0:cc61e9d1c295 | 57 | /** |
mgottscho | 0:cc61e9d1c295 | 58 | * Blink the red user LED. This is a non-blocking call. The LED will blink until it is disabled. |
mgottscho | 0:cc61e9d1c295 | 59 | * @param enable if true, enables the blinking LED |
mgottscho | 0:cc61e9d1c295 | 60 | * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle). |
mgottscho | 0:cc61e9d1c295 | 61 | * If non-positive, the LED will stay on. |
mgottscho | 0:cc61e9d1c295 | 62 | * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect. |
mgottscho | 0:cc61e9d1c295 | 63 | */ |
mgottscho | 0:cc61e9d1c295 | 64 | void blinkRed(bool enable, float half_period); |
mgottscho | 0:cc61e9d1c295 | 65 | |
mgottscho | 1:2a3bbec22035 | 66 | /** |
mgottscho | 1:2a3bbec22035 | 67 | * Receives a line of data from the serial console, terminated by a carriage return character. |
mgottscho | 1:2a3bbec22035 | 68 | * @param line a pointer to the buffer in which to store the incoming data |
mgottscho | 1:2a3bbec22035 | 69 | * @param len the maximum number of bytes to receive |
mgottscho | 1:2a3bbec22035 | 70 | * @returns number of bytes received |
mgottscho | 1:2a3bbec22035 | 71 | */ |
mgottscho | 1:2a3bbec22035 | 72 | uint32_t receiveLine(char *line, const uint32_t len); |
mgottscho | 1:2a3bbec22035 | 73 | |
mgottscho | 1:2a3bbec22035 | 74 | /** |
mgottscho | 1:2a3bbec22035 | 75 | * Sends a line of data to the serial port. |
mgottscho | 1:2a3bbec22035 | 76 | * @param line a pointer to the beginning of the data to send |
mgottscho | 1:2a3bbec22035 | 77 | * @param len the number of bytes to send |
mgottscho | 1:2a3bbec22035 | 78 | * @returns number of bytes sent |
mgottscho | 1:2a3bbec22035 | 79 | */ |
mgottscho | 1:2a3bbec22035 | 80 | //uint32_t sendLine(const char *line, const uint32_t len); |
mgottscho | 1:2a3bbec22035 | 81 | |
mgottscho | 1:2a3bbec22035 | 82 | /** |
mgottscho | 1:2a3bbec22035 | 83 | * @returns true if there is data received from serial port ready to use. |
mgottscho | 1:2a3bbec22035 | 84 | */ |
mgottscho | 1:2a3bbec22035 | 85 | bool haveRxSerialData(); |
mgottscho | 1:2a3bbec22035 | 86 | |
mgottscho | 0:cc61e9d1c295 | 87 | Serial console; |
mgottscho | 0:cc61e9d1c295 | 88 | |
mgottscho | 0:cc61e9d1c295 | 89 | private: |
mgottscho | 0:cc61e9d1c295 | 90 | /** |
mgottscho | 0:cc61e9d1c295 | 91 | * Interrupt service routine for blinking the green user LED. |
mgottscho | 0:cc61e9d1c295 | 92 | */ |
mgottscho | 0:cc61e9d1c295 | 93 | void __greenLED_ISR(); |
mgottscho | 0:cc61e9d1c295 | 94 | |
mgottscho | 0:cc61e9d1c295 | 95 | /** |
mgottscho | 0:cc61e9d1c295 | 96 | * Interrupt service routine for blinking the red user LED. |
mgottscho | 0:cc61e9d1c295 | 97 | */ |
mgottscho | 0:cc61e9d1c295 | 98 | void __redLED_ISR(); |
mgottscho | 1:2a3bbec22035 | 99 | |
mgottscho | 1:2a3bbec22035 | 100 | /** |
mgottscho | 1:2a3bbec22035 | 101 | * Interrupt service routine for serial RX |
mgottscho | 1:2a3bbec22035 | 102 | */ |
mgottscho | 1:2a3bbec22035 | 103 | void __console_rx_ISR(); |
mgottscho | 1:2a3bbec22035 | 104 | |
mgottscho | 1:2a3bbec22035 | 105 | |
mgottscho | 1:2a3bbec22035 | 106 | /** |
mgottscho | 1:2a3bbec22035 | 107 | * Interrupt service routine for serial TX |
mgottscho | 1:2a3bbec22035 | 108 | */ |
mgottscho | 1:2a3bbec22035 | 109 | //void __console_tx_ISR(); |
mgottscho | 1:2a3bbec22035 | 110 | |
mgottscho | 0:cc61e9d1c295 | 111 | |
mgottscho | 0:cc61e9d1c295 | 112 | DigitalOut __green_led; |
mgottscho | 0:cc61e9d1c295 | 113 | Ticker __green_led_interrupt; |
mgottscho | 0:cc61e9d1c295 | 114 | |
mgottscho | 0:cc61e9d1c295 | 115 | DigitalOut __red_led; |
mgottscho | 0:cc61e9d1c295 | 116 | Ticker __red_led_interrupt; |
mgottscho | 1:2a3bbec22035 | 117 | |
mgottscho | 1:2a3bbec22035 | 118 | //Buffers for working with the serial console |
mgottscho | 1:2a3bbec22035 | 119 | const static uint32_t BUFFER_SIZE = 512; //For serial buffer |
mgottscho | 1:2a3bbec22035 | 120 | volatile uint8_t __rx_buf[BUFFER_SIZE]; |
mgottscho | 1:2a3bbec22035 | 121 | volatile uint8_t __tx_buf[BUFFER_SIZE]; |
mgottscho | 1:2a3bbec22035 | 122 | volatile uint32_t __rx_head; //Head always points to the first item to read (oldest) |
mgottscho | 1:2a3bbec22035 | 123 | volatile uint32_t __rx_tail; //Tail always points to the last item written (newest) |
mgottscho | 1:2a3bbec22035 | 124 | volatile uint32_t __tx_head; |
mgottscho | 1:2a3bbec22035 | 125 | volatile uint32_t __tx_tail; |
mgottscho | 1:2a3bbec22035 | 126 | volatile bool __have_rx_serial; //Flag for the RX data |
mgottscho | 0:cc61e9d1c295 | 127 | }; |
mgottscho | 0:cc61e9d1c295 | 128 | |
mgottscho | 0:cc61e9d1c295 | 129 | #endif |