From 5dcfa4f1b03aeb29530403ecaca190fd001ada79 Mon Sep 17 00:00:00 2001 From: Roger Clark Date: Tue, 7 Apr 2015 20:57:51 +1000 Subject: [PATCH] Removed non standard functions togglePin, toggleLED,isButtonPressed, and waitForButtonPress. And updated all examples that used these, to use other functions. Note togglePin was used internally in shiftOut. Also note, that some of these functions were replaced by the code that was inside the non standard function e.g inside togglePin. In the longer term these internal maple function calls need to be replaced by high level Arduino API calls like digitalRead and digitalWrite where applicable --- STM32F1/cores/maple/io.h | 446 ++--- STM32F1/cores/maple/wirish_digital.cpp | 191 +-- STM32F1/cores/maple/wirish_shift.cpp | 74 +- .../libraries/LiquidCrystal/LiquidCrystal.cpp | 672 ++++---- examples/Digital/Blink/Blink.ino | 2 +- .../BlinkWithoutDelay/BlinkWithoutDelay.ino | 74 +- examples/Digital/Button/Button.ino | 16 +- .../Maple/InteractiveTest/InteractiveTest.ino | 1450 ++++++++--------- examples/Maple/QASlave/QASlave.ino | 132 +- examples/MrBrunetteExamples/Blink/Blink.ino | 34 +- .../BlinkNcount/BlinkNcount.ino | 70 +- .../IntegerInput/IntegerInput.ino | 92 +- .../IntegerInput_FloatOutput.ino | 100 +- .../InternalTempSensor/InternalTempSensor.ino | 160 +- .../MrBrunetteExamples/PrimeNos/PrimeNos.ino | 138 +- .../PrimeNos2/PrimeNos2.ino | 82 +- .../PrimeNos3/PrimeNos3.ino | 82 +- .../Print_Binary/Print_Binary.ino | 132 +- .../Print_Float/Print_Float.ino | 168 +- .../Print_HEX/Print_HEX.ino | 122 +- .../SerialReadUntil/SerialReadUntil.ino | 70 +- .../StringEx_Parsing/StringEx_Parsing.ino | Bin 3230 -> 3318 bytes .../USB_ASCII/USB_ASCII.ino | 208 +-- .../strtol_DecEquivalents.ino | 82 +- examples/Sensors/Knock/Knock.ino | 104 +- 25 files changed, 2359 insertions(+), 2342 deletions(-) diff --git a/STM32F1/cores/maple/io.h b/STM32F1/cores/maple/io.h index debed12..4adb2c5 100644 --- a/STM32F1/cores/maple/io.h +++ b/STM32F1/cores/maple/io.h @@ -1,222 +1,224 @@ -/****************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Perry Hung. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ - -/** - * @file wirish/include/wirish/io.h - * @brief Wiring-style pin I/O interface. - */ - -#ifndef _WIRISH_IO_H_ -#define _WIRISH_IO_H_ - -#include -#include - -/** - * Specifies a GPIO pin behavior. - * @see pinMode() - */ -typedef enum WiringPinMode { - OUTPUT, /**< Basic digital output: when the pin is HIGH, the - voltage is held at +3.3v (Vcc) and when it is LOW, it - is pulled down to ground. */ - - OUTPUT_OPEN_DRAIN, /**< In open drain mode, the pin indicates - "low" by accepting current flow to ground - and "high" by providing increased - impedance. An example use would be to - connect a pin to a bus line (which is pulled - up to a positive voltage by a separate - supply through a large resistor). When the - pin is high, not much current flows through - to ground and the line stays at positive - voltage; when the pin is low, the bus - "drains" to ground with a small amount of - current constantly flowing through the large - resistor from the external supply. In this - mode, no current is ever actually sourced - from the pin. */ - - INPUT, /**< Basic digital input. The pin voltage is sampled; when - it is closer to 3.3v (Vcc) the pin status is high, and - when it is closer to 0v (ground) it is low. If no - external circuit is pulling the pin voltage to high or - low, it will tend to randomly oscillate and be very - sensitive to noise (e.g., a breath of air across the pin - might cause the state to flip). */ - - INPUT_ANALOG, /**< This is a special mode for when the pin will be - used for analog (not digital) reads. Enables ADC - conversion to be performed on the voltage at the - pin. */ - - INPUT_PULLUP, /**< The state of the pin in this mode is reported - the same way as with INPUT, but the pin voltage - is gently "pulled up" towards +3.3v. This means - the state will be high unless an external device - is specifically pulling the pin down to ground, - in which case the "gentle" pull up will not - affect the state of the input. */ - - INPUT_PULLDOWN, /**< The state of the pin in this mode is reported - the same way as with INPUT, but the pin voltage - is gently "pulled down" towards 0v. This means - the state will be low unless an external device - is specifically pulling the pin up to 3.3v, in - which case the "gentle" pull down will not - affect the state of the input. */ - - INPUT_FLOATING, /**< Synonym for INPUT. */ - - PWM, /**< This is a special mode for when the pin will be used for - PWM output (a special case of digital output). */ - - PWM_OPEN_DRAIN, /**< Like PWM, except that instead of alternating - cycles of LOW and HIGH, the voltage on the pin - consists of alternating cycles of LOW and - floating (disconnected). */ -} WiringPinMode; - -/** - * Configure behavior of a GPIO pin. - * - * @param pin Number of pin to configure. - * @param mode Mode corresponding to desired pin behavior. - * @see WiringPinMode - */ -void pinMode(uint8 pin, WiringPinMode mode); - -#define HIGH 0x1 -#define LOW 0x0 - -/** - * Writes a (digital) value to a pin. The pin must have its - * mode set to OUTPUT or OUTPUT_OPEN_DRAIN. - * - * @param pin Pin to write to. - * @param value Either LOW (write a 0) or HIGH (write a 1). - * @see pinMode() - */ -void digitalWrite(uint8 pin, uint8 value); - -/** - * Read a digital value from a pin. The pin must have its mode set to - * one of INPUT, INPUT_PULLUP, and INPUT_PULLDOWN. - * - * @param pin Pin to read from. - * @return LOW or HIGH. - * @see pinMode() - */ -uint32 digitalRead(uint8 pin); - -/** - * Read an analog value from pin. This function blocks during ADC - * conversion, and has 12 bits of resolution. The pin must have its - * mode set to INPUT_ANALOG. - * - * @param pin Pin to read from. - * @return Converted voltage, in the range 0--4095, (i.e. a 12-bit ADC - * conversion). - * @see pinMode() - */ -uint16 analogRead(uint8 pin); - -/** - * Toggles the digital value at the given pin. - * - * The pin must have its mode set to OUTPUT. - * - * @param pin the pin to toggle. If the pin is HIGH, set it LOW. If - * it is LOW, set it HIGH. - * - * @see pinMode() - */ -void togglePin(uint8 pin); - -/** - * Toggle the LED. - * - * If the LED is on, turn it off. If it is off, turn it on. - * - * The LED must its mode set to OUTPUT. This can be accomplished - * portably over all LeafLabs boards by calling pinMode(BOARD_LED_PIN, - * OUTPUT) before calling this function. - * - * @see pinMode() - */ -static inline void toggleLED() { - togglePin(BOARD_LED_PIN); -} - -/** - * If the button is currently pressed, waits until the button is no - * longer being pressed, and returns true. Otherwise, returns false. - * - * The button pin must have its mode set to INPUT. This can be - * accomplished portably over all LeafLabs boards by calling - * pinMode(BOARD_BUTTON_PIN, INPUT). - * - * @see pinMode() - */ -uint8 isButtonPressed(uint8 pin=BOARD_BUTTON_PIN, - uint32 pressedLevel=BOARD_BUTTON_PRESSED_LEVEL); - -/** - * Wait until the button is pressed and released, timing out if no - * press occurs. - * - * The button pin must have its mode set to INPUT. This can be - * accomplished portably over all LeafLabs boards by calling - * pinMode(BOARD_BUTTON_PIN, INPUT). - * - * @param timeout_millis Number of milliseconds to wait until the - * button is pressed. If timeout_millis is left out (or 0), wait - * forever. - * - * @return true, if the button was pressed; false, if the timeout was - * reached. - * - * @see pinMode() - */ -uint8 waitForButtonPress(uint32 timeout_millis=0); - -/** - * Shift out a byte of data, one bit at a time. - * - * This function starts at either the most significant or least - * significant bit in a byte value, and shifts out each byte in order - * onto a data pin. After each bit is written to the data pin, a - * separate clock pin is pulsed to indicate that the new bit is - * available. - * - * @param dataPin Pin to shift data out on - * @param clockPin Pin to pulse after each bit is shifted out - * @param bitOrder Either MSBFIRST (big-endian) or LSBFIRST (little-endian). - * @param value Value to shift out - */ -void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 value); - -#endif +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 Perry Hung. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +/** + * @file wirish/include/wirish/io.h + * @brief Wiring-style pin I/O interface. + */ + +#ifndef _WIRISH_IO_H_ +#define _WIRISH_IO_H_ + +#include +#include + +/** + * Specifies a GPIO pin behavior. + * @see pinMode() + */ +typedef enum WiringPinMode { + OUTPUT, /**< Basic digital output: when the pin is HIGH, the + voltage is held at +3.3v (Vcc) and when it is LOW, it + is pulled down to ground. */ + + OUTPUT_OPEN_DRAIN, /**< In open drain mode, the pin indicates + "low" by accepting current flow to ground + and "high" by providing increased + impedance. An example use would be to + connect a pin to a bus line (which is pulled + up to a positive voltage by a separate + supply through a large resistor). When the + pin is high, not much current flows through + to ground and the line stays at positive + voltage; when the pin is low, the bus + "drains" to ground with a small amount of + current constantly flowing through the large + resistor from the external supply. In this + mode, no current is ever actually sourced + from the pin. */ + + INPUT, /**< Basic digital input. The pin voltage is sampled; when + it is closer to 3.3v (Vcc) the pin status is high, and + when it is closer to 0v (ground) it is low. If no + external circuit is pulling the pin voltage to high or + low, it will tend to randomly oscillate and be very + sensitive to noise (e.g., a breath of air across the pin + might cause the state to flip). */ + + INPUT_ANALOG, /**< This is a special mode for when the pin will be + used for analog (not digital) reads. Enables ADC + conversion to be performed on the voltage at the + pin. */ + + INPUT_PULLUP, /**< The state of the pin in this mode is reported + the same way as with INPUT, but the pin voltage + is gently "pulled up" towards +3.3v. This means + the state will be high unless an external device + is specifically pulling the pin down to ground, + in which case the "gentle" pull up will not + affect the state of the input. */ + + INPUT_PULLDOWN, /**< The state of the pin in this mode is reported + the same way as with INPUT, but the pin voltage + is gently "pulled down" towards 0v. This means + the state will be low unless an external device + is specifically pulling the pin up to 3.3v, in + which case the "gentle" pull down will not + affect the state of the input. */ + + INPUT_FLOATING, /**< Synonym for INPUT. */ + + PWM, /**< This is a special mode for when the pin will be used for + PWM output (a special case of digital output). */ + + PWM_OPEN_DRAIN, /**< Like PWM, except that instead of alternating + cycles of LOW and HIGH, the voltage on the pin + consists of alternating cycles of LOW and + floating (disconnected). */ +} WiringPinMode; + +/** + * Configure behavior of a GPIO pin. + * + * @param pin Number of pin to configure. + * @param mode Mode corresponding to desired pin behavior. + * @see WiringPinMode + */ +void pinMode(uint8 pin, WiringPinMode mode); + +#define HIGH 0x1 +#define LOW 0x0 + +/** + * Writes a (digital) value to a pin. The pin must have its + * mode set to OUTPUT or OUTPUT_OPEN_DRAIN. + * + * @param pin Pin to write to. + * @param value Either LOW (write a 0) or HIGH (write a 1). + * @see pinMode() + */ +void digitalWrite(uint8 pin, uint8 value); + +/** + * Read a digital value from a pin. The pin must have its mode set to + * one of INPUT, INPUT_PULLUP, and INPUT_PULLDOWN. + * + * @param pin Pin to read from. + * @return LOW or HIGH. + * @see pinMode() + */ +uint32 digitalRead(uint8 pin); + +/** + * Read an analog value from pin. This function blocks during ADC + * conversion, and has 12 bits of resolution. The pin must have its + * mode set to INPUT_ANALOG. + * + * @param pin Pin to read from. + * @return Converted voltage, in the range 0--4095, (i.e. a 12-bit ADC + * conversion). + * @see pinMode() + */ +uint16 analogRead(uint8 pin); +#if FALSE +// Roger Clark. Deprecated these functions as they are not part of the standard Arduino API + +/** + * Toggles the digital value at the given pin. + * + * The pin must have its mode set to OUTPUT. + * + * @param pin the pin to toggle. If the pin is HIGH, set it LOW. If + * it is LOW, set it HIGH. + * + * @see pinMode() + */ +void togglePin(uint8 pin); + +/** + * Toggle the LED. + * + * If the LED is on, turn it off. If it is off, turn it on. + * + * The LED must its mode set to OUTPUT. This can be accomplished + * portably over all LeafLabs boards by calling pinMode(BOARD_LED_PIN, + * OUTPUT) before calling this function. + * + * @see pinMode() + */ +static inline void toggleLED() { + togglePin(BOARD_LED_PIN); +} + +/** + * If the button is currently pressed, waits until the button is no + * longer being pressed, and returns true. Otherwise, returns false. + * + * The button pin must have its mode set to INPUT. This can be + * accomplished portably over all LeafLabs boards by calling + * pinMode(BOARD_BUTTON_PIN, INPUT). + * + * @see pinMode() + */ +uint8 isButtonPressed(uint8 pin=BOARD_BUTTON_PIN, + uint32 pressedLevel=BOARD_BUTTON_PRESSED_LEVEL); + +/** + * Wait until the button is pressed and released, timing out if no + * press occurs. + * + * The button pin must have its mode set to INPUT. This can be + * accomplished portably over all LeafLabs boards by calling + * pinMode(BOARD_BUTTON_PIN, INPUT). + * + * @param timeout_millis Number of milliseconds to wait until the + * button is pressed. If timeout_millis is left out (or 0), wait + * forever. + * + * @return true, if the button was pressed; false, if the timeout was + * reached. + * + * @see pinMode() + */ +uint8 waitForButtonPress(uint32 timeout_millis=0); +#endif +/** + * Shift out a byte of data, one bit at a time. + * + * This function starts at either the most significant or least + * significant bit in a byte value, and shifts out each byte in order + * onto a data pin. After each bit is written to the data pin, a + * separate clock pin is pulsed to indicate that the new bit is + * available. + * + * @param dataPin Pin to shift data out on + * @param clockPin Pin to pulse after each bit is shifted out + * @param bitOrder Either MSBFIRST (big-endian) or LSBFIRST (little-endian). + * @param value Value to shift out + */ +void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 value); + +#endif diff --git a/STM32F1/cores/maple/wirish_digital.cpp b/STM32F1/cores/maple/wirish_digital.cpp index 3831e03..53bd69b 100644 --- a/STM32F1/cores/maple/wirish_digital.cpp +++ b/STM32F1/cores/maple/wirish_digital.cpp @@ -1,94 +1,97 @@ -/****************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Perry Hung. - * Copyright (c) 2012 LeafLabs, LLC. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ - -/* - * Arduino-compatible digital I/O implementation. - */ - -#include "io.h" - -#include -#include - -#include "wirish_time.h" -#include "boards.h" - -uint32 digitalRead(uint8 pin) { - if (pin >= BOARD_NR_GPIO_PINS) { - return 0; - } - - return gpio_read_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit) ? - HIGH : LOW; -} - -void digitalWrite(uint8 pin, uint8 val) { - if (pin >= BOARD_NR_GPIO_PINS) { - return; - } - - gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val); -} - -void togglePin(uint8 pin) { - if (pin >= BOARD_NR_GPIO_PINS) { - return; - } - - gpio_toggle_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit); -} - -#define BUTTON_DEBOUNCE_DELAY 1 - -uint8 isButtonPressed(uint8 pin, uint32 pressedLevel) { - if (digitalRead(pin) == pressedLevel) { - delay(BUTTON_DEBOUNCE_DELAY); - while (digitalRead(pin) == pressedLevel) - ; - return true; - } - return false; -} - -uint8 waitForButtonPress(uint32 timeout) { - uint32 start = millis(); - uint32 time; - if (timeout == 0) { - while (!isButtonPressed()) - ; - return true; - } - do { - time = millis(); - /* properly handle wrap-around */ - if ((start > time && time + (0xffffffffU - start) > timeout) || - time - start > timeout) { - return false; - } - } while (!isButtonPressed()); - return true; -} +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 Perry Hung. + * Copyright (c) 2012 LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +/* + * Arduino-compatible digital I/O implementation. + */ + +#include "io.h" + +#include +#include + +#include "wirish_time.h" +#include "boards.h" + +uint32 digitalRead(uint8 pin) { + if (pin >= BOARD_NR_GPIO_PINS) { + return 0; + } + + return gpio_read_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit) ? + HIGH : LOW; +} + +void digitalWrite(uint8 pin, uint8 val) { + if (pin >= BOARD_NR_GPIO_PINS) { + return; + } + + gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val); +} + +#if FALSE +// Roger Clark. Deprecated these functions as they are not part of the standard Arduino API +void togglePin(uint8 pin) { + if (pin >= BOARD_NR_GPIO_PINS) { + return; + } + + gpio_toggle_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit); +} + +#define BUTTON_DEBOUNCE_DELAY 1 + +uint8 isButtonPressed(uint8 pin, uint32 pressedLevel) { + if (digitalRead(pin) == pressedLevel) { + delay(BUTTON_DEBOUNCE_DELAY); + while (digitalRead(pin) == pressedLevel) + ; + return true; + } + return false; +} + +uint8 waitForButtonPress(uint32 timeout) { + uint32 start = millis(); + uint32 time; + if (timeout == 0) { + while (!isButtonPressed()) + ; + return true; + } + do { + time = millis(); + /* properly handle wrap-around */ + if ((start > time && time + (0xffffffffU - start) > timeout) || + time - start > timeout) { + return false; + } + } while (!isButtonPressed()); + return true; +} +#endif \ No newline at end of file diff --git a/STM32F1/cores/maple/wirish_shift.cpp b/STM32F1/cores/maple/wirish_shift.cpp index 9caef27..2825507 100644 --- a/STM32F1/cores/maple/wirish_shift.cpp +++ b/STM32F1/cores/maple/wirish_shift.cpp @@ -1,37 +1,37 @@ -/****************************************************************************** - * The MIT License - * - * Copyright (c) 2012 LeafLabs, LLC. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ - -#include "wirish.h" - -void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 value) { - digitalWrite(clockPin, LOW); - for (int i = 0; i < 8; i++) { - int bit = bitOrder == LSBFIRST ? i : (7 - i); - digitalWrite(dataPin, (value >> bit) & 0x1); - togglePin(clockPin); - togglePin(clockPin); - } -} +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2012 LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +#include "wirish.h" + +void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 value) { + digitalWrite(clockPin, LOW); + for (int i = 0; i < 8; i++) { + int bit = bitOrder == LSBFIRST ? i : (7 - i); + digitalWrite(dataPin, (value >> bit) & 0x1); + gpio_toggle_bit(PIN_MAP[clockPin].gpio_device, PIN_MAP[clockPin].gpio_bit);// togglePin(clockPin); + gpio_toggle_bit(PIN_MAP[clockPin].gpio_device, PIN_MAP[clockPin].gpio_bit);// togglePin(clockPin); + } +} diff --git a/STM32F1/libraries/LiquidCrystal/LiquidCrystal.cpp b/STM32F1/libraries/LiquidCrystal/LiquidCrystal.cpp index bcec207..2cced7a 100644 --- a/STM32F1/libraries/LiquidCrystal/LiquidCrystal.cpp +++ b/STM32F1/libraries/LiquidCrystal/LiquidCrystal.cpp @@ -1,336 +1,336 @@ -#include "LiquidCrystal.h" - -#include -#include -#include - -// When the display powers up, it is configured as follows: -// -// 1. Display clear -// 2. Function set: -// DL = 1; 8-bit interface data -// N = 0; 1-line display -// F = 0; 5x8 dot character font -// 3. Display on/off control: -// D = 0; Display off -// C = 0; Cursor off -// B = 0; Blinking off -// 4. Entry mode set: -// I/D = 1; Increment by 1 -// S = 0; No shift -// -// Note, however, that resetting the Arduino doesn't reset the LCD, so we -// can't assume that its in that state when a sketch starts (and the -// LiquidCrystal constructor is called). - -// This library has been modified to be compatible with the LeafLabs Maple; -// very conservative timing is used due to problems with delayMicroseconds() -// that should be fixed in the 0.0.7 release of the libmaple. [bnewbold] - -LiquidCrystal::LiquidCrystal(uint8 rs, uint8 rw, uint8 enable, - uint8 d0, uint8 d1, uint8 d2, uint8 d3, - uint8 d4, uint8 d5, uint8 d6, uint8 d7) -{ - init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7); -} - -LiquidCrystal::LiquidCrystal(uint8 rs, uint8 enable, - uint8 d0, uint8 d1, uint8 d2, uint8 d3, - uint8 d4, uint8 d5, uint8 d6, uint8 d7) -{ - init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7); -} - -LiquidCrystal::LiquidCrystal(uint8 rs, uint8 rw, uint8 enable, - uint8 d0, uint8 d1, uint8 d2, uint8 d3) -{ - init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0); -} - -LiquidCrystal::LiquidCrystal(uint8 rs, uint8 enable, - uint8 d0, uint8 d1, uint8 d2, uint8 d3) -{ - init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0); -} - -void LiquidCrystal::init(uint8 fourbitmode, uint8 rs, uint8 rw, uint8 enable, - uint8 d0, uint8 d1, uint8 d2, uint8 d3, - uint8 d4, uint8 d5, uint8 d6, uint8 d7) -{ - _rs_pin = rs; - _rw_pin = rw; - _enable_pin = enable; - - _data_pins[0] = d0; - _data_pins[1] = d1; - _data_pins[2] = d2; - _data_pins[3] = d3; - _data_pins[4] = d4; - _data_pins[5] = d5; - _data_pins[6] = d6; - _data_pins[7] = d7; - displaymode=fourbitmode; - - if (fourbitmode) - _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; - else - _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; - - // TODO: bnewbold, re-enable this? -// begin(16, 1); -} - -void LiquidCrystal::begin(uint8 cols, uint8 lines, uint8 dotsize) { - - for (int i = 0; i < 8 - displaymode * 4; i++) { - //for (int i = 0; i <4; i++) { - pinMode(_data_pins[i], OUTPUT); - } - - pinMode(_rs_pin, OUTPUT); - // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin# - if (_rw_pin != 255) { - pinMode(_rw_pin, OUTPUT); - } - pinMode(_enable_pin, OUTPUT); - if (lines > 1) { - _displayfunction |= LCD_2LINE; - } - _numlines = lines; - _currline = 0; - - // for some 1 line displays you can select a 10 pixel high font - if ((dotsize != 0) && (lines == 1)) { - _displayfunction |= LCD_5x10DOTS; - } - - // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! according to - // datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way befer 4.5V so - // we'll wait 50 - delay(50); // Maple mod - //delayMicroseconds(50000); - // Now we pull both RS and R/W low to begin commands - digitalWrite(_rs_pin, LOW); - digitalWrite(_enable_pin, LOW); - if (_rw_pin != 255) { - digitalWrite(_rw_pin, LOW); - } - - //put the LCD into 4 bit or 8 bit mode - if (! (_displayfunction & LCD_8BITMODE)) { - // this is according to the hitachi HD44780 datasheet - // figure 24, pg 46 - - // we start in 8bit mode, try to set 4 bit mode - write4bits(0x03); - delay(5); // Maple mod - //delayMicroseconds(4500); // wait min 4.1ms - - // second try - write4bits(0x03); - delay(5); // Maple mod - //delayMicroseconds(4500); // wait min 4.1ms - - // third go! - write4bits(0x03); - delay(1); // Maple mod - //delayMicroseconds(150); - - // finally, set to 8-bit interface - write4bits(0x02); - } else { - // this is according to the hitachi HD44780 datasheet - // page 45 figure 23 - - // Send function set command sequence - command(LCD_FUNCTIONSET | _displayfunction); - delay(5); // Maple mod - //delayMicroseconds(4500); // wait more than 4.1ms - - // second try - command(LCD_FUNCTIONSET | _displayfunction); - delay(1); // Maple mod - //delayMicroseconds(150); - - // third go - command(LCD_FUNCTIONSET | _displayfunction); - } - - // finally, set # lines, font size, etc. - command(LCD_FUNCTIONSET | _displayfunction); - - // turn the display on with no cursor or blinking default - _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; - display(); - - // clear it off - clear(); - - // Initialize to default text direction (for romance languages) - _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; - // set the entry mode - command(LCD_ENTRYMODESET | _displaymode); - -} - -/********** high level commands, for the user! */ -void LiquidCrystal::clear() -{ - command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero - delay(2); // Maple mod - //delayMicroseconds(2000); // this command takes a long time! -} - -void LiquidCrystal::home() -{ - command(LCD_RETURNHOME); // set cursor position to zero - delay(2); // Maple mod - //delayMicroseconds(2000); // this command takes a long time! -} - -void LiquidCrystal::setCursor(uint8 col, uint8 row) -{ - int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; - if ( row > _numlines ) { - row = _numlines-1; // we count rows starting w/0 - } - - command(LCD_SETDDRAMADDR | (col + row_offsets[row])); -} - -// Turn the display on/off (quickly) -void LiquidCrystal::noDisplay() { - _displaycontrol &= ~LCD_DISPLAYON; - command(LCD_DISPLAYCONTROL | _displaycontrol); -} -void LiquidCrystal::display() { - _displaycontrol |= LCD_DISPLAYON; - command(LCD_DISPLAYCONTROL | _displaycontrol); -} - -// Turns the underline cursor on/off -void LiquidCrystal::noCursor() { - _displaycontrol &= ~LCD_CURSORON; - command(LCD_DISPLAYCONTROL | _displaycontrol); -} -void LiquidCrystal::cursor() { - _displaycontrol |= LCD_CURSORON; - command(LCD_DISPLAYCONTROL | _displaycontrol); -} - -// Turn on and off the blinking cursor -void LiquidCrystal::noBlink() { - _displaycontrol &= ~LCD_BLINKON; - command(LCD_DISPLAYCONTROL | _displaycontrol); -} -void LiquidCrystal::blink() { - _displaycontrol |= LCD_BLINKON; - command(LCD_DISPLAYCONTROL | _displaycontrol); -} - -// These commands scroll the display without changing the RAM -void LiquidCrystal::scrollDisplayLeft(void) { - command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); -} -void LiquidCrystal::scrollDisplayRight(void) { - command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); -} - -// This is for text that flows Left to Right -void LiquidCrystal::leftToRight(void) { - _displaymode |= LCD_ENTRYLEFT; - command(LCD_ENTRYMODESET | _displaymode); -} - -// This is for text that flows Right to Left -void LiquidCrystal::rightToLeft(void) { - _displaymode &= ~LCD_ENTRYLEFT; - command(LCD_ENTRYMODESET | _displaymode); -} - -// This will 'right justify' text from the cursor -void LiquidCrystal::autoscroll(void) { - _displaymode |= LCD_ENTRYSHIFTINCREMENT; - command(LCD_ENTRYMODESET | _displaymode); -} - -// This will 'left justify' text from the cursor -void LiquidCrystal::noAutoscroll(void) { - _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; - command(LCD_ENTRYMODESET | _displaymode); -} - -// Allows us to fill the first 8 CGRAM locations -// with custom characters -void LiquidCrystal::createChar(uint8 location, uint8 charmap[]) { - location &= 0x7; // we only have 8 locations 0-7 - command(LCD_SETCGRAMADDR | (location << 3)); - for (int i=0; i<8; i++) { - write(charmap[i]); - } -} - -/*********** mid level commands, for sending data/cmds */ - -inline void LiquidCrystal::command(uint8 value) { - send(value, LOW); -} - -inline size_t LiquidCrystal::write(uint8 value) { - send(value, HIGH); - return 1; -} - -/************ low level data pushing commands **********/ - -// write either command or data, with automatic 4/8-bit selection -void LiquidCrystal::send(uint8 value, uint8 mode) { - digitalWrite(_rs_pin, mode); - - // if there is a RW pin indicated, set it low to Write - if (_rw_pin != 255) { - digitalWrite(_rw_pin, LOW); - } - - if (_displayfunction & LCD_8BITMODE) { - write8bits(value); - } else { - write4bits(value>>4); - write4bits(value); - } -} - -void LiquidCrystal::pulseEnable(void) { - // _enable_pin should already be LOW (unless someone else messed - // with it), so don't sit around waiting for long. - digitalWrite(_enable_pin, LOW); - delayMicroseconds(1); - - // Enable pulse must be > 450 ns. Value chosen here according to - // the following threads: - // http://forums.leaflabs.com/topic.php?id=640 - // http://forums.leaflabs.com/topic.php?id=512 - togglePin(_enable_pin); - delayMicroseconds(1); - togglePin(_enable_pin); - - // Commands needs > 37us to settle. - delayMicroseconds(42); -} - -void LiquidCrystal::write4bits(uint8 value) { - for (int i = 0; i < 4; i++) { - digitalWrite(_data_pins[i], (value >> i) & 0x01); - } - - pulseEnable(); -} - -void LiquidCrystal::write8bits(uint8 value) { - for (int i = 0; i < 8; i++) { - digitalWrite(_data_pins[i], (value >> i) & 0x01); - } - - pulseEnable(); -} +#include "LiquidCrystal.h" + +#include +#include +#include + +// When the display powers up, it is configured as follows: +// +// 1. Display clear +// 2. Function set: +// DL = 1; 8-bit interface data +// N = 0; 1-line display +// F = 0; 5x8 dot character font +// 3. Display on/off control: +// D = 0; Display off +// C = 0; Cursor off +// B = 0; Blinking off +// 4. Entry mode set: +// I/D = 1; Increment by 1 +// S = 0; No shift +// +// Note, however, that resetting the Arduino doesn't reset the LCD, so we +// can't assume that its in that state when a sketch starts (and the +// LiquidCrystal constructor is called). + +// This library has been modified to be compatible with the LeafLabs Maple; +// very conservative timing is used due to problems with delayMicroseconds() +// that should be fixed in the 0.0.7 release of the libmaple. [bnewbold] + +LiquidCrystal::LiquidCrystal(uint8 rs, uint8 rw, uint8 enable, + uint8 d0, uint8 d1, uint8 d2, uint8 d3, + uint8 d4, uint8 d5, uint8 d6, uint8 d7) +{ + init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7); +} + +LiquidCrystal::LiquidCrystal(uint8 rs, uint8 enable, + uint8 d0, uint8 d1, uint8 d2, uint8 d3, + uint8 d4, uint8 d5, uint8 d6, uint8 d7) +{ + init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7); +} + +LiquidCrystal::LiquidCrystal(uint8 rs, uint8 rw, uint8 enable, + uint8 d0, uint8 d1, uint8 d2, uint8 d3) +{ + init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0); +} + +LiquidCrystal::LiquidCrystal(uint8 rs, uint8 enable, + uint8 d0, uint8 d1, uint8 d2, uint8 d3) +{ + init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0); +} + +void LiquidCrystal::init(uint8 fourbitmode, uint8 rs, uint8 rw, uint8 enable, + uint8 d0, uint8 d1, uint8 d2, uint8 d3, + uint8 d4, uint8 d5, uint8 d6, uint8 d7) +{ + _rs_pin = rs; + _rw_pin = rw; + _enable_pin = enable; + + _data_pins[0] = d0; + _data_pins[1] = d1; + _data_pins[2] = d2; + _data_pins[3] = d3; + _data_pins[4] = d4; + _data_pins[5] = d5; + _data_pins[6] = d6; + _data_pins[7] = d7; + displaymode=fourbitmode; + + if (fourbitmode) + _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; + else + _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; + + // TODO: bnewbold, re-enable this? +// begin(16, 1); +} + +void LiquidCrystal::begin(uint8 cols, uint8 lines, uint8 dotsize) { + + for (int i = 0; i < 8 - displaymode * 4; i++) { + //for (int i = 0; i <4; i++) { + pinMode(_data_pins[i], OUTPUT); + } + + pinMode(_rs_pin, OUTPUT); + // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin# + if (_rw_pin != 255) { + pinMode(_rw_pin, OUTPUT); + } + pinMode(_enable_pin, OUTPUT); + if (lines > 1) { + _displayfunction |= LCD_2LINE; + } + _numlines = lines; + _currline = 0; + + // for some 1 line displays you can select a 10 pixel high font + if ((dotsize != 0) && (lines == 1)) { + _displayfunction |= LCD_5x10DOTS; + } + + // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! according to + // datasheet, we need at least 40ms after power rises above 2.7V + // before sending commands. Arduino can turn on way befer 4.5V so + // we'll wait 50 + delay(50); // Maple mod + //delayMicroseconds(50000); + // Now we pull both RS and R/W low to begin commands + digitalWrite(_rs_pin, LOW); + digitalWrite(_enable_pin, LOW); + if (_rw_pin != 255) { + digitalWrite(_rw_pin, LOW); + } + + //put the LCD into 4 bit or 8 bit mode + if (! (_displayfunction & LCD_8BITMODE)) { + // this is according to the hitachi HD44780 datasheet + // figure 24, pg 46 + + // we start in 8bit mode, try to set 4 bit mode + write4bits(0x03); + delay(5); // Maple mod + //delayMicroseconds(4500); // wait min 4.1ms + + // second try + write4bits(0x03); + delay(5); // Maple mod + //delayMicroseconds(4500); // wait min 4.1ms + + // third go! + write4bits(0x03); + delay(1); // Maple mod + //delayMicroseconds(150); + + // finally, set to 8-bit interface + write4bits(0x02); + } else { + // this is according to the hitachi HD44780 datasheet + // page 45 figure 23 + + // Send function set command sequence + command(LCD_FUNCTIONSET | _displayfunction); + delay(5); // Maple mod + //delayMicroseconds(4500); // wait more than 4.1ms + + // second try + command(LCD_FUNCTIONSET | _displayfunction); + delay(1); // Maple mod + //delayMicroseconds(150); + + // third go + command(LCD_FUNCTIONSET | _displayfunction); + } + + // finally, set # lines, font size, etc. + command(LCD_FUNCTIONSET | _displayfunction); + + // turn the display on with no cursor or blinking default + _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; + display(); + + // clear it off + clear(); + + // Initialize to default text direction (for romance languages) + _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; + // set the entry mode + command(LCD_ENTRYMODESET | _displaymode); + +} + +/********** high level commands, for the user! */ +void LiquidCrystal::clear() +{ + command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero + delay(2); // Maple mod + //delayMicroseconds(2000); // this command takes a long time! +} + +void LiquidCrystal::home() +{ + command(LCD_RETURNHOME); // set cursor position to zero + delay(2); // Maple mod + //delayMicroseconds(2000); // this command takes a long time! +} + +void LiquidCrystal::setCursor(uint8 col, uint8 row) +{ + int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; + if ( row > _numlines ) { + row = _numlines-1; // we count rows starting w/0 + } + + command(LCD_SETDDRAMADDR | (col + row_offsets[row])); +} + +// Turn the display on/off (quickly) +void LiquidCrystal::noDisplay() { + _displaycontrol &= ~LCD_DISPLAYON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal::display() { + _displaycontrol |= LCD_DISPLAYON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// Turns the underline cursor on/off +void LiquidCrystal::noCursor() { + _displaycontrol &= ~LCD_CURSORON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal::cursor() { + _displaycontrol |= LCD_CURSORON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// Turn on and off the blinking cursor +void LiquidCrystal::noBlink() { + _displaycontrol &= ~LCD_BLINKON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal::blink() { + _displaycontrol |= LCD_BLINKON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// These commands scroll the display without changing the RAM +void LiquidCrystal::scrollDisplayLeft(void) { + command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); +} +void LiquidCrystal::scrollDisplayRight(void) { + command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); +} + +// This is for text that flows Left to Right +void LiquidCrystal::leftToRight(void) { + _displaymode |= LCD_ENTRYLEFT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This is for text that flows Right to Left +void LiquidCrystal::rightToLeft(void) { + _displaymode &= ~LCD_ENTRYLEFT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This will 'right justify' text from the cursor +void LiquidCrystal::autoscroll(void) { + _displaymode |= LCD_ENTRYSHIFTINCREMENT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This will 'left justify' text from the cursor +void LiquidCrystal::noAutoscroll(void) { + _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// Allows us to fill the first 8 CGRAM locations +// with custom characters +void LiquidCrystal::createChar(uint8 location, uint8 charmap[]) { + location &= 0x7; // we only have 8 locations 0-7 + command(LCD_SETCGRAMADDR | (location << 3)); + for (int i=0; i<8; i++) { + write(charmap[i]); + } +} + +/*********** mid level commands, for sending data/cmds */ + +inline void LiquidCrystal::command(uint8 value) { + send(value, LOW); +} + +inline size_t LiquidCrystal::write(uint8 value) { + send(value, HIGH); + return 1; +} + +/************ low level data pushing commands **********/ + +// write either command or data, with automatic 4/8-bit selection +void LiquidCrystal::send(uint8 value, uint8 mode) { + digitalWrite(_rs_pin, mode); + + // if there is a RW pin indicated, set it low to Write + if (_rw_pin != 255) { + digitalWrite(_rw_pin, LOW); + } + + if (_displayfunction & LCD_8BITMODE) { + write8bits(value); + } else { + write4bits(value>>4); + write4bits(value); + } +} + +void LiquidCrystal::pulseEnable(void) { + // _enable_pin should already be LOW (unless someone else messed + // with it), so don't sit around waiting for long. + digitalWrite(_enable_pin, LOW); + delayMicroseconds(1); + + // Enable pulse must be > 450 ns. Value chosen here according to + // the following threads: + // http://forums.leaflabs.com/topic.php?id=640 + // http://forums.leaflabs.com/topic.php?id=512 + gpio_toggle_bit(PIN_MAP[_enable_pin].gpio_device, PIN_MAP[_enable_pin].gpio_bit);// togglePin(_enable_pin); + delayMicroseconds(1); + gpio_toggle_bit(PIN_MAP[_enable_pin].gpio_device, PIN_MAP[_enable_pin].gpio_bit);// togglePin(_enable_pin); + + // Commands needs > 37us to settle. + delayMicroseconds(42); +} + +void LiquidCrystal::write4bits(uint8 value) { + for (int i = 0; i < 4; i++) { + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + pulseEnable(); +} + +void LiquidCrystal::write8bits(uint8 value) { + for (int i = 0; i < 8; i++) { + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + pulseEnable(); +} diff --git a/examples/Digital/Blink/Blink.ino b/examples/Digital/Blink/Blink.ino index deb0b0f..2730873 100644 --- a/examples/Digital/Blink/Blink.ino +++ b/examples/Digital/Blink/Blink.ino @@ -14,6 +14,6 @@ void setup() { } void loop() { - toggleLED(); // Turn the LED from off to on, or on to off + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off delay(1000); // Wait for 1 second (1000 milliseconds) } diff --git a/examples/Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino b/examples/Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino index 3dce0a5..78b4c5e 100644 --- a/examples/Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino +++ b/examples/Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino @@ -1,37 +1,37 @@ -/* - Blink without delay - - Turns on and off the built-in light emitting diode (LED), without - using the delay() function. This means that other code can run at - the same time without being interrupted by the LED code. - - created 2005 - by David A. Mellis - modified 17 Jun 2009 - by Tom Igoe - modified for Maple 27 May 2011 - by Marti Bolivar -*/ - -// Variables: -int previousMillis = 0; // will store the last time the LED was updated -int interval = 1000; // interval at which to blink (in milliseconds) - -void setup() { - // Set up the built-in LED pin as output: - pinMode(BOARD_LED_PIN, OUTPUT); -} - -void loop() { - // Check to see if it's time to blink the LED; that is, if the - // difference between the current time and last time we blinked - // the LED is bigger than the interval at which we want to blink - // the LED. - if (millis() - previousMillis > interval) { - // Save the last time you blinked the LED - previousMillis = millis(); - - // If the LED is off, turn it on, and vice-versa: - toggleLED(); - } -} +/* + Blink without delay + + Turns on and off the built-in light emitting diode (LED), without + using the delay() function. This means that other code can run at + the same time without being interrupted by the LED code. + + created 2005 + by David A. Mellis + modified 17 Jun 2009 + by Tom Igoe + modified for Maple 27 May 2011 + by Marti Bolivar +*/ + +// Variables: +int previousMillis = 0; // will store the last time the LED was updated +int interval = 1000; // interval at which to blink (in milliseconds) + +void setup() { + // Set up the built-in LED pin as output: + pinMode(BOARD_LED_PIN, OUTPUT); +} + +void loop() { + // Check to see if it's time to blink the LED; that is, if the + // difference between the current time and last time we blinked + // the LED is bigger than the interval at which we want to blink + // the LED. + if (millis() - previousMillis > interval) { + // Save the last time you blinked the LED + previousMillis = millis(); + + // If the LED is off, turn it on, and vice-versa: + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + } +} diff --git a/examples/Digital/Button/Button.ino b/examples/Digital/Button/Button.ino index 2c09deb..d438e4c 100644 --- a/examples/Digital/Button/Button.ino +++ b/examples/Digital/Button/Button.ino @@ -15,10 +15,22 @@ void setup() { pinMode(BOARD_BUTTON_PIN, INPUT); } +#define BUTTON_DEBOUNCE_DELAY 1 +uint8 isButtonPressed(uint8 pin=BOARD_BUTTON_PIN, + uint32 pressedLevel=BOARD_BUTTON_PRESSED_LEVEL) { + if (digitalRead(pin) == pressedLevel) { + delay(BUTTON_DEBOUNCE_DELAY); + while (digitalRead(pin) == pressedLevel) + ; + return true; + } + return false; +} + void loop() { // Check if the button is pressed. if (isButtonPressed()) { // If so, turn the LED from on to off, or from off to on: - toggleLED(); + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off } -} +} diff --git a/examples/Maple/InteractiveTest/InteractiveTest.ino b/examples/Maple/InteractiveTest/InteractiveTest.ino index b3b77b1..1f0460f 100644 --- a/examples/Maple/InteractiveTest/InteractiveTest.ino +++ b/examples/Maple/InteractiveTest/InteractiveTest.ino @@ -1,725 +1,725 @@ -/* - Interactive Test Session for LeafLabs Maple - - Useful for testing Maple features and troubleshooting. - Communicates over Serial. - - This code is released into the public domain. -*/ - -// ASCII escape character -#define ESC ((uint8)27) - -// Default USART baud rate -#define BAUD 9600 - -uint8 gpio_state[BOARD_NR_GPIO_PINS]; - -const char* dummy_data = ("qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-=" - "qwertyuiopasdfghjklzxcvbnm,./1234567890"); - -// -- setup() and loop() ------------------------------------------------------ - -void setup() { - Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor - // Set up the LED to blink - pinMode(BOARD_LED_PIN, OUTPUT); - - // Start up the serial ports - Serial1.begin(BAUD); - Serial2.begin(BAUD); - Serial3.begin(BAUD); - - // Send a message out over Serial interface - Serial.println(" "); - Serial.println(" __ __ _ _"); - Serial.println(" | \\/ | __ _ _ __ | | ___| |"); - Serial.println(" | |\\/| |/ _` | '_ \\| |/ _ \\ |"); - Serial.println(" | | | | (_| | |_) | | __/_|"); - Serial.println(" |_| |_|\\__,_| .__/|_|\\___(_)"); - Serial.println(" |_|"); - Serial.println(" by leaflabs"); - Serial.println(""); - Serial.println(""); - Serial.println("Maple interactive test program (type '?' for help)"); - Serial.println("----------------------------------------------------------"); - Serial.print("> "); - -} - -void loop () { - toggleLED(); - delay(100); - - while (Serial.available()) { - uint8 input = Serial.read(); - Serial.println(input); - - switch(input) { - case '\r': - break; - - case ' ': - Serial.println("spacebar, nice!"); - break; - - case '?': - case 'h': - cmd_print_help(); - break; - - case 'u': - Serial.println("Hello World!"); - break; - - case 'w': - Serial1.println("Hello World!"); - Serial2.println("Hello World!"); - Serial3.println("Hello World!"); - break; - - case 'm': - cmd_serial1_serial3(); - break; - - case '.': - while (!Serial.available()) { - Serial1.print("."); - Serial2.print("."); - Serial3.print("."); - Serial.print("."); - } - break; - - case 'n': - cmd_adc_stats(); - break; - - case 'N': - cmd_stressful_adc_stats(); - break; - - case 'e': - cmd_everything(); - break; - - case 'W': - while (!Serial.available()) { - Serial1.print(dummy_data); - Serial2.print(dummy_data); - Serial3.print(dummy_data); - } - break; - - case 'U': - Serial.println("Dumping data to USB. Press any key."); - while (!Serial.available()) { - Serial.print(dummy_data); - } - break; - - case 'g': - cmd_sequential_gpio_writes(); - break; - - case 'G': - cmd_gpio_toggling(); - break; - - case 'f': - Serial.println("Wiggling D4 as fast as possible in bursts. " - "Press any key."); - pinMode(4, OUTPUT); - while (!Serial.available()) { - fast_gpio(4); - delay(1); - } - break; - - case 'p': - cmd_sequential_pwm_test(); - break; - - case '_': - Serial.println("Delaying for 5 seconds..."); - delay(5000); - break; - - // Be sure to update cmd_print_help() if you implement these: - - case 't': // TODO - Serial.println("Unimplemented."); - break; - - case 'T': // TODO - Serial.println("Unimplemented."); - break; - - case 's': - cmd_servo_sweep(); - break; - - case 'd': - Serial.println("Pulling down D4, D22. Press any key."); - pinMode(22, INPUT_PULLDOWN); - pinMode(4, INPUT_PULLDOWN); - while (!Serial.available()) { - continue; - } - Serial.println("Pulling up D4, D22. Press any key."); - pinMode(22, INPUT_PULLUP); - pinMode(4, INPUT_PULLUP); - while (!Serial.available()) { - continue; - } - Serial.read(); - pinMode(4, OUTPUT); - break; - - // Be sure to update cmd_print_help() if you implement these: - - case 'i': // TODO - Serial.println("Unimplemented."); - break; - - case 'I': // TODO - Serial.println("Unimplemented."); - break; - - case 'r': - cmd_gpio_monitoring(); - break; - - case 'a': - cmd_sequential_adc_reads(); - break; - - case 'b': - cmd_board_info(); - break; - - case '+': - cmd_gpio_qa(); - break; - - default: // ------------------------------- - Serial.print("Unexpected: "); - Serial.print(input); - Serial.println(", press h for help."); - } - - Serial.print("> "); - } -} - -// -- Commands ---------------------------------------------------------------- - -void cmd_print_help(void) { - Serial.println(""); - Serial.println("Command Listing"); - Serial.println("\t?: print this menu"); - Serial.println("\th: print this menu"); - Serial.println("\tw: print Hello World on all 3 USARTS"); - Serial.println("\tn: measure noise and do statistics"); - Serial.println("\tN: measure noise and do statistics with background stuff"); - Serial.println("\ta: show realtime ADC info"); - Serial.println("\t.: echo '.' until new input"); - Serial.println("\tu: print Hello World on USB"); - Serial.println("\t_: do as little as possible for a couple seconds (delay)"); - Serial.println("\tp: test all PWM channels sequentially"); - Serial.println("\tW: dump data as fast as possible on all 3 USARTS"); - Serial.println("\tU: dump data as fast as possible on USB"); - Serial.println("\tg: toggle GPIOs sequentially"); - Serial.println("\tG: toggle GPIOs at the same time"); - Serial.println("\tf: toggle pin 4 as fast as possible in bursts"); - Serial.println("\tr: monitor and print GPIO status changes"); - Serial.println("\ts: output a sweeping servo PWM on all PWM channels"); - Serial.println("\tm: output data on USART1 and USART3 with various rates"); - Serial.println("\tb: print information about the board."); - Serial.println("\t+: test shield mode (for quality assurance testing)"); - - Serial.println("Unimplemented:"); - Serial.println("\te: do everything all at once until new input"); - Serial.println("\tt: output a 1khz squarewave on all GPIOs"); - Serial.println("\tT: output a 1hz squarewave on all GPIOs"); - Serial.println("\ti: print out a bunch of info about system state"); - Serial.println("\tI: print out status of all headers"); -} - -void cmd_adc_stats(void) { - Serial.println("Taking ADC noise stats."); - digitalWrite(BOARD_LED_PIN, 0); - for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) { - delay(5); - measure_adc_noise(boardADCPins[i]); - } -} - -void cmd_stressful_adc_stats(void) { - Serial.println("Taking ADC noise stats under duress."); - - for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) { - for (uint32 j = 0; j < BOARD_NR_PWM_PINS; j++) { - if (boardADCPins[i] != boardPWMPins[j]) { - pinMode(boardPWMPins[j], PWM); - pwmWrite(boardPWMPins[j], 1000 + i); - } - } - - Serial1.print(dummy_data); - - measure_adc_noise(boardADCPins[i]); - - for (uint32 j = 0; j < BOARD_NR_PWM_PINS; j++) { - if (boardADCPins[i] != boardPWMPins[j]) { - pinMode(boardPWMPins[j], OUTPUT); - digitalWrite(boardPWMPins[j], LOW); - } - } - } -} - -void cmd_everything(void) { // TODO - // Be sure to update cmd_print_help() if you implement this. - - // print to usart - // print to usb - // toggle gpios - // enable pwm - Serial.println("Unimplemented."); -} - -void cmd_serial1_serial3(void) { - HardwareSerial *serial_1_and_3[] = {&Serial1, &Serial3}; - - Serial.println("Testing 57600 baud on USART1 and USART3. " - "Press any key to stop."); - usart_baud_test(serial_1_and_3, 2, 57600); - Serial.read(); - - Serial.println("Testing 115200 baud on USART1 and USART3. " - "Press any key to stop."); - usart_baud_test(serial_1_and_3, 2, 115200); - Serial.read(); - - Serial.println("Testing 9600 baud on USART1 and USART3. " - "Press any key to stop."); - usart_baud_test(serial_1_and_3, 2, 9600); - Serial.read(); - - Serial.println("Resetting USART1 and USART3..."); - Serial1.begin(BAUD); - Serial3.begin(BAUD); -} - -void cmd_gpio_monitoring(void) { - Serial.println("Monitoring pin state changes. Press any key to stop."); - - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - pinMode(i, INPUT_PULLDOWN); - gpio_state[i] = (uint8)digitalRead(i); - } - - while (!Serial.available()) { - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - - uint8 current_state = (uint8)digitalRead(i); - if (current_state != gpio_state[i]) { - Serial.print("State change on pin "); - Serial.print(i, DEC); - if (current_state) { - Serial.println(":\tHIGH"); - } else { - Serial.println(":\tLOW"); - } - gpio_state[i] = current_state; - } - } - } - - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - pinMode(i, OUTPUT); - } -} - -void cmd_sequential_adc_reads(void) { - Serial.print("Sequentially reading most ADC ports."); - Serial.println("Press any key for next port, or ESC to stop."); - - for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) { - if (boardUsesPin(i)) - continue; - - Serial.print("Reading pin "); - Serial.print(boardADCPins[i], DEC); - Serial.println("..."); - pinMode(boardADCPins[i], INPUT_ANALOG); - while (!Serial.available()) { - int sample = analogRead(boardADCPins[i]); - Serial.print(boardADCPins[i], DEC); - Serial.print("\t"); - Serial.print(sample, DEC); - Serial.print("\t"); - Serial.print("|"); - for (int j = 0; j < 4096; j += 100) { - if (sample >= j) { - Serial.print("#"); - } else { - Serial.print(" "); - } - } - Serial.print("| "); - for (int j = 0; j < 12; j++) { - if (sample & (1 << (11 - j))) { - Serial.print("1"); - } else { - Serial.print("0"); - } - } - Serial.println(""); - } - pinMode(boardADCPins[i], OUTPUT); - digitalWrite(boardADCPins[i], 0); - if (Serial.read() == ESC) - break; - } -} - -bool test_single_pin_is_high(int high_pin, const char* err_msg) { - bool ok = true; - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) continue; - - if (digitalRead(i) == HIGH && i != high_pin) { - Serial.println(); - Serial.print("\t*** FAILURE! pin "); - Serial.print(i, DEC); - Serial.print(' '); - Serial.println(err_msg); - ok = false; - } - } - return ok; -} - -bool wait_for_low_transition(uint8 pin) { - uint32 start = millis(); - while (millis() - start < 2000) { - if (digitalRead(pin) == LOW) { - return true; - } - } - return false; -} - -void cmd_gpio_qa(void) { - bool all_pins_ok = true; - const int not_a_pin = -1; - Serial.println("Doing QA testing for unused GPIO pins."); - - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) continue; - - pinMode(i, INPUT); - } - - Serial.println("Waiting to start."); - ASSERT(!boardUsesPin(0)); - while (digitalRead(0) == LOW) continue; - - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) { - Serial.print("Skipping pin "); - Serial.println(i, DEC); - continue; - } - bool pin_ok = true; - Serial.print("Checking pin "); - Serial.print(i, DEC); - while (digitalRead(i) == LOW) continue; - - pin_ok = pin_ok && test_single_pin_is_high(i, "is also HIGH"); - - if (!wait_for_low_transition(i)) { - Serial.println("Transition to low timed out; something is " - "very wrong. Aborting test."); - return; - } - - pin_ok = pin_ok && test_single_pin_is_high(not_a_pin, "is still HIGH"); - - if (pin_ok) { - Serial.println(": ok"); - } - - all_pins_ok = all_pins_ok && pin_ok; - } - - if (all_pins_ok) { - Serial.println("Finished; test passes."); - } else { - Serial.println("**** TEST FAILS *****"); - } - - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) continue; - - pinMode(i, OUTPUT); - digitalWrite(i, LOW); - gpio_state[i] = 0; - } -} - -void cmd_sequential_gpio_writes(void) { - Serial.println("Sequentially toggling all unused pins. " - "Press any key for next pin, ESC to stop."); - - for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - - Serial.print("Toggling pin "); - Serial.print((int)i, DEC); - Serial.println("..."); - - pinMode(i, OUTPUT); - do { - togglePin(i); - } while (!Serial.available()); - - digitalWrite(i, LOW); - if (Serial.read() == ESC) - break; - } -} - -void cmd_gpio_toggling(void) { - Serial.println("Toggling all unused pins simultaneously. " - "Press any key to stop."); - - for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - pinMode(i, OUTPUT); - } - - while (!Serial.available()) { - for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - togglePin(i); - } - } - - for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - digitalWrite(i, LOW); - } -} - -void cmd_sequential_pwm_test(void) { - Serial.println("Sequentially testing PWM on all unused pins. " - "Press any key for next pin, ESC to stop."); - - for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { - if (boardUsesPin(i)) - continue; - - Serial.print("PWM out on header D"); - Serial.print(boardPWMPins[i], DEC); - Serial.println("..."); - pinMode(boardPWMPins[i], PWM); - pwmWrite(boardPWMPins[i], 16000); - - while (!Serial.available()) { - delay(10); - } - - pinMode(boardPWMPins[i], OUTPUT); - digitalWrite(boardPWMPins[i], 0); - if (Serial.read() == ESC) - break; - } -} - -void cmd_servo_sweep(void) { - Serial.println("Testing all PWM headers with a servo sweep. " - "Press any key to stop."); - Serial.println(); - - disable_usarts(); - init_all_timers(21); - - for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { - if (boardUsesPin(i)) - continue; - pinMode(boardPWMPins[i], PWM); - pwmWrite(boardPWMPins[i], 4000); - } - - // 1.25ms = 4096counts = 0deg - // 1.50ms = 4915counts = 90deg - // 1.75ms = 5734counts = 180deg - int rate = 4096; - while (!Serial.available()) { - rate += 20; - if (rate > 5734) - rate = 4096; - for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { - if (boardUsesPin(i)) - continue; - pwmWrite(boardPWMPins[i], rate); - } - delay(20); - } - - for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { - if (boardUsesPin(i)) - continue; - pinMode(boardPWMPins[i], OUTPUT); - } - init_all_timers(1); - enable_usarts(); -} - -void cmd_board_info(void) { // TODO print more information - Serial.println("Board information"); - Serial.println("================="); - - Serial.print("* Clock speed (cycles/us): "); - Serial.println(CYCLES_PER_MICROSECOND); - - Serial.print("* BOARD_LED_PIN: "); - Serial.println(BOARD_LED_PIN); - - Serial.print("* BOARD_BUTTON_PIN: "); - Serial.println(BOARD_BUTTON_PIN); - - Serial.print("* GPIO information (BOARD_NR_GPIO_PINS = "); - Serial.print(BOARD_NR_GPIO_PINS); - Serial.println("):"); - print_board_array("ADC pins", boardADCPins, BOARD_NR_ADC_PINS); - print_board_array("PWM pins", boardPWMPins, BOARD_NR_PWM_PINS); - print_board_array("Used pins", boardUsedPins, BOARD_NR_USED_PINS); -} - -// -- Helper functions -------------------------------------------------------- - -void measure_adc_noise(uint8 pin) { - uint16 data[100]; - float mean = 0; - float delta = 0; - float M2 = 0; - pinMode(pin, INPUT_ANALOG); - - // Variance algorithm from Welford, via Knuth, by way of Wikipedia: - // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm - for (int i = 0; i < 100; i++) { - data[i] = analogRead(pin); - delta = data[i] - mean; - mean = mean + delta / (i + 1); - M2 = M2 + delta * (data[i] - mean); - } - - Serial.print("header: D"); - Serial.print(pin, DEC); - Serial.print("\tn: "); - Serial.print(100, DEC); - Serial.print("\tmean: "); - Serial.print(mean); - Serial.print("\tvariance: "); - Serial.println(M2 / 99.0); - pinMode(pin, OUTPUT); -} - -void fast_gpio(int maple_pin) { - gpio_dev *dev = PIN_MAP[maple_pin].gpio_device; - uint32 bit = PIN_MAP[maple_pin].gpio_bit; - - gpio_write_bit(dev, bit, 1); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); - gpio_toggle_bit(dev, bit); -} - -void usart_baud_test(HardwareSerial **serials, int n, unsigned baud) { - for (int i = 0; i < n; i++) { - serials[i]->begin(baud); - } - while (!Serial.available()) { - for (int i = 0; i < n; i++) { - serials[i]->println(dummy_data); - if (serials[i]->available()) { - serials[i]->println(serials[i]->read()); - delay(1000); - } - } - } -} - -static uint16 init_all_timers_prescale = 0; - -static void set_prescale(timer_dev *dev) { - timer_set_prescaler(dev, init_all_timers_prescale); -} - -void init_all_timers(uint16 prescale) { - init_all_timers_prescale = prescale; - timer_foreach(set_prescale); -} - -void enable_usarts(void) { - // FIXME generalize after USART refactor - Serial1.begin(BAUD); - Serial2.begin(BAUD); - Serial3.begin(BAUD); -} - -void disable_usarts(void) { - // FIXME generalize after USART refactor - Serial1.end(); - Serial2.end(); - Serial3.end(); -} - -void print_board_array(const char* msg, const uint8 arr[], int len) { - Serial.print("\t"); - Serial.print(msg); - Serial.print(" ("); - Serial.print(len); - Serial.print("): "); - for (int i = 0; i < len; i++) { - Serial.print(arr[i], DEC); - if (i < len - 1) Serial.print(", "); - } - Serial.println(); -} +/* + Interactive Test Session for LeafLabs Maple + + Useful for testing Maple features and troubleshooting. + Communicates over Serial. + + This code is released into the public domain. +*/ + +// ASCII escape character +#define ESC ((uint8)27) + +// Default USART baud rate +#define BAUD 9600 + +uint8 gpio_state[BOARD_NR_GPIO_PINS]; + +const char* dummy_data = ("qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-=" + "qwertyuiopasdfghjklzxcvbnm,./1234567890"); + +// -- setup() and loop() ------------------------------------------------------ + +void setup() { + Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor + // Set up the LED to blink + pinMode(BOARD_LED_PIN, OUTPUT); + + // Start up the serial ports + Serial1.begin(BAUD); + Serial2.begin(BAUD); + Serial3.begin(BAUD); + + // Send a message out over Serial interface + Serial.println(" "); + Serial.println(" __ __ _ _"); + Serial.println(" | \\/ | __ _ _ __ | | ___| |"); + Serial.println(" | |\\/| |/ _` | '_ \\| |/ _ \\ |"); + Serial.println(" | | | | (_| | |_) | | __/_|"); + Serial.println(" |_| |_|\\__,_| .__/|_|\\___(_)"); + Serial.println(" |_|"); + Serial.println(" by leaflabs"); + Serial.println(""); + Serial.println(""); + Serial.println("Maple interactive test program (type '?' for help)"); + Serial.println("----------------------------------------------------------"); + Serial.print("> "); + +} + +void loop () { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); + + while (Serial.available()) { + uint8 input = Serial.read(); + Serial.println(input); + + switch(input) { + case '\r': + break; + + case ' ': + Serial.println("spacebar, nice!"); + break; + + case '?': + case 'h': + cmd_print_help(); + break; + + case 'u': + Serial.println("Hello World!"); + break; + + case 'w': + Serial1.println("Hello World!"); + Serial2.println("Hello World!"); + Serial3.println("Hello World!"); + break; + + case 'm': + cmd_serial1_serial3(); + break; + + case '.': + while (!Serial.available()) { + Serial1.print("."); + Serial2.print("."); + Serial3.print("."); + Serial.print("."); + } + break; + + case 'n': + cmd_adc_stats(); + break; + + case 'N': + cmd_stressful_adc_stats(); + break; + + case 'e': + cmd_everything(); + break; + + case 'W': + while (!Serial.available()) { + Serial1.print(dummy_data); + Serial2.print(dummy_data); + Serial3.print(dummy_data); + } + break; + + case 'U': + Serial.println("Dumping data to USB. Press any key."); + while (!Serial.available()) { + Serial.print(dummy_data); + } + break; + + case 'g': + cmd_sequential_gpio_writes(); + break; + + case 'G': + cmd_gpio_toggling(); + break; + + case 'f': + Serial.println("Wiggling D4 as fast as possible in bursts. " + "Press any key."); + pinMode(4, OUTPUT); + while (!Serial.available()) { + fast_gpio(4); + delay(1); + } + break; + + case 'p': + cmd_sequential_pwm_test(); + break; + + case '_': + Serial.println("Delaying for 5 seconds..."); + delay(5000); + break; + + // Be sure to update cmd_print_help() if you implement these: + + case 't': // TODO + Serial.println("Unimplemented."); + break; + + case 'T': // TODO + Serial.println("Unimplemented."); + break; + + case 's': + cmd_servo_sweep(); + break; + + case 'd': + Serial.println("Pulling down D4, D22. Press any key."); + pinMode(22, INPUT_PULLDOWN); + pinMode(4, INPUT_PULLDOWN); + while (!Serial.available()) { + continue; + } + Serial.println("Pulling up D4, D22. Press any key."); + pinMode(22, INPUT_PULLUP); + pinMode(4, INPUT_PULLUP); + while (!Serial.available()) { + continue; + } + Serial.read(); + pinMode(4, OUTPUT); + break; + + // Be sure to update cmd_print_help() if you implement these: + + case 'i': // TODO + Serial.println("Unimplemented."); + break; + + case 'I': // TODO + Serial.println("Unimplemented."); + break; + + case 'r': + cmd_gpio_monitoring(); + break; + + case 'a': + cmd_sequential_adc_reads(); + break; + + case 'b': + cmd_board_info(); + break; + + case '+': + cmd_gpio_qa(); + break; + + default: // ------------------------------- + Serial.print("Unexpected: "); + Serial.print(input); + Serial.println(", press h for help."); + } + + Serial.print("> "); + } +} + +// -- Commands ---------------------------------------------------------------- + +void cmd_print_help(void) { + Serial.println(""); + Serial.println("Command Listing"); + Serial.println("\t?: print this menu"); + Serial.println("\th: print this menu"); + Serial.println("\tw: print Hello World on all 3 USARTS"); + Serial.println("\tn: measure noise and do statistics"); + Serial.println("\tN: measure noise and do statistics with background stuff"); + Serial.println("\ta: show realtime ADC info"); + Serial.println("\t.: echo '.' until new input"); + Serial.println("\tu: print Hello World on USB"); + Serial.println("\t_: do as little as possible for a couple seconds (delay)"); + Serial.println("\tp: test all PWM channels sequentially"); + Serial.println("\tW: dump data as fast as possible on all 3 USARTS"); + Serial.println("\tU: dump data as fast as possible on USB"); + Serial.println("\tg: toggle GPIOs sequentially"); + Serial.println("\tG: toggle GPIOs at the same time"); + Serial.println("\tf: toggle pin 4 as fast as possible in bursts"); + Serial.println("\tr: monitor and print GPIO status changes"); + Serial.println("\ts: output a sweeping servo PWM on all PWM channels"); + Serial.println("\tm: output data on USART1 and USART3 with various rates"); + Serial.println("\tb: print information about the board."); + Serial.println("\t+: test shield mode (for quality assurance testing)"); + + Serial.println("Unimplemented:"); + Serial.println("\te: do everything all at once until new input"); + Serial.println("\tt: output a 1khz squarewave on all GPIOs"); + Serial.println("\tT: output a 1hz squarewave on all GPIOs"); + Serial.println("\ti: print out a bunch of info about system state"); + Serial.println("\tI: print out status of all headers"); +} + +void cmd_adc_stats(void) { + Serial.println("Taking ADC noise stats."); + digitalWrite(BOARD_LED_PIN, 0); + for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) { + delay(5); + measure_adc_noise(boardADCPins[i]); + } +} + +void cmd_stressful_adc_stats(void) { + Serial.println("Taking ADC noise stats under duress."); + + for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) { + for (uint32 j = 0; j < BOARD_NR_PWM_PINS; j++) { + if (boardADCPins[i] != boardPWMPins[j]) { + pinMode(boardPWMPins[j], PWM); + pwmWrite(boardPWMPins[j], 1000 + i); + } + } + + Serial1.print(dummy_data); + + measure_adc_noise(boardADCPins[i]); + + for (uint32 j = 0; j < BOARD_NR_PWM_PINS; j++) { + if (boardADCPins[i] != boardPWMPins[j]) { + pinMode(boardPWMPins[j], OUTPUT); + digitalWrite(boardPWMPins[j], LOW); + } + } + } +} + +void cmd_everything(void) { // TODO + // Be sure to update cmd_print_help() if you implement this. + + // print to usart + // print to usb + // toggle gpios + // enable pwm + Serial.println("Unimplemented."); +} + +void cmd_serial1_serial3(void) { + HardwareSerial *serial_1_and_3[] = {&Serial1, &Serial3}; + + Serial.println("Testing 57600 baud on USART1 and USART3. " + "Press any key to stop."); + usart_baud_test(serial_1_and_3, 2, 57600); + Serial.read(); + + Serial.println("Testing 115200 baud on USART1 and USART3. " + "Press any key to stop."); + usart_baud_test(serial_1_and_3, 2, 115200); + Serial.read(); + + Serial.println("Testing 9600 baud on USART1 and USART3. " + "Press any key to stop."); + usart_baud_test(serial_1_and_3, 2, 9600); + Serial.read(); + + Serial.println("Resetting USART1 and USART3..."); + Serial1.begin(BAUD); + Serial3.begin(BAUD); +} + +void cmd_gpio_monitoring(void) { + Serial.println("Monitoring pin state changes. Press any key to stop."); + + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + pinMode(i, INPUT_PULLDOWN); + gpio_state[i] = (uint8)digitalRead(i); + } + + while (!Serial.available()) { + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + + uint8 current_state = (uint8)digitalRead(i); + if (current_state != gpio_state[i]) { + Serial.print("State change on pin "); + Serial.print(i, DEC); + if (current_state) { + Serial.println(":\tHIGH"); + } else { + Serial.println(":\tLOW"); + } + gpio_state[i] = current_state; + } + } + } + + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + pinMode(i, OUTPUT); + } +} + +void cmd_sequential_adc_reads(void) { + Serial.print("Sequentially reading most ADC ports."); + Serial.println("Press any key for next port, or ESC to stop."); + + for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) { + if (boardUsesPin(i)) + continue; + + Serial.print("Reading pin "); + Serial.print(boardADCPins[i], DEC); + Serial.println("..."); + pinMode(boardADCPins[i], INPUT_ANALOG); + while (!Serial.available()) { + int sample = analogRead(boardADCPins[i]); + Serial.print(boardADCPins[i], DEC); + Serial.print("\t"); + Serial.print(sample, DEC); + Serial.print("\t"); + Serial.print("|"); + for (int j = 0; j < 4096; j += 100) { + if (sample >= j) { + Serial.print("#"); + } else { + Serial.print(" "); + } + } + Serial.print("| "); + for (int j = 0; j < 12; j++) { + if (sample & (1 << (11 - j))) { + Serial.print("1"); + } else { + Serial.print("0"); + } + } + Serial.println(""); + } + pinMode(boardADCPins[i], OUTPUT); + digitalWrite(boardADCPins[i], 0); + if (Serial.read() == ESC) + break; + } +} + +bool test_single_pin_is_high(int high_pin, const char* err_msg) { + bool ok = true; + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) continue; + + if (digitalRead(i) == HIGH && i != high_pin) { + Serial.println(); + Serial.print("\t*** FAILURE! pin "); + Serial.print(i, DEC); + Serial.print(' '); + Serial.println(err_msg); + ok = false; + } + } + return ok; +} + +bool wait_for_low_transition(uint8 pin) { + uint32 start = millis(); + while (millis() - start < 2000) { + if (digitalRead(pin) == LOW) { + return true; + } + } + return false; +} + +void cmd_gpio_qa(void) { + bool all_pins_ok = true; + const int not_a_pin = -1; + Serial.println("Doing QA testing for unused GPIO pins."); + + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) continue; + + pinMode(i, INPUT); + } + + Serial.println("Waiting to start."); + ASSERT(!boardUsesPin(0)); + while (digitalRead(0) == LOW) continue; + + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) { + Serial.print("Skipping pin "); + Serial.println(i, DEC); + continue; + } + bool pin_ok = true; + Serial.print("Checking pin "); + Serial.print(i, DEC); + while (digitalRead(i) == LOW) continue; + + pin_ok = pin_ok && test_single_pin_is_high(i, "is also HIGH"); + + if (!wait_for_low_transition(i)) { + Serial.println("Transition to low timed out; something is " + "very wrong. Aborting test."); + return; + } + + pin_ok = pin_ok && test_single_pin_is_high(not_a_pin, "is still HIGH"); + + if (pin_ok) { + Serial.println(": ok"); + } + + all_pins_ok = all_pins_ok && pin_ok; + } + + if (all_pins_ok) { + Serial.println("Finished; test passes."); + } else { + Serial.println("**** TEST FAILS *****"); + } + + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) continue; + + pinMode(i, OUTPUT); + digitalWrite(i, LOW); + gpio_state[i] = 0; + } +} + +void cmd_sequential_gpio_writes(void) { + Serial.println("Sequentially toggling all unused pins. " + "Press any key for next pin, ESC to stop."); + + for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + + Serial.print("Toggling pin "); + Serial.print((int)i, DEC); + Serial.println("..."); + + pinMode(i, OUTPUT); + do { + gpio_toggle_bit(PIN_MAP[i].gpio_device, PIN_MAP[i].gpio_bit); + } while (!Serial.available()); + + digitalWrite(i, LOW); + if (Serial.read() == ESC) + break; + } +} + +void cmd_gpio_toggling(void) { + Serial.println("Toggling all unused pins simultaneously. " + "Press any key to stop."); + + for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + pinMode(i, OUTPUT); + } + + while (!Serial.available()) { + for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + gpio_toggle_bit(PIN_MAP[i].gpio_device, PIN_MAP[i].gpio_bit); + } + } + + for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + digitalWrite(i, LOW); + } +} + +void cmd_sequential_pwm_test(void) { + Serial.println("Sequentially testing PWM on all unused pins. " + "Press any key for next pin, ESC to stop."); + + for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { + if (boardUsesPin(i)) + continue; + + Serial.print("PWM out on header D"); + Serial.print(boardPWMPins[i], DEC); + Serial.println("..."); + pinMode(boardPWMPins[i], PWM); + pwmWrite(boardPWMPins[i], 16000); + + while (!Serial.available()) { + delay(10); + } + + pinMode(boardPWMPins[i], OUTPUT); + digitalWrite(boardPWMPins[i], 0); + if (Serial.read() == ESC) + break; + } +} + +void cmd_servo_sweep(void) { + Serial.println("Testing all PWM headers with a servo sweep. " + "Press any key to stop."); + Serial.println(); + + disable_usarts(); + init_all_timers(21); + + for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { + if (boardUsesPin(i)) + continue; + pinMode(boardPWMPins[i], PWM); + pwmWrite(boardPWMPins[i], 4000); + } + + // 1.25ms = 4096counts = 0deg + // 1.50ms = 4915counts = 90deg + // 1.75ms = 5734counts = 180deg + int rate = 4096; + while (!Serial.available()) { + rate += 20; + if (rate > 5734) + rate = 4096; + for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { + if (boardUsesPin(i)) + continue; + pwmWrite(boardPWMPins[i], rate); + } + delay(20); + } + + for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) { + if (boardUsesPin(i)) + continue; + pinMode(boardPWMPins[i], OUTPUT); + } + init_all_timers(1); + enable_usarts(); +} + +void cmd_board_info(void) { // TODO print more information + Serial.println("Board information"); + Serial.println("================="); + + Serial.print("* Clock speed (cycles/us): "); + Serial.println(CYCLES_PER_MICROSECOND); + + Serial.print("* BOARD_LED_PIN: "); + Serial.println(BOARD_LED_PIN); + + Serial.print("* BOARD_BUTTON_PIN: "); + Serial.println(BOARD_BUTTON_PIN); + + Serial.print("* GPIO information (BOARD_NR_GPIO_PINS = "); + Serial.print(BOARD_NR_GPIO_PINS); + Serial.println("):"); + print_board_array("ADC pins", boardADCPins, BOARD_NR_ADC_PINS); + print_board_array("PWM pins", boardPWMPins, BOARD_NR_PWM_PINS); + print_board_array("Used pins", boardUsedPins, BOARD_NR_USED_PINS); +} + +// -- Helper functions -------------------------------------------------------- + +void measure_adc_noise(uint8 pin) { + uint16 data[100]; + float mean = 0; + float delta = 0; + float M2 = 0; + pinMode(pin, INPUT_ANALOG); + + // Variance algorithm from Welford, via Knuth, by way of Wikipedia: + // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm + for (int i = 0; i < 100; i++) { + data[i] = analogRead(pin); + delta = data[i] - mean; + mean = mean + delta / (i + 1); + M2 = M2 + delta * (data[i] - mean); + } + + Serial.print("header: D"); + Serial.print(pin, DEC); + Serial.print("\tn: "); + Serial.print(100, DEC); + Serial.print("\tmean: "); + Serial.print(mean); + Serial.print("\tvariance: "); + Serial.println(M2 / 99.0); + pinMode(pin, OUTPUT); +} + +void fast_gpio(int maple_pin) { + gpio_dev *dev = PIN_MAP[maple_pin].gpio_device; + uint32 bit = PIN_MAP[maple_pin].gpio_bit; + + gpio_write_bit(dev, bit, 1); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); + gpio_toggle_bit(dev, bit); +} + +void usart_baud_test(HardwareSerial **serials, int n, unsigned baud) { + for (int i = 0; i < n; i++) { + serials[i]->begin(baud); + } + while (!Serial.available()) { + for (int i = 0; i < n; i++) { + serials[i]->println(dummy_data); + if (serials[i]->available()) { + serials[i]->println(serials[i]->read()); + delay(1000); + } + } + } +} + +static uint16 init_all_timers_prescale = 0; + +static void set_prescale(timer_dev *dev) { + timer_set_prescaler(dev, init_all_timers_prescale); +} + +void init_all_timers(uint16 prescale) { + init_all_timers_prescale = prescale; + timer_foreach(set_prescale); +} + +void enable_usarts(void) { + // FIXME generalize after USART refactor + Serial1.begin(BAUD); + Serial2.begin(BAUD); + Serial3.begin(BAUD); +} + +void disable_usarts(void) { + // FIXME generalize after USART refactor + Serial1.end(); + Serial2.end(); + Serial3.end(); +} + +void print_board_array(const char* msg, const uint8 arr[], int len) { + Serial.print("\t"); + Serial.print(msg); + Serial.print(" ("); + Serial.print(len); + Serial.print("): "); + for (int i = 0; i < len; i++) { + Serial.print(arr[i], DEC); + if (i < len - 1) Serial.print(", "); + } + Serial.println(); +} diff --git a/examples/Maple/QASlave/QASlave.ino b/examples/Maple/QASlave/QASlave.ino index 9942cf3..b78190f 100644 --- a/examples/Maple/QASlave/QASlave.ino +++ b/examples/Maple/QASlave/QASlave.ino @@ -1,66 +1,66 @@ -/* - Slave mode for Quality Assurance test - - Used as follows: - - 1) Connect all non-used pins on the test board to their - corresponding pins on a board running InteractiveTest. - - 2) Connect a serial monitor to the InteractiveTest board and - enter "+" (a plus sign, without the quotes). - - This program pulses each unused pin in order, starting from pin 0. - The InteractiveTest "+" command detects these pulses, and makes - sure that no other pins change state at the same time. - - If you hold the button on the board running this program, the - pulses run slower. - - Useful as a simple test of functionality for GPIO pins. - */ - -#define INTER_TOGGLE_DELAY_NORMAL 5 -#define INTER_TOGGLE_DELAY_SLOW 80 - -void interToggleDelay(void); - -void setup() { - pinMode(BOARD_LED_PIN, OUTPUT); - pinMode(BOARD_BUTTON_PIN, INPUT); - - // All unused pins start out low. - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - pinMode(i, OUTPUT); - digitalWrite(i, LOW); - } -} - -void loop() { - toggleLED(); - delay(100); - toggleLED(); - - for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { - if (boardUsesPin(i)) - continue; - - // Bring just this pin high. - digitalWrite(i, HIGH); - // Give the master time to detect if any other pins also went high. - interToggleDelay(); - // Bring this pin back low again; all pins should now be low. - digitalWrite(i, LOW); - // Give the master time to detect if any pins are still high. - interToggleDelay(); - } -} - -void interToggleDelay(void) { - if (digitalRead(BOARD_BUTTON_PIN)) { // don't pay the debouncing time - delay(INTER_TOGGLE_DELAY_SLOW); - } else { - delay(INTER_TOGGLE_DELAY_NORMAL); - } -} +/* + Slave mode for Quality Assurance test + + Used as follows: + + 1) Connect all non-used pins on the test board to their + corresponding pins on a board running InteractiveTest. + + 2) Connect a serial monitor to the InteractiveTest board and + enter "+" (a plus sign, without the quotes). + + This program pulses each unused pin in order, starting from pin 0. + The InteractiveTest "+" command detects these pulses, and makes + sure that no other pins change state at the same time. + + If you hold the button on the board running this program, the + pulses run slower. + + Useful as a simple test of functionality for GPIO pins. + */ + +#define INTER_TOGGLE_DELAY_NORMAL 5 +#define INTER_TOGGLE_DELAY_SLOW 80 + +void interToggleDelay(void); + +void setup() { + pinMode(BOARD_LED_PIN, OUTPUT); + pinMode(BOARD_BUTTON_PIN, INPUT); + + // All unused pins start out low. + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + pinMode(i, OUTPUT); + digitalWrite(i, LOW); + } +} + +void loop() { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + + for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) { + if (boardUsesPin(i)) + continue; + + // Bring just this pin high. + digitalWrite(i, HIGH); + // Give the master time to detect if any other pins also went high. + interToggleDelay(); + // Bring this pin back low again; all pins should now be low. + digitalWrite(i, LOW); + // Give the master time to detect if any pins are still high. + interToggleDelay(); + } +} + +void interToggleDelay(void) { + if (digitalRead(BOARD_BUTTON_PIN)) { // don't pay the debouncing time + delay(INTER_TOGGLE_DELAY_SLOW); + } else { + delay(INTER_TOGGLE_DELAY_NORMAL); + } +} diff --git a/examples/MrBrunetteExamples/Blink/Blink.ino b/examples/MrBrunetteExamples/Blink/Blink.ino index 932d0d0..6d515a0 100644 --- a/examples/MrBrunetteExamples/Blink/Blink.ino +++ b/examples/MrBrunetteExamples/Blink/Blink.ino @@ -1,17 +1,17 @@ -/* - Blink: Turns on the built-in LED on for one second, then off for one second, repeatedly. - Arduino 1.6.0rc1 - Sketch uses 11,900 bytes (11%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,592 bytes of dynamic memory. - Ported to Maple from the Arduino example 27 May 2011 By Marti Bolivar -*/ - -void setup() { - // Set up the built-in LED pin as an output: - pinMode(BOARD_LED_PIN, OUTPUT); -} - -void loop() { - toggleLED(); // Turn the LED from off to on, or on to off - delay(1000); // Wait for 1 second (1000 milliseconds) -} +/* + Blink: Turns on the built-in LED on for one second, then off for one second, repeatedly. + Arduino 1.6.0rc1 + Sketch uses 11,900 bytes (11%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,592 bytes of dynamic memory. + Ported to Maple from the Arduino example 27 May 2011 By Marti Bolivar +*/ + +void setup() { + // Set up the built-in LED pin as an output: + pinMode(BOARD_LED_PIN, OUTPUT); +} + +void loop() { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(1000); // Wait for 1 second (1000 milliseconds) +} diff --git a/examples/MrBrunetteExamples/BlinkNcount/BlinkNcount.ino b/examples/MrBrunetteExamples/BlinkNcount/BlinkNcount.ino index b26b2ca..2981d9c 100644 --- a/examples/MrBrunetteExamples/BlinkNcount/BlinkNcount.ino +++ b/examples/MrBrunetteExamples/BlinkNcount/BlinkNcount.ino @@ -1,35 +1,35 @@ -/* - BlinkNcount for Maple Mini by m. ray burnette - Sketch uses 13,808 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,592 bytes of dynamic memory. - Turns on an LED on for one second, then off for one second, repeatedly. - Counts and displays the count on the attached serial monitor - This example code is in the public domain. - */ -int n = 0; - - -void setup() { - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - // Initialize virtual COM over USB on Maple Mini - Serial.begin(9600); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Blink LED & count Demo"); -} - -void loop() { - digitalWrite(BOARD_LED_PIN, HIGH); // set the LED on - delay(500); // wait for a second - digitalWrite(BOARD_LED_PIN, LOW); // set the LED off - Serial.print("Loop #: "); - n++; - Serial.println(n); - - delay(500); // wait -} +/* + BlinkNcount for Maple Mini by m. ray burnette + Sketch uses 13,808 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,592 bytes of dynamic memory. + Turns on an LED on for one second, then off for one second, repeatedly. + Counts and displays the count on the attached serial monitor + This example code is in the public domain. + */ +int n = 0; + + +void setup() { + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + // Initialize virtual COM over USB on Maple Mini + Serial.begin(9600); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Blink LED & count Demo"); +} + +void loop() { + digitalWrite(BOARD_LED_PIN, HIGH); // set the LED on + delay(500); // wait for a second + digitalWrite(BOARD_LED_PIN, LOW); // set the LED off + Serial.print("Loop #: "); + n++; + Serial.println(n); + + delay(500); // wait +} diff --git a/examples/MrBrunetteExamples/IntegerInput/IntegerInput.ino b/examples/MrBrunetteExamples/IntegerInput/IntegerInput.ino index e6f02a0..c6be14d 100644 --- a/examples/MrBrunetteExamples/IntegerInput/IntegerInput.ino +++ b/examples/MrBrunetteExamples/IntegerInput/IntegerInput.ino @@ -1,46 +1,46 @@ -/* - IntegerInput by m. Ray Burnette - PUBLIC DOMAIN EXAMPLE - Maple Mini: Compiled under Arduino 1.6.0rc1 - Sketch uses 15,624 bytes (14%) of program storage space. Maximum is 108,000 bytes. - Global variables use 3,704 bytes of dynamic memory. -*/ - -#define BAUD 9600 -#define timeoutPeriod 2147483647 // Long time... about 25 days - -int a; -int b; - -void setup() -{ - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - Serial.setTimeout(timeoutPeriod); // default is 1 second - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } -} - -void loop() -{ - Serial.println("Enter first integer: "); - a = Serial.parseInt(); - Serial.print("a = "); - Serial.println(a); - - - Serial.println("Enter second integer: "); - b = Serial.parseInt(); - Serial.print("b = "); - Serial.println(b); - - Serial.print("Sum a + b ="); - Serial.println( a + b); - Serial.print("Dif a - b ="); - Serial.println(a - b); -} - +/* + IntegerInput by m. Ray Burnette - PUBLIC DOMAIN EXAMPLE + Maple Mini: Compiled under Arduino 1.6.0rc1 + Sketch uses 15,624 bytes (14%) of program storage space. Maximum is 108,000 bytes. + Global variables use 3,704 bytes of dynamic memory. +*/ + +#define BAUD 9600 +#define timeoutPeriod 2147483647 // Long time... about 25 days + +int a; +int b; + +void setup() +{ + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + Serial.setTimeout(timeoutPeriod); // default is 1 second + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } +} + +void loop() +{ + Serial.println("Enter first integer: "); + a = Serial.parseInt(); + Serial.print("a = "); + Serial.println(a); + + + Serial.println("Enter second integer: "); + b = Serial.parseInt(); + Serial.print("b = "); + Serial.println(b); + + Serial.print("Sum a + b ="); + Serial.println( a + b); + Serial.print("Dif a - b ="); + Serial.println(a - b); +} + diff --git a/examples/MrBrunetteExamples/IntegerInput_FloatOutput/IntegerInput_FloatOutput.ino b/examples/MrBrunetteExamples/IntegerInput_FloatOutput/IntegerInput_FloatOutput.ino index fae768d..a0eaa0b 100644 --- a/examples/MrBrunetteExamples/IntegerInput_FloatOutput/IntegerInput_FloatOutput.ino +++ b/examples/MrBrunetteExamples/IntegerInput_FloatOutput/IntegerInput_FloatOutput.ino @@ -1,50 +1,50 @@ -/* - IntegerInput_FloatOutput by m. Ray Burnette - PUBLIC DOMAIN EXAMPLE - Maple Mini: Compiled under Arduino 1.6.0rc1 - Sketch uses 19,868 bytes (18%) of program storage space. Maximum is 108,000 bytes. - Global variables use 3,704 bytes of dynamic memory. -*/ - -#define BAUD 9600 -#define timeoutPeriod 2147483647 // Long var... about 25 days - -float a; -float b; - - -void setup() -{ - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - Serial.setTimeout(timeoutPeriod); // default is 1 second - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Integer Input - Floating Output"); - Serial.println("You may wish to try 355 and 113 (Pi)"); -} - -void loop() -{ - Serial.println("Enter first integer: "); - - a = Serial.parseInt(); - Serial.print("a = "); - Serial.println(a); - - Serial.println("Enter second integer: "); - - b = Serial.parseInt(); - Serial.print("b = "); - Serial.println(b); - - a = a / b; - Serial.print( "a/b = "); - Serial.println(a); - Serial.println(); -} - +/* + IntegerInput_FloatOutput by m. Ray Burnette - PUBLIC DOMAIN EXAMPLE + Maple Mini: Compiled under Arduino 1.6.0rc1 + Sketch uses 19,868 bytes (18%) of program storage space. Maximum is 108,000 bytes. + Global variables use 3,704 bytes of dynamic memory. +*/ + +#define BAUD 9600 +#define timeoutPeriod 2147483647 // Long var... about 25 days + +float a; +float b; + + +void setup() +{ + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + Serial.setTimeout(timeoutPeriod); // default is 1 second + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Integer Input - Floating Output"); + Serial.println("You may wish to try 355 and 113 (Pi)"); +} + +void loop() +{ + Serial.println("Enter first integer: "); + + a = Serial.parseInt(); + Serial.print("a = "); + Serial.println(a); + + Serial.println("Enter second integer: "); + + b = Serial.parseInt(); + Serial.print("b = "); + Serial.println(b); + + a = a / b; + Serial.print( "a/b = "); + Serial.println(a); + Serial.println(); +} + diff --git a/examples/MrBrunetteExamples/InternalTempSensor/InternalTempSensor.ino b/examples/MrBrunetteExamples/InternalTempSensor/InternalTempSensor.ino index 958b1b8..956021b 100644 --- a/examples/MrBrunetteExamples/InternalTempSensor/InternalTempSensor.ino +++ b/examples/MrBrunetteExamples/InternalTempSensor/InternalTempSensor.ino @@ -1,80 +1,80 @@ -/* - Re: http://leaflabs.com/docs/hardware - Arduino 1.6.0rc1 modifications by m. ray burnette - Sketch uses 31,608 bytes (29%) of program storage space. Maximum is 108,000 bytes. - Global variables use 3,752 bytes of dynamic memory. - PUBLIC DOMAIN EXAMPLE -*/ - -//#include -#include - -#define LED_PIN BOARD_LED_PIN -#define A_RANDOM_ANALOG_PIN 15 - -void setup_temperature_sensor() { - adc_reg_map *regs = ADC1->regs; - -// 3. Set the TSVREFE bit in the ADC control register 2 (ADC_CR2) to wake up the -// temperature sensor from power down mode. Do this first 'cause according to -// the Datasheet section 5.3.21 it takes from 4 to 10 uS to power up the sensor. - - regs->CR2 |= ADC_CR2_TSEREFE; - -// 2. Select a sample time of 17.1 μs -// set channel 16 sample time to 239.5 cycles -// 239.5 cycles of the ADC clock (72MHz/6=12MHz) is over 17.1us (about 20us), but no smaller -// sample time exceeds 17.1us. - - regs->SMPR1 = (0b111 << (3*6)); // set channel 16, the temp. sensor -} - -void setup(void) -{ -// Set up the LED to blink - pinMode(LED_PIN, OUTPUT); - -// set up an analog input. We want to test and make sure other analog reads don't screw this up. - pinMode(A_RANDOM_ANALOG_PIN, INPUT_ANALOG); - -// wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - -// init tem psensor - setup_temperature_sensor(); - -// announce start up - if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())) - Serial.println("Temp mon startup"); -} - -// -// once per second perform a standard analog read and read the temperature sensor -// the analog read is to test whether our messing with the temp sensor will screw up analog reads -// or visa versa. -// Also compute and display the time each operation takes. -// -void loop(void) -{ - uint16 vsense, alogpin; - uint32 t1, t2, t3; - char buf[64]; - - t1 = micros(); - alogpin = analogRead(A_RANDOM_ANALOG_PIN); - t2 = micros(); - vsense = adc_read(ADC1, 16); - t3 = micros(); - if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())) { - sprintf(buf,"%04x %08x %04x %08x" , vsense, t3-t2, alogpin, t2-t1); - Serial.println(buf); - } - toggleLED(); - delay(1000); -} - - +/* + Re: http://leaflabs.com/docs/hardware + Arduino 1.6.0rc1 modifications by m. ray burnette + Sketch uses 31,608 bytes (29%) of program storage space. Maximum is 108,000 bytes. + Global variables use 3,752 bytes of dynamic memory. + PUBLIC DOMAIN EXAMPLE +*/ + +//#include +#include + +#define LED_PIN BOARD_LED_PIN +#define A_RANDOM_ANALOG_PIN 15 + +void setup_temperature_sensor() { + adc_reg_map *regs = ADC1->regs; + +// 3. Set the TSVREFE bit in the ADC control register 2 (ADC_CR2) to wake up the +// temperature sensor from power down mode. Do this first 'cause according to +// the Datasheet section 5.3.21 it takes from 4 to 10 uS to power up the sensor. + + regs->CR2 |= ADC_CR2_TSEREFE; + +// 2. Select a sample time of 17.1 μs +// set channel 16 sample time to 239.5 cycles +// 239.5 cycles of the ADC clock (72MHz/6=12MHz) is over 17.1us (about 20us), but no smaller +// sample time exceeds 17.1us. + + regs->SMPR1 = (0b111 << (3*6)); // set channel 16, the temp. sensor +} + +void setup(void) +{ +// Set up the LED to blink + pinMode(LED_PIN, OUTPUT); + +// set up an analog input. We want to test and make sure other analog reads don't screw this up. + pinMode(A_RANDOM_ANALOG_PIN, INPUT_ANALOG); + +// wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + +// init tem psensor + setup_temperature_sensor(); + +// announce start up + if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())) + Serial.println("Temp mon startup"); +} + +// +// once per second perform a standard analog read and read the temperature sensor +// the analog read is to test whether our messing with the temp sensor will screw up analog reads +// or visa versa. +// Also compute and display the time each operation takes. +// +void loop(void) +{ + uint16 vsense, alogpin; + uint32 t1, t2, t3; + char buf[64]; + + t1 = micros(); + alogpin = analogRead(A_RANDOM_ANALOG_PIN); + t2 = micros(); + vsense = adc_read(ADC1, 16); + t3 = micros(); + if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())) { + sprintf(buf,"%04x %08x %04x %08x" , vsense, t3-t2, alogpin, t2-t1); + Serial.println(buf); + } + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(1000); +} + + diff --git a/examples/MrBrunetteExamples/PrimeNos/PrimeNos.ino b/examples/MrBrunetteExamples/PrimeNos/PrimeNos.ino index 41c0f2a..ef50d73 100644 --- a/examples/MrBrunetteExamples/PrimeNos/PrimeNos.ino +++ b/examples/MrBrunetteExamples/PrimeNos/PrimeNos.ino @@ -1,69 +1,69 @@ -/* - PrimeNos: by Nick Gammon - Maple Mini port m. ray burnette: Compiled under Arduino 1.6.0rc1 - Sketch uses 13,644 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,656 bytes of dynamic memory. - PUBLIC DOMAIN EXAMPLE -*/ - -#define BAUD 9600 - -// just add more primes to the prime table for larger search -// byte data type to save memory - use a larger datatype with prime table entries above 255 :) -byte primes[]={ - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, - 102, 107, 109, 113, 127, 131, 137 , 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, - 199, 211, 223, 227, 229, 233, 239, 241, 251 }; - -// if you change the datatype of primes array to int, change next line to -// "const int TopPrimeIndex = (sizeof(primes)/2) - 1;" - -const unsigned int TopPrimeIndex = sizeof(primes) - 1; -const unsigned long TopPrimeSquared = (long)primes[TopPrimeIndex] * (long)primes[TopPrimeIndex]; -int primeFlag; - - -void setup() -{ - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Prime Number Generator"); - Serial.print("Number of primes in prime table = "); - Serial.println(TopPrimeIndex); - Serial.println(); - Serial.print("Last prime in table = "); - Serial.println((unsigned int)primes[TopPrimeIndex]); - Serial.println(); - - Serial.print("Calculating primes through "); - Serial.println(TopPrimeSquared); - Serial.println(); - - -} -void loop() // run over and over again -{ - for (long x = 1; x < TopPrimeSquared; x+=2){ // skips even numbers, including 2, which is prime, but it makes algorithm tad faster - - for (long j=0; j < TopPrimeIndex; j++){ - primeFlag = true; - - if (x == primes[j]) break; - - if (x % primes[j] == 0){ // if the test number modolo (next number from prime table) == 0 - primeFlag = false; // then test number is not prime, bailout and check the next number - break; - } - } - if (primeFlag == true){ // found a prime - print it - Serial.println(x); - } - } -} +/* + PrimeNos: by Nick Gammon + Maple Mini port m. ray burnette: Compiled under Arduino 1.6.0rc1 + Sketch uses 13,644 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,656 bytes of dynamic memory. + PUBLIC DOMAIN EXAMPLE +*/ + +#define BAUD 9600 + +// just add more primes to the prime table for larger search +// byte data type to save memory - use a larger datatype with prime table entries above 255 :) +byte primes[]={ + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, + 102, 107, 109, 113, 127, 131, 137 , 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, + 199, 211, 223, 227, 229, 233, 239, 241, 251 }; + +// if you change the datatype of primes array to int, change next line to +// "const int TopPrimeIndex = (sizeof(primes)/2) - 1;" + +const unsigned int TopPrimeIndex = sizeof(primes) - 1; +const unsigned long TopPrimeSquared = (long)primes[TopPrimeIndex] * (long)primes[TopPrimeIndex]; +int primeFlag; + + +void setup() +{ + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Prime Number Generator"); + Serial.print("Number of primes in prime table = "); + Serial.println(TopPrimeIndex); + Serial.println(); + Serial.print("Last prime in table = "); + Serial.println((unsigned int)primes[TopPrimeIndex]); + Serial.println(); + + Serial.print("Calculating primes through "); + Serial.println(TopPrimeSquared); + Serial.println(); + + +} +void loop() // run over and over again +{ + for (long x = 1; x < TopPrimeSquared; x+=2){ // skips even numbers, including 2, which is prime, but it makes algorithm tad faster + + for (long j=0; j < TopPrimeIndex; j++){ + primeFlag = true; + + if (x == primes[j]) break; + + if (x % primes[j] == 0){ // if the test number modolo (next number from prime table) == 0 + primeFlag = false; // then test number is not prime, bailout and check the next number + break; + } + } + if (primeFlag == true){ // found a prime - print it + Serial.println(x); + } + } +} diff --git a/examples/MrBrunetteExamples/PrimeNos2/PrimeNos2.ino b/examples/MrBrunetteExamples/PrimeNos2/PrimeNos2.ino index 44efc76..50bd24f 100644 --- a/examples/MrBrunetteExamples/PrimeNos2/PrimeNos2.ino +++ b/examples/MrBrunetteExamples/PrimeNos2/PrimeNos2.ino @@ -1,41 +1,41 @@ -/* - PrimeNos3: by Nick Gammon - Maple Mini port m. ray burnette: Compiled under Arduino 1.6.0rc1 - Sketch uses 16,616 bytes (15%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,624 bytes of dynamic memory. - PUBLIC DOMAIN EXAMPLE -*/ - -#define BAUD 9600 -const int SHOW_EVERY = 500; // how often to echo a prime to the serial port -int candidate; -int found = 5; // Number we found -int count = found - 1; - - -void setup() { - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Prime Number Generator V2"); -} - -void loop() { - Serial.println("Prime numbers between 1 and 99999999 are:"); - Serial.println("2 \t"); - for (int i=3; i<99999999;i+=2) { - // This loop stops either when j*j>i or when i is divisible by j. - // The first condition means prime, the second, not prime. - int j=3; - for(;j*j<=i && i%j!=0; j+=2); // No loop body - - if (j*j>i) Serial.print(i);Serial.print( "\t"); - } - Serial.println("\r\n"); -} +/* + PrimeNos3: by Nick Gammon + Maple Mini port m. ray burnette: Compiled under Arduino 1.6.0rc1 + Sketch uses 16,616 bytes (15%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,624 bytes of dynamic memory. + PUBLIC DOMAIN EXAMPLE +*/ + +#define BAUD 9600 +const int SHOW_EVERY = 500; // how often to echo a prime to the serial port +int candidate; +int found = 5; // Number we found +int count = found - 1; + + +void setup() { + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Prime Number Generator V2"); +} + +void loop() { + Serial.println("Prime numbers between 1 and 99999999 are:"); + Serial.println("2 \t"); + for (int i=3; i<99999999;i+=2) { + // This loop stops either when j*j>i or when i is divisible by j. + // The first condition means prime, the second, not prime. + int j=3; + for(;j*j<=i && i%j!=0; j+=2); // No loop body + + if (j*j>i) Serial.print(i);Serial.print( "\t"); + } + Serial.println("\r\n"); +} diff --git a/examples/MrBrunetteExamples/PrimeNos3/PrimeNos3.ino b/examples/MrBrunetteExamples/PrimeNos3/PrimeNos3.ino index 05314a1..30762c5 100644 --- a/examples/MrBrunetteExamples/PrimeNos3/PrimeNos3.ino +++ b/examples/MrBrunetteExamples/PrimeNos3/PrimeNos3.ino @@ -1,41 +1,41 @@ -/* - PrimeNos3: by Nick Gammon - Maple Mini port m. ray burnette: Compiled under Arduino 1.6.0rc1 - Sketch uses 13,420 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,600 bytes of dynamic memory. - PUBLIC DOMAIN EXAMPLE -*/ - -#define BAUD 9600 -const int SHOW_EVERY = 500; // how often to echo a prime to the serial port -int candidate; -int found = 5; // Number we found -int count = found - 1; - - -void setup() { - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Prime Number Generator V2"); -} - -void loop() { - Serial.println("Prime numbers between 1 and 99999999 are:"); - Serial.println("2 \t"); - for (int i=3; i<99999999;i+=2) { - // This loop stops either when j*j>i or when i is divisible by j. - // The first condition means prime, the second, not prime. - int j=3; - for(;j*j<=i && i%j!=0; j+=2); // No loop body - - if (j*j>i) Serial.print(i);Serial.print( "\n\r"); - } - Serial.println("\r\n"); -} +/* + PrimeNos3: by Nick Gammon + Maple Mini port m. ray burnette: Compiled under Arduino 1.6.0rc1 + Sketch uses 13,420 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,600 bytes of dynamic memory. + PUBLIC DOMAIN EXAMPLE +*/ + +#define BAUD 9600 +const int SHOW_EVERY = 500; // how often to echo a prime to the serial port +int candidate; +int found = 5; // Number we found +int count = found - 1; + + +void setup() { + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Prime Number Generator V2"); +} + +void loop() { + Serial.println("Prime numbers between 1 and 99999999 are:"); + Serial.println("2 \t"); + for (int i=3; i<99999999;i+=2) { + // This loop stops either when j*j>i or when i is divisible by j. + // The first condition means prime, the second, not prime. + int j=3; + for(;j*j<=i && i%j!=0; j+=2); // No loop body + + if (j*j>i) Serial.print(i);Serial.print( "\n\r"); + } + Serial.println("\r\n"); +} diff --git a/examples/MrBrunetteExamples/Print_Binary/Print_Binary.ino b/examples/MrBrunetteExamples/Print_Binary/Print_Binary.ino index 895451a..7353b4a 100644 --- a/examples/MrBrunetteExamples/Print_Binary/Print_Binary.ino +++ b/examples/MrBrunetteExamples/Print_Binary/Print_Binary.ino @@ -1,66 +1,66 @@ -/* PRINT_BINARY - Arduino 1.6.0rc1 - Adapted to Maple Mini by m. ray burnette - Sketch uses 11,672 bytes (10%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,592 bytes of dynamic memory - Prints a positive integer in binary format with a fixed withdth - copyright, Peter H Anderson, Baltimore, MD, Nov, '07 - PUBLIC DOMAIN EXAMPLE -*/ - -#define BAUD 9600 - -void setup() -{ - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Print Binary Format"); -} - -void loop() -{ - while(1) - { - print_binary(1024+256+63, 12); - Serial.println(); - delay(1000); - - } -} - -void print_binary(int v, int num_places) -{ - int mask=0, n; - - for (n=1; n<=num_places; n++) - { - mask = (mask << 1) | 0x0001; - } - v = v & mask; // truncate v to specified number of places - - while(num_places) - { - - if (v & (0x0001 << num_places-1)) - { - Serial.print("1"); - } - else - { - Serial.print("0"); - } - - --num_places; - if(((num_places%4) == 0) && (num_places != 0)) - { - Serial.print("_"); - } - } -} - +/* PRINT_BINARY - Arduino 1.6.0rc1 + Adapted to Maple Mini by m. ray burnette + Sketch uses 11,672 bytes (10%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,592 bytes of dynamic memory + Prints a positive integer in binary format with a fixed withdth + copyright, Peter H Anderson, Baltimore, MD, Nov, '07 + PUBLIC DOMAIN EXAMPLE +*/ + +#define BAUD 9600 + +void setup() +{ + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Print Binary Format"); +} + +void loop() +{ + while(1) + { + print_binary(1024+256+63, 12); + Serial.println(); + delay(1000); + + } +} + +void print_binary(int v, int num_places) +{ + int mask=0, n; + + for (n=1; n<=num_places; n++) + { + mask = (mask << 1) | 0x0001; + } + v = v & mask; // truncate v to specified number of places + + while(num_places) + { + + if (v & (0x0001 << num_places-1)) + { + Serial.print("1"); + } + else + { + Serial.print("0"); + } + + --num_places; + if(((num_places%4) == 0) && (num_places != 0)) + { + Serial.print("_"); + } + } +} + diff --git a/examples/MrBrunetteExamples/Print_Float/Print_Float.ino b/examples/MrBrunetteExamples/Print_Float/Print_Float.ino index 32d3f0b..4dd0149 100644 --- a/examples/MrBrunetteExamples/Print_Float/Print_Float.ino +++ b/examples/MrBrunetteExamples/Print_Float/Print_Float.ino @@ -1,84 +1,84 @@ -/* - PRINT_FLOAT - Arduino 1.6.0rc1 - Sketch uses 15,164 bytes (14%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,592 bytes of dynamic memory. - Adapted to Maple Mini by m. ray burnette - Illustrates how to display floats in the range of -999.999 to 999.999 with a specified - number of digits after the decimal point. - copyright, Peter H Anderson, Baltimore, MD, Nov, '07 - PUBLIC DOMAIN EXAMPLE -*/ - -#define BAUD 9600 - -void setup() // run once, when the sketch starts -{ - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Print Float Format"); -} - -void loop() -{ - while(1) - { - print_float(0.6, 2); // illustrate various test cases - Serial.println(); - print_float(1.2, 1); - Serial.println(); - print_float(10.27, 2); - Serial.println(); - print_float(10.345, 3); - Serial.println(); - print_float(107.345, 3); - Serial.println(); - delay(1000); - - print_float(-0.6, 2); - Serial.println(); - print_float(-1.2, 1); - Serial.println(); - print_float(-10.27, 2); - Serial.println(); - print_float(-10.345, 3); - Serial.println(); - print_float(-107.345, 3); - Serial.println(); - delay(1000); - } -} - -void print_float(float f, int num_digits) -{ - int f_int; - int pows_of_ten[4] = {1, 10, 100, 1000}; - int multiplier, whole, fract, d, n; - - multiplier = pows_of_ten[num_digits]; - if (f < 0.0) - { - f = -f; - Serial.print("-"); - } - whole = (int) f; - fract = (int) (multiplier * (f - (float)whole)); - - Serial.print(whole); - Serial.print("."); - - for (n=num_digits-1; n>=0; n--) // print each digit with no leading zero suppression - { - d = fract / pows_of_ten[n]; - Serial.print(d); - fract = fract % pows_of_ten[n]; - } -} - - +/* + PRINT_FLOAT - Arduino 1.6.0rc1 + Sketch uses 15,164 bytes (14%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,592 bytes of dynamic memory. + Adapted to Maple Mini by m. ray burnette + Illustrates how to display floats in the range of -999.999 to 999.999 with a specified + number of digits after the decimal point. + copyright, Peter H Anderson, Baltimore, MD, Nov, '07 + PUBLIC DOMAIN EXAMPLE +*/ + +#define BAUD 9600 + +void setup() // run once, when the sketch starts +{ + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Print Float Format"); +} + +void loop() +{ + while(1) + { + print_float(0.6, 2); // illustrate various test cases + Serial.println(); + print_float(1.2, 1); + Serial.println(); + print_float(10.27, 2); + Serial.println(); + print_float(10.345, 3); + Serial.println(); + print_float(107.345, 3); + Serial.println(); + delay(1000); + + print_float(-0.6, 2); + Serial.println(); + print_float(-1.2, 1); + Serial.println(); + print_float(-10.27, 2); + Serial.println(); + print_float(-10.345, 3); + Serial.println(); + print_float(-107.345, 3); + Serial.println(); + delay(1000); + } +} + +void print_float(float f, int num_digits) +{ + int f_int; + int pows_of_ten[4] = {1, 10, 100, 1000}; + int multiplier, whole, fract, d, n; + + multiplier = pows_of_ten[num_digits]; + if (f < 0.0) + { + f = -f; + Serial.print("-"); + } + whole = (int) f; + fract = (int) (multiplier * (f - (float)whole)); + + Serial.print(whole); + Serial.print("."); + + for (n=num_digits-1; n>=0; n--) // print each digit with no leading zero suppression + { + d = fract / pows_of_ten[n]; + Serial.print(d); + fract = fract % pows_of_ten[n]; + } +} + + diff --git a/examples/MrBrunetteExamples/Print_HEX/Print_HEX.ino b/examples/MrBrunetteExamples/Print_HEX/Print_HEX.ino index 7b6e4f9..a4f5d69 100644 --- a/examples/MrBrunetteExamples/Print_HEX/Print_HEX.ino +++ b/examples/MrBrunetteExamples/Print_HEX/Print_HEX.ino @@ -1,61 +1,61 @@ -/* - PRINT_HEX - Arduino 1.6.0rc1 - Sketch uses 13,336 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,592 bytes of dynamic memory. - Adapted to the Maple Mini by m. ray burnette - Illustrates how to display a hexadecimal number with a fixed width. - opyright, Peter H Anderson, Baltimore, MD, Nov, '07 - PUBLIC DOMAIN EXAMPLE -*/ - -#define BAUD 9600 - -void setup() // run once, when the sketch starts -{ - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Print HEX Format"); -} - -void loop() -{ - while(1) - { - print_hex(1024+256+63, 13); - Serial.println(); - delay(1000); - - } -} - -void print_hex(int v, int num_places) -{ - int mask=0, n, num_nibbles, digit; - - for (n=1; n<=num_places; n++) - { - mask = (mask << 1) | 0x0001; - } - v = v & mask; // truncate v to specified number of places - - num_nibbles = num_places / 4; - if ((num_places % 4) != 0) - { - ++num_nibbles; - } - - do - { - digit = ((v >> (num_nibbles-1) * 4)) & 0x0f; - Serial.print(digit, HEX); - } while(--num_nibbles); - -} - +/* + PRINT_HEX - Arduino 1.6.0rc1 + Sketch uses 13,336 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,592 bytes of dynamic memory. + Adapted to the Maple Mini by m. ray burnette + Illustrates how to display a hexadecimal number with a fixed width. + opyright, Peter H Anderson, Baltimore, MD, Nov, '07 + PUBLIC DOMAIN EXAMPLE +*/ + +#define BAUD 9600 + +void setup() // run once, when the sketch starts +{ + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Print HEX Format"); +} + +void loop() +{ + while(1) + { + print_hex(1024+256+63, 13); + Serial.println(); + delay(1000); + + } +} + +void print_hex(int v, int num_places) +{ + int mask=0, n, num_nibbles, digit; + + for (n=1; n<=num_places; n++) + { + mask = (mask << 1) | 0x0001; + } + v = v & mask; // truncate v to specified number of places + + num_nibbles = num_places / 4; + if ((num_places % 4) != 0) + { + ++num_nibbles; + } + + do + { + digit = ((v >> (num_nibbles-1) * 4)) & 0x0f; + Serial.print(digit, HEX); + } while(--num_nibbles); + +} + diff --git a/examples/MrBrunetteExamples/SerialReadUntil/SerialReadUntil.ino b/examples/MrBrunetteExamples/SerialReadUntil/SerialReadUntil.ino index 815bf3a..b324554 100644 --- a/examples/MrBrunetteExamples/SerialReadUntil/SerialReadUntil.ino +++ b/examples/MrBrunetteExamples/SerialReadUntil/SerialReadUntil.ino @@ -1,35 +1,35 @@ - -// http://arduino.cc/forum/index.php?topic=114035.0 ` -/* Sketch uses 13,836 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 3,696 bytes of dynamic memory. - Read an unknown length string of ASCII characters terminated - with a line feed from the UART -*/ - -#define BAUD 9600 - -void setup() { - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UAR - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("Serial Read Until Example:"); - Serial.print("Type a few characters & press ENTER\r\n(make certain serial monitor sends CR+LF)"); -} - -void loop() { - char serialdata[80]; - int lf = 10; - - Serial.readBytesUntil(lf, serialdata, 80); - - Serial.println(serialdata); - -} - - + +// http://arduino.cc/forum/index.php?topic=114035.0 ` +/* Sketch uses 13,836 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 3,696 bytes of dynamic memory. + Read an unknown length string of ASCII characters terminated + with a line feed from the UART +*/ + +#define BAUD 9600 + +void setup() { + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UAR + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("Serial Read Until Example:"); + Serial.print("Type a few characters & press ENTER\r\n(make certain serial monitor sends CR+LF)"); +} + +void loop() { + char serialdata[80]; + int lf = 10; + + Serial.readBytesUntil(lf, serialdata, 80); + + Serial.println(serialdata); + +} + + diff --git a/examples/MrBrunetteExamples/StringEx_Parsing/StringEx_Parsing.ino b/examples/MrBrunetteExamples/StringEx_Parsing/StringEx_Parsing.ino index d1cd2498263bdc1904a0483008edc71ba99af0c1..1740cc9e91e3c2d5f25b135be1ede107cd6d000b 100644 GIT binary patch delta 113 zcmbOy`Au?zA6rUFW_o5xVorEbW=X1slfPq-OT3S(OMHN*pN=9_E+{oI1yxE@(^_9& tA*8e@PoX3uRRO3$A+0DsS0O(wO`#-TAwN$?A-@PnfjMbuo1@rV*Z_gsB=G8b~0C*}0ZvX%Q diff --git a/examples/MrBrunetteExamples/USB_ASCII/USB_ASCII.ino b/examples/MrBrunetteExamples/USB_ASCII/USB_ASCII.ino index c398843..50f449e 100644 --- a/examples/MrBrunetteExamples/USB_ASCII/USB_ASCII.ino +++ b/examples/MrBrunetteExamples/USB_ASCII/USB_ASCII.ino @@ -1,104 +1,104 @@ -/* - USBascii Example for Arduino 1.6.0rc1 on the Maple Mini STM32 platform - Sketch uses 13,572 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,600 bytes of dynamic memory. - Connect to the Maple Serial using the Serial Monitor, then press - any key and hit enter. - - Prints out byte values in all possible formats: - * as raw binary values - * as ASCII-encoded decimal, hex, octal, and binary values - - For more on ASCII, see: - http://www.asciitable.com - http://en.wikipedia.org/wiki/ASCII - - No external hardware needed. - - created 2006 - by Nicholas Zambetti - modified 18 Jan 2009 - by Tom Igoe - - - - Ported to the Maple 27 May 2010 - by Bryan Newbold - Minor edits by m. ray burnette for Arduino 1.6.0 - - PUBLIC DOMAIN EXAMPLE -*/ - -void setup() { - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(); // USB does not require BAUD - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - Serial.println("ASCII Table ~ Character Map"); - Serial.println("Press CR to start the printout"); -} - -// First visible ASCII character: '!' is number 33: -int thisByte = 33; -int junk = 0; -bool Virgin = true; -// You can also write ASCII characters in single quotes. -// for example. '!' is the same as 33, so you could also use this: -//int thisByte = '!'; - -void loop() { - Restart: - // Wait for the user to press a key - if (!Virgin) goto NextPhase; - while (!Serial.available()) - continue; - - while (Serial.available()) { - junk = Serial.read(); - } - //continue; - NextPhase: - Virgin = false ; - // Prints value unaltered, i.e. the raw binary version of the - // byte. The serial monitor interprets all bytes as - // ASCII, so 33, the first number, will show up as '!' - Serial.print(thisByte, BYTE); - - Serial.print(", dec: "); - // Prints value as string as an ASCII-encoded decimal (base 10). - // Decimal is the default format for Serial.print() and - // Serial.println(), so no modifier is needed: - Serial.print(thisByte); - // But you can declare the modifier for decimal if you want to. - // This also works if you uncomment it: - // Serial.print(thisByte, DEC); - - Serial.print(", hex: "); - // Prints value as string in hexadecimal (base 16): - Serial.print(thisByte, HEX); - - Serial.print(", oct: "); - // Prints value as string in octal (base 8); - Serial.print(thisByte, OCT); - - Serial.print(", bin: "); - // Prints value as string in binary (base 2); also prints ending - // line break: - Serial.println(thisByte, BIN); - - // If printed last visible character '~' or 126, stop: - if (thisByte == 126) { // You could also use if (thisByte == '~') { - thisByte = 33; - Virgin = true; - Serial.println("=============================="); - Serial.println("Press CR to start the printout"); - goto Restart ; - } - // Go on to the next character - thisByte++; -} +/* + USBascii Example for Arduino 1.6.0rc1 on the Maple Mini STM32 platform + Sketch uses 13,572 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,600 bytes of dynamic memory. + Connect to the Maple Serial using the Serial Monitor, then press + any key and hit enter. + + Prints out byte values in all possible formats: + * as raw binary values + * as ASCII-encoded decimal, hex, octal, and binary values + + For more on ASCII, see: + http://www.asciitable.com + http://en.wikipedia.org/wiki/ASCII + + No external hardware needed. + + created 2006 + by Nicholas Zambetti + modified 18 Jan 2009 + by Tom Igoe + + + + Ported to the Maple 27 May 2010 + by Bryan Newbold + Minor edits by m. ray burnette for Arduino 1.6.0 + + PUBLIC DOMAIN EXAMPLE +*/ + +void setup() { + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(); // USB does not require BAUD + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + Serial.println("ASCII Table ~ Character Map"); + Serial.println("Press CR to start the printout"); +} + +// First visible ASCII character: '!' is number 33: +int thisByte = 33; +int junk = 0; +bool Virgin = true; +// You can also write ASCII characters in single quotes. +// for example. '!' is the same as 33, so you could also use this: +//int thisByte = '!'; + +void loop() { + Restart: + // Wait for the user to press a key + if (!Virgin) goto NextPhase; + while (!Serial.available()) + continue; + + while (Serial.available()) { + junk = Serial.read(); + } + //continue; + NextPhase: + Virgin = false ; + // Prints value unaltered, i.e. the raw binary version of the + // byte. The serial monitor interprets all bytes as + // ASCII, so 33, the first number, will show up as '!' + Serial.print(thisByte, BYTE); + + Serial.print(", dec: "); + // Prints value as string as an ASCII-encoded decimal (base 10). + // Decimal is the default format for Serial.print() and + // Serial.println(), so no modifier is needed: + Serial.print(thisByte); + // But you can declare the modifier for decimal if you want to. + // This also works if you uncomment it: + // Serial.print(thisByte, DEC); + + Serial.print(", hex: "); + // Prints value as string in hexadecimal (base 16): + Serial.print(thisByte, HEX); + + Serial.print(", oct: "); + // Prints value as string in octal (base 8); + Serial.print(thisByte, OCT); + + Serial.print(", bin: "); + // Prints value as string in binary (base 2); also prints ending + // line break: + Serial.println(thisByte, BIN); + + // If printed last visible character '~' or 126, stop: + if (thisByte == 126) { // You could also use if (thisByte == '~') { + thisByte = 33; + Virgin = true; + Serial.println("=============================="); + Serial.println("Press CR to start the printout"); + goto Restart ; + } + // Go on to the next character + thisByte++; +} diff --git a/examples/MrBrunetteExamples/strtol_DecEquivalents/strtol_DecEquivalents.ino b/examples/MrBrunetteExamples/strtol_DecEquivalents/strtol_DecEquivalents.ino index d3da88f..a8e13aa 100644 --- a/examples/MrBrunetteExamples/strtol_DecEquivalents/strtol_DecEquivalents.ino +++ b/examples/MrBrunetteExamples/strtol_DecEquivalents/strtol_DecEquivalents.ino @@ -1,41 +1,41 @@ -/* - Modified for Arduino from: http://www.cplusplus.com/reference/cstdlib/strtol/ - Convert string to long integer: Maple Mini version by m. ray burnette: PUBLIC DOMAIN - Sketch uses 13,924 bytes (12%) of program storage space. Maximum is 108,000 bytes. - Global variables use 2,664 bytes of dynamic memory. - Following C++ libs not needed after Arduino 1.0.2 - #include - #include -*/ - -template inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } - - char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; - char * pEnd; - long int li1, li2, li3, li4; - -void setup() { - // initialize the digital pin as an output. - pinMode(BOARD_LED_PIN, OUTPUT); - Serial.begin(9600); - // wait for serial monitor to be connected. - while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) - { - toggleLED(); - delay(100); // fast blink - } - li1 = strtol (szNumbers,&pEnd,10); // BASE 10 - li2 = strtol (pEnd,&pEnd,16); // HEX - li3 = strtol (pEnd,&pEnd,2); // Binary - li4 = strtol (pEnd,NULL,0); // Integer constant with prefixed base Octal or Hex - // Serial.print ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4); - Serial << "The decimal equivalents are: " << li1 << " " << li2 << " " << li3 << " " << li4; - //return 0; -} - - - -void loop() { - // put your main code here, to run repeatedly: - -} +/* + Modified for Arduino from: http://www.cplusplus.com/reference/cstdlib/strtol/ + Convert string to long integer: Maple Mini version by m. ray burnette: PUBLIC DOMAIN + Sketch uses 13,924 bytes (12%) of program storage space. Maximum is 108,000 bytes. + Global variables use 2,664 bytes of dynamic memory. + Following C++ libs not needed after Arduino 1.0.2 + #include + #include +*/ + +template inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } + + char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; + char * pEnd; + long int li1, li2, li3, li4; + +void setup() { + // initialize the digital pin as an output. + pinMode(BOARD_LED_PIN, OUTPUT); + Serial.begin(9600); + // wait for serial monitor to be connected. + while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))) + { + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + delay(100); // fast blink + } + li1 = strtol (szNumbers,&pEnd,10); // BASE 10 + li2 = strtol (pEnd,&pEnd,16); // HEX + li3 = strtol (pEnd,&pEnd,2); // Binary + li4 = strtol (pEnd,NULL,0); // Integer constant with prefixed base Octal or Hex + // Serial.print ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4); + Serial << "The decimal equivalents are: " << li1 << " " << li2 << " " << li3 << " " << li4; + //return 0; +} + + + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/examples/Sensors/Knock/Knock.ino b/examples/Sensors/Knock/Knock.ino index ec68072..1fabceb 100644 --- a/examples/Sensors/Knock/Knock.ino +++ b/examples/Sensors/Knock/Knock.ino @@ -1,52 +1,52 @@ -/* - Knock Sensor - - This sketch reads a piezo element to detect a knocking sound. It - reads an analog pin and compares the result to a set threshold. If - the result is greater than the threshold, it writes "knock" to the - serial port, and toggles the LED on pin 13. - - The circuit: - * + connection of the piezo attached to analog in 0 - * - connection of the piezo attached to ground - * 1-megohm resistor attached from analog in 0 to ground - - http://www.arduino.cc/en/Tutorial/Knock - - created 25 Mar 2007 - by David Cuartielles - modified 30 Jun 2009 - by Tom Igoe - - Ported to the Maple - by LeafLabs -*/ - -// these constants won't change: -const int knockSensor = 0; // the piezo is connected to analog pin 0 -const int threshold = 100; // threshold value to decide when the detected sound is a knock or not - -// these variables will change: -int sensorReading = 0; // variable to store the value read from the sensor pin - -void setup() { - Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor - // Declare the knockSensor as an analog input: - pinMode(knockSensor, INPUT_ANALOG); - // declare the built-in LED pin as an output: - pinMode(BOARD_LED_PIN, OUTPUT); -} - -void loop() { - // read the sensor and store it in the variable sensorReading: - sensorReading = analogRead(knockSensor); - - // if the sensor reading is greater than the threshold: - if (sensorReading >= threshold) { - // toggle the built-in LED - toggleLED(); - // send the string "Knock!" back to the computer, followed by newline - Serial.println("Knock!"); - } - delay(100); // delay to avoid printing too often -} +/* + Knock Sensor + + This sketch reads a piezo element to detect a knocking sound. It + reads an analog pin and compares the result to a set threshold. If + the result is greater than the threshold, it writes "knock" to the + serial port, and toggles the LED on pin 13. + + The circuit: + * + connection of the piezo attached to analog in 0 + * - connection of the piezo attached to ground + * 1-megohm resistor attached from analog in 0 to ground + + http://www.arduino.cc/en/Tutorial/Knock + + created 25 Mar 2007 + by David Cuartielles + modified 30 Jun 2009 + by Tom Igoe + + Ported to the Maple + by LeafLabs +*/ + +// these constants won't change: +const int knockSensor = 0; // the piezo is connected to analog pin 0 +const int threshold = 100; // threshold value to decide when the detected sound is a knock or not + +// these variables will change: +int sensorReading = 0; // variable to store the value read from the sensor pin + +void setup() { + Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor + // Declare the knockSensor as an analog input: + pinMode(knockSensor, INPUT_ANALOG); + // declare the built-in LED pin as an output: + pinMode(BOARD_LED_PIN, OUTPUT); +} + +void loop() { + // read the sensor and store it in the variable sensorReading: + sensorReading = analogRead(knockSensor); + + // if the sensor reading is greater than the threshold: + if (sensorReading >= threshold) { + // toggle the built-in LED + digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));// Turn the LED from off to on, or on to off + // send the string "Knock!" back to the computer, followed by newline + Serial.println("Knock!"); + } + delay(100); // delay to avoid printing too often +}