From 767bf60238435be10b7197dc4300b9e678f18c72 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Fri, 4 Jun 2010 20:01:34 -0400 Subject: example updates... --- examples/test-session.cpp | 1047 +++++++++++++++++++++++---------------------- 1 file changed, 524 insertions(+), 523 deletions(-) (limited to 'examples') diff --git a/examples/test-session.cpp b/examples/test-session.cpp index 78b1d31..49aabef 100644 --- a/examples/test-session.cpp +++ b/examples/test-session.cpp @@ -1,523 +1,524 @@ -/* ***************************************************************************** - * The MIT License - * - * Copyright (c) 2010 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" - -#define LED_PIN 13 -#define PWM_PIN 2 - -uint8 input = 0; -uint8 tiddle = 0; -int toggle = 0; -int rate = 0; -int sample = 0; - -// read these off maple board rev3 -const uint8 pwm_pins[] = {0,1,2,3,5,6,7,8,9,11,12,14,24,25,27,28}; -// note that 38 is just a button and 39+ aren't functional as of 04/22/2010 -const uint8 adc_pins[] = {0,1,2,10,11,12,13,15,16,17,18,19,20,27,28}; -#define NUM_GPIO 44 // 43 is the MAX -uint8 gpio_state[NUM_GPIO]; - -#define DUMMY_DAT "qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-=qwertyuiopasdfghjklzxcvbnm,./1234567890" - -void print_help(void); -void do_noise(uint8 pin); -void do_everything(void); -void do_fast_gpio(void); - -void setup() { - /* Set up the LED to blink */ - pinMode(LED_PIN, OUTPUT); - - /* Send a message out over USART2 */ - //Serial2.begin(115200); // 9600 is more compatible - Serial2.begin(9600); - Serial2.println(""); - Serial2.println(" __ __ _ _"); - Serial2.println(" | \\/ | __ _ _ __ | | ___| |"); - Serial2.println(" | |\\/| |/ _` | '_ \\| |/ _ \\ |"); - Serial2.println(" | | | | (_| | |_) | | __/_|"); - Serial2.println(" |_| |_|\\__,_| .__/|_|\\___(_)"); - Serial2.println(" |_|"); - Serial2.println(" by leaflabs"); - Serial2.println(""); - Serial2.println(""); - Serial2.println("Maple interactive test program (type '?' for help)"); - Serial2.println("------------------------------------------------------------"); - Serial2.print("> "); - - /* Send a message out the USB virtual com port */ - // TODO: this should all be over usb as well - //Usb.println("Maple test program starting; use serial port for interactivity"); -} - -void loop() { - toggle ^= 1; - digitalWrite(LED_PIN, toggle); - delay(100); - - while(Serial2.available()) { - input = Serial2.read(); - Serial2.println(input); - switch(input) { - case 13: // Carriage Return - break; - case 32: // ' ' - Serial2.println("spacebar, nice!"); - break; - case 63: // '?' - case 104: // 'h' - print_help(); - break; - case 117: // 'u' - Usb.println("Hello World!"); - break; - case 119: // 'w' - Serial1.println("Hello World!"); - Serial2.println("Hello World!"); - Serial3.println("Hello World!"); - break; - case 109: // 'm' - Serial2.println("Testing 57600 baud on USART1 and USART3. Press enter."); - Serial1.begin(57600); - Serial3.begin(57600); - while(!Serial2.available()) { - Serial1.println(DUMMY_DAT); - Serial3.println(DUMMY_DAT); - if(Serial1.available()) { - Serial1.println(Serial1.read()); - delay(1000); - } - if(Serial3.available()) { - Serial3.println(Serial3.read()); - delay(1000); - } - } - Serial2.read(); - Serial2.println("Testing 115200 baud on USART1 and USART3. Press enter."); - Serial1.begin(115200); - Serial3.begin(115200); - while(!Serial2.available()) { - Serial1.println(DUMMY_DAT); - Serial3.println(DUMMY_DAT); - if(Serial1.available()) { - Serial1.println(Serial1.read()); - delay(1000); - } - if(Serial3.available()) { - Serial3.println(Serial3.read()); - delay(1000); - } - } - Serial2.read(); - Serial2.println("Testing 9600 baud on USART1 and USART3. Press enter."); - Serial1.begin(9600); - Serial3.begin(9600); - while(!Serial2.available()) { - Serial1.println(DUMMY_DAT); - Serial3.println(DUMMY_DAT); - if(Serial1.available()) { - Serial1.println(Serial1.read()); - delay(1000); - } - if(Serial3.available()) { - Serial3.println(Serial3.read()); - delay(1000); - } - } - Serial2.read(); - Serial2.println("Resetting USART1 and USART3..."); - Serial1.begin(9600); - Serial3.begin(9600); - break; - case 46: // '.' - while(!Serial2.available()) { - Serial2.print("."); - Usb.print("."); - } - //Serial2.flush(); - break; - case 110: // 'n' - Serial2.println("Taking ADC noise stats..."); - // turn off LED - digitalWrite(LED_PIN, 0); - // make sure to skip the TX/RX headers - for(uint32 i = 2; i 65500) rate = 0; - for(uint32 i = 2; i 5734) rate = 4096; - for(uint32 i = 2; i= j) Serial2.print("#"); - else Serial2.print(" "); - } - Serial2.print("| "); - for(int j = 0; j<12; j++) { - if(sample & (1 << (11-j))) Serial2.print("1"); - else Serial2.print("0"); - } - Serial2.println(""); - } - pinMode(adc_pins[i], OUTPUT); - digitalWrite(adc_pins[i], 0); - if((uint8)Serial2.read() == (uint8)27) break; // ESC - } - break; - default: - Serial2.print("Unexpected: "); - Serial2.println(input); - } - Serial2.print("> "); - } -} - -void print_help(void) { - Serial2.println(""); - //Serial2.println("Command Listing\t(# means any digit)"); - Serial2.println("Command Listing"); - Serial2.println("\t?: print this menu"); - Serial2.println("\th: print this menu"); - Serial2.println("\tw: print Hello World on all 3 USARTS"); - Serial2.println("\tn: measure noise and do statistics"); - Serial2.println("\tN: measure noise and do statistics with background stuff"); - Serial2.println("\ta: show realtime ADC info"); - Serial2.println("\t.: echo '.' until new input"); - Serial2.println("\tu: print Hello World on USB"); - Serial2.println("\t_: try to do as little as possible for a couple seconds (delay)"); - Serial2.println("\tp: test all PWM channels sequentially"); - Serial2.println("\tW: dump data as fast as possible on all 3 USARTS"); - Serial2.println("\tU: dump data as fast as possible on USB"); - Serial2.println("\tg: toggle all GPIOs sequentialy"); - Serial2.println("\tG: toggle all GPIOs at the same time"); - Serial2.println("\tf: toggle GPIO D4 as fast as possible in bursts"); - Serial2.println("\tP: test all PWM channels at the same time with different speeds/sweeps"); - Serial2.println("\tr: read in GPIO status changes and print them in realtime"); - Serial2.println("\ts: output a sweeping SERVO PWM on all PWM channels"); - Serial2.println("\tm: output serial data dumps on USART1 and USART3 with various rates"); - - Serial2.println("Unimplemented:"); - Serial2.println("\te: do everything all at once until new input"); - Serial2.println("\tt: output a 1khz squarewave on all GPIOs as well as possible"); - Serial2.println("\tT: output a 1hz squarewave on all GPIOs as well as possible"); - Serial2.println("\ti: print out a bunch of info about system state"); - Serial2.println("\tI: print out status of all headers"); -} - -void do_noise(uint8 pin) { // TODO - uint16 data[100]; - float mean = 0; - //float stddev = 0; - float delta = 0; - float M2 = 0; - pinMode(pin, INPUT_ANALOG); - - // variance algorithm from knuth; see wikipedia - // checked against python - 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); - } - - //sqrt is broken? - //stddev = sqrt(variance); - Serial2.print("header: D"); Serial2.print(pin,DEC); - Serial2.print("\tn: "); Serial2.print(100,DEC); - Serial2.print("\tmean: "); Serial2.print(mean); - Serial2.print("\tvar: "); Serial2.println(M2/99.0); - pinMode(pin, OUTPUT); -} - -void do_everything(void) { // TODO - // TODO - // print to usart - // print to usb - // toggle gpios - // enable pwm - Serial2.println("(unimplemented)"); -} - -void do_fast_gpio(void) { - // header D4 is on port B and is pin 5 on the uC - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); - gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); -} - -int main(void) { - init(); - setup(); - - while (1) { - loop(); - } - return 0; -} - +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 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" + +#define LED_PIN 13 +#define PWM_PIN 2 + +// choose your weapon +#define COMM SerialUSB +//#define COMM Serial2 + +uint8 input = 0; +uint8 tiddle = 0; +int toggle = 0; +int rate = 0; +int sample = 0; + +// read these off maple board rev3 +// note that 38 is just a button and 39+ aren't functional as of 04/22/2010 +const uint8 pwm_pins[] = {0,1,2,3,5,6,7,8,9,11,12,14,24,25,27,28}; +const uint8 adc_pins[] = {0,1,2,10,11,12,13,15,16,17,18,19,20,27,28}; +#define NUM_GPIO 44 // 44 is the MAX +uint8 gpio_state[NUM_GPIO]; + +#define DUMMY_DAT "qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-=qwertyuiopasdfghjklzxcvbnm,./1234567890" + +void print_help(void); +void do_noise(uint8 pin); +void do_everything(void); +void do_fast_gpio(void); + +void setup() { + /* Set up the LED to blink */ + pinMode(LED_PIN, OUTPUT); + + /* Send a message out over COMM interface */ + //COMM.begin(115200); // if USART; 9600 is more compatible + COMM.println(""); + COMM.println(" __ __ _ _"); + COMM.println(" | \\/ | __ _ _ __ | | ___| |"); + COMM.println(" | |\\/| |/ _` | '_ \\| |/ _ \\ |"); + COMM.println(" | | | | (_| | |_) | | __/_|"); + COMM.println(" |_| |_|\\__,_| .__/|_|\\___(_)"); + COMM.println(" |_|"); + COMM.println(" by leaflabs"); + COMM.println(""); + COMM.println(""); + COMM.println("Maple interactive test program (type '?' for help)"); + COMM.println("------------------------------------------------------------"); + COMM.print("> "); + +} + +void loop() { + toggle ^= 1; + digitalWrite(LED_PIN, toggle); + delay(100); + + while(COMM.available()) { + input = COMM.read(); + COMM.println(input); + switch(input) { + case 13: // Carriage Return + break; + case 32: // ' ' + COMM.println("spacebar, nice!"); + break; + case 63: // '?' + case 104: // 'h' + print_help(); + break; + case 117: // 'u' + Usb.println("Hello World!"); + break; + case 119: // 'w' + Serial1.println("Hello World!"); + Serial2.println("Hello World!"); + Serial3.println("Hello World!"); + break; + case 109: // 'm' + COMM.println("Testing 57600 baud on USART1 and USART3. Press enter."); + Serial1.begin(57600); + Serial3.begin(57600); + while(!COMM.available()) { + Serial1.println(DUMMY_DAT); + Serial3.println(DUMMY_DAT); + if(Serial1.available()) { + Serial1.println(Serial1.read()); + delay(1000); + } + if(Serial3.available()) { + Serial3.println(Serial3.read()); + delay(1000); + } + } + COMM.read(); + COMM.println("Testing 115200 baud on USART1 and USART3. Press enter."); + Serial1.begin(115200); + Serial3.begin(115200); + while(!COMM.available()) { + Serial1.println(DUMMY_DAT); + Serial3.println(DUMMY_DAT); + if(Serial1.available()) { + Serial1.println(Serial1.read()); + delay(1000); + } + if(Serial3.available()) { + Serial3.println(Serial3.read()); + delay(1000); + } + } + COMM.read(); + COMM.println("Testing 9600 baud on USART1 and USART3. Press enter."); + Serial1.begin(9600); + Serial3.begin(9600); + while(!COMM.available()) { + Serial1.println(DUMMY_DAT); + Serial3.println(DUMMY_DAT); + if(Serial1.available()) { + Serial1.println(Serial1.read()); + delay(1000); + } + if(Serial3.available()) { + Serial3.println(Serial3.read()); + delay(1000); + } + } + COMM.read(); + COMM.println("Resetting USART1 and USART3..."); + Serial1.begin(9600); + Serial3.begin(9600); + break; + case 46: // '.' + while(!COMM.available()) { + Serial1.print("."); + Serial2.print("."); + Serial3.print("."); + Usb.print("."); + } + break; + case 110: // 'n' + COMM.println("Taking ADC noise stats..."); + // turn off LED + digitalWrite(LED_PIN, 0); + // make sure to skip the TX/RX headers + for(uint32 i = 2; i 65500) rate = 0; + for(uint32 i = 2; i 5734) rate = 4096; + for(uint32 i = 2; i= j) COMM.print("#"); + else COMM.print(" "); + } + COMM.print("| "); + for(int j = 0; j<12; j++) { + if(sample & (1 << (11-j))) COMM.print("1"); + else COMM.print("0"); + } + COMM.println(""); + } + pinMode(adc_pins[i], OUTPUT); + digitalWrite(adc_pins[i], 0); + if((uint8)COMM.read() == (uint8)27) break; // ESC + } + break; + default: + COMM.print("Unexpected: "); + COMM.println(input); + } + COMM.print("> "); + } +} + +void print_help(void) { + COMM.println(""); + //COMM.println("Command Listing\t(# means any digit)"); + COMM.println("Command Listing"); + COMM.println("\t?: print this menu"); + COMM.println("\th: print this menu"); + COMM.println("\tw: print Hello World on all 3 USARTS"); + COMM.println("\tn: measure noise and do statistics"); + COMM.println("\tN: measure noise and do statistics with background stuff"); + COMM.println("\ta: show realtime ADC info"); + COMM.println("\t.: echo '.' until new input"); + COMM.println("\tu: print Hello World on USB"); + COMM.println("\t_: try to do as little as possible for a couple seconds (delay)"); + COMM.println("\tp: test all PWM channels sequentially"); + COMM.println("\tW: dump data as fast as possible on all 3 USARTS"); + COMM.println("\tU: dump data as fast as possible on USB"); + COMM.println("\tg: toggle all GPIOs sequentialy"); + COMM.println("\tG: toggle all GPIOs at the same time"); + COMM.println("\tf: toggle GPIO D4 as fast as possible in bursts"); + COMM.println("\tP: test all PWM channels at the same time with different speeds/sweeps"); + COMM.println("\tr: read in GPIO status changes and print them in realtime"); + COMM.println("\ts: output a sweeping SERVO PWM on all PWM channels"); + COMM.println("\tm: output serial data dumps on USART1 and USART3 with various rates"); + + COMM.println("Unimplemented:"); + COMM.println("\te: do everything all at once until new input"); + COMM.println("\tt: output a 1khz squarewave on all GPIOs as well as possible"); + COMM.println("\tT: output a 1hz squarewave on all GPIOs as well as possible"); + COMM.println("\ti: print out a bunch of info about system state"); + COMM.println("\tI: print out status of all headers"); +} + +void do_noise(uint8 pin) { // TODO + uint16 data[100]; + float mean = 0; + //float stddev = 0; + float delta = 0; + float M2 = 0; + pinMode(pin, INPUT_ANALOG); + + // variance algorithm from knuth; see wikipedia + // checked against python + 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); + } + + //sqrt is broken? + //stddev = sqrt(variance); + COMM.print("header: D"); COMM.print(pin,DEC); + COMM.print("\tn: "); COMM.print(100,DEC); + COMM.print("\tmean: "); COMM.print(mean); + COMM.print("\tvar: "); COMM.println(M2/99.0); + pinMode(pin, OUTPUT); +} + +void do_everything(void) { // TODO + // TODO + // print to usart + // print to usb + // toggle gpios + // enable pwm + COMM.println("(unimplemented)"); +} + +void do_fast_gpio(void) { + // header D4 is on port B and is pin 5 on the uC + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); + gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0); +} + +int main(void) { + init(); + setup(); + + while (1) { + loop(); + } + return 0; +} + -- cgit v1.2.3