Only imu output

Dependencies:   Servo mbed

Fork of FYDP_Final2 by Mark Vandermeulen

Committer:
tntmarket
Date:
Wed Mar 25 09:58:50 2015 +0000
Revision:
5:d2e955a94940
Parent:
0:21019d94ad33
only imu output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
majik 0:21019d94ad33 1 /*
majik 0:21019d94ad33 2 Copyright (c) 2011 Andy Kirkham
majik 0:21019d94ad33 3
majik 0:21019d94ad33 4 Permission is hereby granted, free of charge, to any person obtaining a copy
majik 0:21019d94ad33 5 of this software and associated documentation files (the "Software"), to deal
majik 0:21019d94ad33 6 in the Software without restriction, including without limitation the rights
majik 0:21019d94ad33 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
majik 0:21019d94ad33 8 copies of the Software, and to permit persons to whom the Software is
majik 0:21019d94ad33 9 furnished to do so, subject to the following conditions:
majik 0:21019d94ad33 10
majik 0:21019d94ad33 11 The above copyright notice and this permission notice shall be included in
majik 0:21019d94ad33 12 all copies or substantial portions of the Software.
majik 0:21019d94ad33 13
majik 0:21019d94ad33 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
majik 0:21019d94ad33 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
majik 0:21019d94ad33 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
majik 0:21019d94ad33 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
majik 0:21019d94ad33 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
majik 0:21019d94ad33 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
majik 0:21019d94ad33 20 THE SOFTWARE.
majik 0:21019d94ad33 21
majik 0:21019d94ad33 22 @file example2.cpp
majik 0:21019d94ad33 23 @purpose Demos a simple messaging system.
majik 0:21019d94ad33 24 @version see ChangeLog.c
majik 0:21019d94ad33 25 @date Jan 2011
majik 0:21019d94ad33 26 @author Andy Kirkham
majik 0:21019d94ad33 27 */
majik 0:21019d94ad33 28
majik 0:21019d94ad33 29 /*
majik 0:21019d94ad33 30 This example demostrates a simple "messaging" system. You can use it with
majik 0:21019d94ad33 31 a terminal program to test it out or write a cusom C#/C++/VB/etc program
majik 0:21019d94ad33 32 to read and write messages to or from the Mbed. The default baud rate in
majik 0:21019d94ad33 33 this example is 115200.
majik 0:21019d94ad33 34
majik 0:21019d94ad33 35 In this example, the LEDs are controlled and pins p21 to p24 are set as
majik 0:21019d94ad33 36 InterruptIn and send messages out when their value changes.
majik 0:21019d94ad33 37
majik 0:21019d94ad33 38 To use, hook up the MBed USB and open your fav terminal. All messages
majik 0:21019d94ad33 39 end with the \n character, don't forget to hit carriage return!.
majik 0:21019d94ad33 40 As an example:-
majik 0:21019d94ad33 41
majik 0:21019d94ad33 42 to switch on LED1 send LED1:1\n, off is LED1:0\n and toggle is LED1:2\n
majik 0:21019d94ad33 43 to switch on LED2 send LED2:1\n, off is LED2:0\n and toggle is LED2:2\n
majik 0:21019d94ad33 44 to switch on LED3 send LED3:1\n, off is LED3:0\n and toggle is LED3:2\n
majik 0:21019d94ad33 45 to switch on LED4 send LED4:1\n, off is LED4:0\n and toggle is LED4:2\n
majik 0:21019d94ad33 46
majik 0:21019d94ad33 47 When a pin change on p21 to p24 happens, a message is sent. As an example
majik 0:21019d94ad33 48 when p21 goes low PIN21:0\n is sent, when goes high PIN21:1\n is sent.
majik 0:21019d94ad33 49
majik 0:21019d94ad33 50 Note, the InterruptIn pins p21 to p24 are setup to have pullups. This means
majik 0:21019d94ad33 51 they are high. To activate them use a wire to short the pin to 0volts.
majik 0:21019d94ad33 52
majik 0:21019d94ad33 53 If you find that p21 to p24 sent a lot of on/off/on/off then it's probably
majik 0:21019d94ad33 54 due to "bounce". If you are connecting a mechanical switch to a pin you
majik 0:21019d94ad33 55 may prefer to use the PinDetect library rather than using InterruptIn.
majik 0:21019d94ad33 56 @see http://mbed.org/users/AjK/libraries/PinDetect/latest
majik 0:21019d94ad33 57
majik 0:21019d94ad33 58 One point you may notice. Incoming messages are processed via main()'s
majik 0:21019d94ad33 59 while(1) loop whereas pin changes have their messages directly sent.
majik 0:21019d94ad33 60 The reason for this is when MODSERIAL makes callbacks to your application
majik 0:21019d94ad33 61 it is in "interrupt context". And one thing you want to avoid is spending
majik 0:21019d94ad33 62 lots of CPU time in that context. So, the callback moves the message from
majik 0:21019d94ad33 63 the input buffer to a local holding buffer and it then sets a bool flag
majik 0:21019d94ad33 64 which tells main()'s while(1) loop to process that buffer. This means the
majik 0:21019d94ad33 65 time spent doing the real incoming message handing is within your program
majik 0:21019d94ad33 66 and not within MODSERIAL's interrupt context. So you may ask, why not do
majik 0:21019d94ad33 67 the same for out going messages? Well, because MODSERIAL output buffers
majik 0:21019d94ad33 68 all your sent content then sending chars is very fast. MODSERIAL handles
majik 0:21019d94ad33 69 all the nitty gritty bits for you. You can just send. This example uses
majik 0:21019d94ad33 70 puts() to send the message. If you can, always try and use sprintf()+puts()
majik 0:21019d94ad33 71 rathe than printf(), printf() is known to often screw things up when used
majik 0:21019d94ad33 72 within an interrupt context. Better still, just use puts() and do away
majik 0:21019d94ad33 73 with any of the crappy ?printf() calls if possible. But I found the code
majik 0:21019d94ad33 74 below to work fine even at 115200baud.
majik 0:21019d94ad33 75
majik 0:21019d94ad33 76 */
majik 0:21019d94ad33 77
majik 0:21019d94ad33 78
majik 0:21019d94ad33 79 #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
majik 0:21019d94ad33 80
majik 0:21019d94ad33 81 #include "mbed.h"
majik 0:21019d94ad33 82 #include "MODSERIAL.h"
majik 0:21019d94ad33 83
majik 0:21019d94ad33 84 #define MESSAGE_BUFFER_SIZE 32
majik 0:21019d94ad33 85
majik 0:21019d94ad33 86 DigitalOut led1(LED1);
majik 0:21019d94ad33 87 DigitalOut led2(LED2);
majik 0:21019d94ad33 88 DigitalOut led3(LED3);
majik 0:21019d94ad33 89 DigitalOut led4(LED4);
majik 0:21019d94ad33 90
majik 0:21019d94ad33 91 InterruptIn P21(p21);
majik 0:21019d94ad33 92 InterruptIn P22(p22);
majik 0:21019d94ad33 93 InterruptIn P23(p23);
majik 0:21019d94ad33 94 InterruptIn P24(p24);
majik 0:21019d94ad33 95
majik 0:21019d94ad33 96 MODSERIAL messageSystem(USBTX, USBRX);
majik 0:21019d94ad33 97
majik 0:21019d94ad33 98 char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
majik 0:21019d94ad33 99 char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
majik 0:21019d94ad33 100 bool messageReceived;
majik 0:21019d94ad33 101
majik 0:21019d94ad33 102 void messageReceive(MODSERIAL_IRQ_INFO *q) {
majik 0:21019d94ad33 103 MODSERIAL *sys = q->serial;
majik 0:21019d94ad33 104 sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
majik 0:21019d94ad33 105 messageReceived = true;
majik 0:21019d94ad33 106 return 0;
majik 0:21019d94ad33 107 }
majik 0:21019d94ad33 108
majik 0:21019d94ad33 109 void messageProcess(void) {
majik 0:21019d94ad33 110 if (!strncmp(messageBufferIncoming, "LED1:1", sizeof("LED1:1")-1)) led1 = 1;
majik 0:21019d94ad33 111 else if (!strncmp(messageBufferIncoming, "LED1:0", sizeof("LED1:0")-1)) led1 = 0;
majik 0:21019d94ad33 112 else if (!strncmp(messageBufferIncoming, "LED1:2", sizeof("LED1:2")-1)) led1 = !led1;
majik 0:21019d94ad33 113
majik 0:21019d94ad33 114 else if (!strncmp(messageBufferIncoming, "LED2:1", sizeof("LED2:1")-1)) led2 = 1;
majik 0:21019d94ad33 115 else if (!strncmp(messageBufferIncoming, "LED2:0", sizeof("LED2:0")-1)) led2 = 0;
majik 0:21019d94ad33 116 else if (!strncmp(messageBufferIncoming, "LED2:2", sizeof("LED2:2")-1)) led2 = !led2;
majik 0:21019d94ad33 117
majik 0:21019d94ad33 118 else if (!strncmp(messageBufferIncoming, "LED3:1", sizeof("LED3:1")-1)) led3 = 1;
majik 0:21019d94ad33 119 else if (!strncmp(messageBufferIncoming, "LED3:0", sizeof("LED3:0")-1)) led3 = 0;
majik 0:21019d94ad33 120 else if (!strncmp(messageBufferIncoming, "LED3:2", sizeof("LED3:2")-1)) led3 = !led3;
majik 0:21019d94ad33 121
majik 0:21019d94ad33 122 else if (!strncmp(messageBufferIncoming, "LED4:1", sizeof("LED4:1")-1)) led4 = 1;
majik 0:21019d94ad33 123 else if (!strncmp(messageBufferIncoming, "LED4:0", sizeof("LED4:0")-1)) led4 = 0;
majik 0:21019d94ad33 124 else if (!strncmp(messageBufferIncoming, "LED4:2", sizeof("LED4:2")-1)) led4 = !led4;
majik 0:21019d94ad33 125
majik 0:21019d94ad33 126 messageReceived = false;
majik 0:21019d94ad33 127 }
majik 0:21019d94ad33 128
majik 0:21019d94ad33 129 #define PIN_MESSAGE_SEND(x,y) \
majik 0:21019d94ad33 130 sprintf(messageBufferOutgoing,"PIN%02d:%d\n",x,y);\
majik 0:21019d94ad33 131 messageSystem.puts(messageBufferOutgoing);
majik 0:21019d94ad33 132
majik 0:21019d94ad33 133 void pin21Rise(void) { PIN_MESSAGE_SEND(21, 1); }
majik 0:21019d94ad33 134 void pin21Fall(void) { PIN_MESSAGE_SEND(21, 0); }
majik 0:21019d94ad33 135 void pin22Rise(void) { PIN_MESSAGE_SEND(22, 1); }
majik 0:21019d94ad33 136 void pin22Fall(void) { PIN_MESSAGE_SEND(22, 0); }
majik 0:21019d94ad33 137 void pin23Rise(void) { PIN_MESSAGE_SEND(23, 1); }
majik 0:21019d94ad33 138 void pin23Fall(void) { PIN_MESSAGE_SEND(23, 0); }
majik 0:21019d94ad33 139 void pin24Rise(void) { PIN_MESSAGE_SEND(24, 1); }
majik 0:21019d94ad33 140 void pin24Fall(void) { PIN_MESSAGE_SEND(24, 0); }
majik 0:21019d94ad33 141
majik 0:21019d94ad33 142 int main() {
majik 0:21019d94ad33 143
majik 0:21019d94ad33 144 messageReceived = false;
majik 0:21019d94ad33 145 messageSystem.baud(115200);
majik 0:21019d94ad33 146 messageSystem.attach(&messageReceive, MODSERIAL::RxAutoDetect);
majik 0:21019d94ad33 147 messageSystem.autoDetectChar('\n');
majik 0:21019d94ad33 148
majik 0:21019d94ad33 149 // Enable pullup resistors on pins.
majik 0:21019d94ad33 150 P21.mode(PullUp); P22.mode(PullUp); P23.mode(PullUp); P24.mode(PullUp);
majik 0:21019d94ad33 151
majik 0:21019d94ad33 152 // Fix Mbed library bug, see http://mbed.org/forum/bugs-suggestions/topic/1498
majik 0:21019d94ad33 153 LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2);
majik 0:21019d94ad33 154
majik 0:21019d94ad33 155 // Attach InterruptIn pin callbacks.
majik 0:21019d94ad33 156 P21.rise(&pin21Rise); P21.fall(&pin21Fall);
majik 0:21019d94ad33 157 P22.rise(&pin22Rise); P22.fall(&pin22Fall);
majik 0:21019d94ad33 158 P23.rise(&pin23Rise); P23.fall(&pin23Fall);
majik 0:21019d94ad33 159 P24.rise(&pin24Rise); P24.fall(&pin24Fall);
majik 0:21019d94ad33 160
majik 0:21019d94ad33 161 while(1) {
majik 0:21019d94ad33 162 // Process incoming messages.
majik 0:21019d94ad33 163 if (messageReceived) messageProcess();
majik 0:21019d94ad33 164 }
majik 0:21019d94ad33 165 }
majik 0:21019d94ad33 166
majik 0:21019d94ad33 167 #endif