Reaction Wheel Actuated Satellite Dynamics Test Platform

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example2.cpp Source File

example2.cpp

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021     
00022     @file          example2.cpp 
00023     @purpose       Demos a simple messaging system.
00024     @version       see ChangeLog.c
00025     @date          Jan 2011
00026     @author        Andy Kirkham
00027 */
00028 
00029 /*
00030     This example demostrates a simple "messaging" system. You can use it with
00031     a terminal program to test it out or write a cusom C#/C++/VB/etc program
00032     to read and write messages to or from the Mbed. The default baud rate in
00033     this example is 115200.
00034     
00035     In this example, the LEDs are controlled and pins p21 to p24 are set as
00036     InterruptIn and send messages out when their value changes.
00037     
00038     To use, hook up the MBed USB and open your fav terminal. All messages
00039     end with the \n character, don't forget to hit carriage return!. 
00040     As an example:-
00041         
00042         to switch on LED1 send LED1:1\n, off is LED1:0\n and toggle is LED1:2\n
00043         to switch on LED2 send LED2:1\n, off is LED2:0\n and toggle is LED2:2\n
00044         to switch on LED3 send LED3:1\n, off is LED3:0\n and toggle is LED3:2\n
00045         to switch on LED4 send LED4:1\n, off is LED4:0\n and toggle is LED4:2\n
00046         
00047     When a pin change on p21 to p24 happens, a message is sent. As an example
00048     when p21 goes low PIN21:0\n is sent, when goes high PIN21:1\n is sent.
00049     
00050     Note, the InterruptIn pins p21 to p24 are setup to have pullups. This means
00051     they are high. To activate them use a wire to short the pin to 0volts.
00052     
00053     If you find that p21 to p24 sent a lot of on/off/on/off then it's probably
00054     due to "bounce". If you are connecting a mechanical switch to a pin you
00055     may prefer to use the PinDetect library rather than using InterruptIn.
00056     @see http://mbed.org/users/AjK/libraries/PinDetect/latest
00057     
00058     One point you may notice. Incoming messages are processed via main()'s
00059     while(1) loop whereas pin changes have their messages directly sent.
00060     The reason for this is when MODSERIAL makes callbacks to your application
00061     it is in "interrupt context". And one thing you want to avoid is spending
00062     lots of CPU time in that context. So, the callback moves the message from
00063     the input buffer to a local holding buffer and it then sets a bool flag
00064     which tells main()'s while(1) loop to process that buffer. This means the 
00065     time spent doing the real incoming message handing is within your program
00066     and not within MODSERIAL's interrupt context. So you may ask, why not do
00067     the same for out going messages? Well, because MODSERIAL output buffers
00068     all your sent content then sending chars is very fast. MODSERIAL handles
00069     all the nitty gritty bits for you. You can just send. This example uses
00070     puts() to send the message. If you can, always try and use sprintf()+puts()
00071     rathe than printf(), printf() is known to often screw things up when used
00072     within an interrupt context. Better still, just use puts() and do away
00073     with any of the crappy ?printf() calls if possible. But I found the code
00074     below to work fine even at 115200baud.
00075     
00076 */
00077 
00078 
00079 #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
00080 
00081 #include "mbed.h"
00082 #include "MODSERIAL.h"
00083 
00084 #define MESSAGE_BUFFER_SIZE 32
00085 
00086 DigitalOut led1(LED1);
00087 DigitalOut led2(LED2);
00088 DigitalOut led3(LED3);
00089 DigitalOut led4(LED4);
00090 
00091 InterruptIn P21(p21);
00092 InterruptIn P22(p22);
00093 InterruptIn P23(p23);
00094 InterruptIn P24(p24);
00095 
00096 MODSERIAL messageSystem(USBTX, USBRX);
00097 
00098 char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
00099 char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
00100 bool messageReceived;
00101 
00102 void messageReceive(MODSERIAL_IRQ_INFO *q) {
00103     MODSERIAL *sys = q->serial;
00104     sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
00105     messageReceived = true;
00106     return 0;
00107 }
00108 
00109 void messageProcess(void) {
00110          if (!strncmp(messageBufferIncoming, "LED1:1", sizeof("LED1:1")-1)) led1 = 1;
00111     else if (!strncmp(messageBufferIncoming, "LED1:0", sizeof("LED1:0")-1)) led1 = 0;
00112     else if (!strncmp(messageBufferIncoming, "LED1:2", sizeof("LED1:2")-1)) led1 = !led1;
00113     
00114     else if (!strncmp(messageBufferIncoming, "LED2:1", sizeof("LED2:1")-1)) led2 = 1;
00115     else if (!strncmp(messageBufferIncoming, "LED2:0", sizeof("LED2:0")-1)) led2 = 0;
00116     else if (!strncmp(messageBufferIncoming, "LED2:2", sizeof("LED2:2")-1)) led2 = !led2;
00117     
00118     else if (!strncmp(messageBufferIncoming, "LED3:1", sizeof("LED3:1")-1)) led3 = 1;
00119     else if (!strncmp(messageBufferIncoming, "LED3:0", sizeof("LED3:0")-1)) led3 = 0;
00120     else if (!strncmp(messageBufferIncoming, "LED3:2", sizeof("LED3:2")-1)) led3 = !led3;
00121     
00122     else if (!strncmp(messageBufferIncoming, "LED4:1", sizeof("LED4:1")-1)) led4 = 1;
00123     else if (!strncmp(messageBufferIncoming, "LED4:0", sizeof("LED4:0")-1)) led4 = 0;
00124     else if (!strncmp(messageBufferIncoming, "LED4:2", sizeof("LED4:2")-1)) led4 = !led4;
00125     
00126     messageReceived = false;
00127 }
00128 
00129 #define PIN_MESSAGE_SEND(x,y) \
00130     sprintf(messageBufferOutgoing,"PIN%02d:%d\n",x,y);\
00131     messageSystem.puts(messageBufferOutgoing);
00132 
00133 void pin21Rise(void) { PIN_MESSAGE_SEND(21, 1); }
00134 void pin21Fall(void) { PIN_MESSAGE_SEND(21, 0); }
00135 void pin22Rise(void) { PIN_MESSAGE_SEND(22, 1); }
00136 void pin22Fall(void) { PIN_MESSAGE_SEND(22, 0); }
00137 void pin23Rise(void) { PIN_MESSAGE_SEND(23, 1); }
00138 void pin23Fall(void) { PIN_MESSAGE_SEND(23, 0); }
00139 void pin24Rise(void) { PIN_MESSAGE_SEND(24, 1); }
00140 void pin24Fall(void) { PIN_MESSAGE_SEND(24, 0); }
00141 
00142 int main() {
00143 
00144     messageReceived = false;
00145     messageSystem.baud(115200);
00146     messageSystem.attach(&messageReceive, MODSERIAL::RxAutoDetect);
00147     messageSystem.autoDetectChar('\n'); 
00148 
00149     // Enable pullup resistors on pins.
00150     P21.mode(PullUp); P22.mode(PullUp); P23.mode(PullUp); P24.mode(PullUp);
00151     
00152     // Fix Mbed library bug, see http://mbed.org/forum/bugs-suggestions/topic/1498
00153     LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2); 
00154     
00155     // Attach InterruptIn pin callbacks.
00156     P21.rise(&pin21Rise); P21.fall(&pin21Fall);
00157     P22.rise(&pin22Rise); P22.fall(&pin22Fall);
00158     P23.rise(&pin23Rise); P23.fall(&pin23Fall);
00159     P24.rise(&pin24Rise); P24.fall(&pin24Fall);
00160     
00161     while(1) {
00162         // Process incoming messages.
00163         if (messageReceived) messageProcess();
00164     }
00165 }
00166 
00167 #endif