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.cpp@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.cpp |
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 | #include "mbed.h" |
mgottscho | 0:cc61e9d1c295 | 8 | #include <string> |
mgottscho | 0:cc61e9d1c295 | 9 | #include "Utility.h" |
mgottscho | 0:cc61e9d1c295 | 10 | |
mgottscho | 0:cc61e9d1c295 | 11 | using namespace std; |
mgottscho | 0:cc61e9d1c295 | 12 | |
mgottscho | 0:cc61e9d1c295 | 13 | Utility::Utility(PinName green_led_pin, PinName red_led_pin, PinName serial_tx_pin, PinName serial_rx_pin) : |
mgottscho | 0:cc61e9d1c295 | 14 | console(serial_tx_pin, serial_rx_pin), |
mgottscho | 0:cc61e9d1c295 | 15 | __green_led(green_led_pin), |
mgottscho | 0:cc61e9d1c295 | 16 | __green_led_interrupt(), |
mgottscho | 0:cc61e9d1c295 | 17 | __red_led(red_led_pin), |
mgottscho | 0:cc61e9d1c295 | 18 | __red_led_interrupt() { |
mgottscho | 0:cc61e9d1c295 | 19 | } |
mgottscho | 0:cc61e9d1c295 | 20 | |
mgottscho | 0:cc61e9d1c295 | 21 | Utility::~Utility() { } |
mgottscho | 0:cc61e9d1c295 | 22 | |
mgottscho | 0:cc61e9d1c295 | 23 | |
mgottscho | 0:cc61e9d1c295 | 24 | void Utility::panic(string errorMessage, int errorCode) { |
mgottscho | 0:cc61e9d1c295 | 25 | //We're dead. This is the point of no return! Permanently ignore interrupts. |
mgottscho | 0:cc61e9d1c295 | 26 | __disable_irq(); |
mgottscho | 0:cc61e9d1c295 | 27 | |
mgottscho | 0:cc61e9d1c295 | 28 | console.printf(">>> PANIC, CODE # %d: %s <<<\r\n", errorCode, errorMessage); |
mgottscho | 0:cc61e9d1c295 | 29 | |
mgottscho | 0:cc61e9d1c295 | 30 | __green_led = 1; |
mgottscho | 0:cc61e9d1c295 | 31 | __red_led = 0; |
mgottscho | 0:cc61e9d1c295 | 32 | |
mgottscho | 0:cc61e9d1c295 | 33 | volatile uint32_t fake = 0; |
mgottscho | 0:cc61e9d1c295 | 34 | while(1) { fake = 0; } //Spinloop for eternity |
mgottscho | 0:cc61e9d1c295 | 35 | } |
mgottscho | 0:cc61e9d1c295 | 36 | |
mgottscho | 0:cc61e9d1c295 | 37 | void Utility::warn(string errorMessage, int errorCode) { |
mgottscho | 0:cc61e9d1c295 | 38 | console.printf(">>> WARN, CODE # %d: %s <<<\r\n", errorCode, errorMessage); |
mgottscho | 0:cc61e9d1c295 | 39 | } |
mgottscho | 0:cc61e9d1c295 | 40 | |
mgottscho | 0:cc61e9d1c295 | 41 | #ifdef NDEBUG |
mgottscho | 0:cc61e9d1c295 | 42 | void Utility::myAssert(bool condition, const char *file, const unsigned long line) { } |
mgottscho | 0:cc61e9d1c295 | 43 | #else |
mgottscho | 0:cc61e9d1c295 | 44 | void Utility::myAssert(bool condition, const char *file, const unsigned long line) { |
mgottscho | 0:cc61e9d1c295 | 45 | if (!condition) |
mgottscho | 0:cc61e9d1c295 | 46 | { |
mgottscho | 0:cc61e9d1c295 | 47 | char msg[256]; |
mgottscho | 0:cc61e9d1c295 | 48 | sprintf(msg, "Assertion failed at file %s, line %d", file, line); |
mgottscho | 0:cc61e9d1c295 | 49 | panic(msg, -1); \ |
mgottscho | 0:cc61e9d1c295 | 50 | } |
mgottscho | 0:cc61e9d1c295 | 51 | } |
mgottscho | 0:cc61e9d1c295 | 52 | #endif |
mgottscho | 0:cc61e9d1c295 | 53 | |
mgottscho | 0:cc61e9d1c295 | 54 | void Utility::blinkGreen(bool enable, float half_period) { |
mgottscho | 0:cc61e9d1c295 | 55 | if (enable) { |
mgottscho | 0:cc61e9d1c295 | 56 | if (half_period <= 0) { |
mgottscho | 0:cc61e9d1c295 | 57 | __green_led_interrupt.detach(); |
mgottscho | 0:cc61e9d1c295 | 58 | __green_led = 0; |
mgottscho | 0:cc61e9d1c295 | 59 | } else if (half_period <= 1800) |
mgottscho | 0:cc61e9d1c295 | 60 | __green_led_interrupt.attach(this, &Utility::__greenLED_ISR, half_period); |
mgottscho | 0:cc61e9d1c295 | 61 | } |
mgottscho | 0:cc61e9d1c295 | 62 | else { |
mgottscho | 0:cc61e9d1c295 | 63 | __green_led_interrupt.detach(); |
mgottscho | 0:cc61e9d1c295 | 64 | __green_led = 1; |
mgottscho | 0:cc61e9d1c295 | 65 | } |
mgottscho | 0:cc61e9d1c295 | 66 | } |
mgottscho | 0:cc61e9d1c295 | 67 | |
mgottscho | 0:cc61e9d1c295 | 68 | void Utility::blinkRed(bool enable, float half_period) { |
mgottscho | 0:cc61e9d1c295 | 69 | if (enable) { |
mgottscho | 0:cc61e9d1c295 | 70 | if (half_period <= 0) { |
mgottscho | 0:cc61e9d1c295 | 71 | __red_led_interrupt.detach(); |
mgottscho | 0:cc61e9d1c295 | 72 | __red_led = 0; |
mgottscho | 0:cc61e9d1c295 | 73 | } else if (half_period <= 1800) |
mgottscho | 0:cc61e9d1c295 | 74 | __red_led_interrupt.attach(this, &Utility::__redLED_ISR, half_period); |
mgottscho | 0:cc61e9d1c295 | 75 | } |
mgottscho | 0:cc61e9d1c295 | 76 | else { |
mgottscho | 0:cc61e9d1c295 | 77 | __red_led_interrupt.detach(); |
mgottscho | 0:cc61e9d1c295 | 78 | __red_led = 1; |
mgottscho | 0:cc61e9d1c295 | 79 | } |
mgottscho | 0:cc61e9d1c295 | 80 | } |
mgottscho | 0:cc61e9d1c295 | 81 | |
mgottscho | 0:cc61e9d1c295 | 82 | void Utility::__greenLED_ISR() { |
mgottscho | 0:cc61e9d1c295 | 83 | if (__green_led == 0) //Just flip the LED status every time we are called |
mgottscho | 0:cc61e9d1c295 | 84 | __green_led = 1; |
mgottscho | 0:cc61e9d1c295 | 85 | else |
mgottscho | 0:cc61e9d1c295 | 86 | __green_led = 0; |
mgottscho | 0:cc61e9d1c295 | 87 | } |
mgottscho | 0:cc61e9d1c295 | 88 | |
mgottscho | 0:cc61e9d1c295 | 89 | void Utility::__redLED_ISR() { |
mgottscho | 0:cc61e9d1c295 | 90 | if (__red_led == 0) //Just flip the LED status every time we are called |
mgottscho | 0:cc61e9d1c295 | 91 | __red_led = 1; |
mgottscho | 0:cc61e9d1c295 | 92 | else |
mgottscho | 0:cc61e9d1c295 | 93 | __red_led = 0; |
mgottscho | 0:cc61e9d1c295 | 94 | } |