add LPC1114FN28 support
Dependents: rosserial_mbed_lib
Fork of MODSERIAL by
Diff: example2.cpp
- Revision:
- 13:70bb7c1769fa
- Parent:
- 12:8c7394e2ae7f
- Child:
- 15:a1d9e745d71e
--- a/example2.cpp Thu Jan 20 11:57:32 2011 +0000 +++ b/example2.cpp Thu Jan 20 12:16:14 2011 +0000 @@ -29,7 +29,7 @@ /* This example demostrates a simple "messaging" system. You can use it with a terminal program to test it out or write a cusom C#/C++/VB/etc program - to read and write messages to or from the Mbed. The default buad rate in + to read and write messages to or from the Mbed. The default baud rate in this example is 115200. In this example, the LEDs are controlled and pins p21 to p24 are set as @@ -55,6 +55,24 @@ may prefer to use the PinDetect library rather than using InterruptIn. @see http://mbed.org/users/AjK/libraries/PinDetect/latest + One point you may notice. Incoming messages are processed via main()'s + while(1) loop whereas pin changes have their messages directly sent. + The reason for this is when MODSERIAL makes callbacks to your application + it is in "interrupt context". And one thing you want to avoid is spending + lots of CPU time in that context. So, the callback moves the message from + the input buffer to a local holding buffer and it then sets a bool flag + which tells main()'s while(1) loop to process that buffer. This means the + time spent doing the real incoming message handing is within your program + and not within MODSERIAL's interrupt context. So you may ask, why not do + the same for out going messages? Well, because MODSERIAL output buffers + all your sent content then sending chars is very fast. MODSERIAL handles + all the nitty gritty bits for you. You can just send. This example uses + puts() to send the message. If you can, always try and use sprintf()+puts() + rathe than printf(), printf() is known to often screw things up when used + within an interrupt context. Better still, just use puts() and do away + with any of the crappy ?printf() calls if possible. But I found the code + below to work fine even at 115200baud. + */ @@ -76,12 +94,11 @@ InterruptIn P24(p24); MODSERIAL messageSystem(USBTX, USBRX); + char messageBufferIncoming[MESSAGE_BUFFER_SIZE]; char messageBufferOutgoing[MESSAGE_BUFFER_SIZE]; bool messageReceived; -uint32_t pinChanged, pinAction; - void messageReceive(void) { messageSystem.move(messageBufferIncoming, MESSAGE_BUFFER_SIZE); messageReceived = true; @@ -107,7 +124,9 @@ messageReceived = false; } -#define PIN_MESSAGE_SEND(x,y) sprintf(messageBufferOutgoing,"PIN%02d:%d\n",x,y);messageSystem.puts(messageBufferOutgoing); +#define PIN_MESSAGE_SEND(x,y) \ + sprintf(messageBufferOutgoing,"PIN%02d:%d\n",x,y);\ + messageSystem.puts(messageBufferOutgoing); void pin21Rise(void) { PIN_MESSAGE_SEND(21, 1); } void pin21Fall(void) { PIN_MESSAGE_SEND(21, 0); } @@ -125,15 +144,13 @@ messageSystem.attach(&messageReceive, MODSERIAL::RxAutoDetect); messageSystem.autoDectectChar('\n'); - pinChanged = pinAction = 0; - // Enable pullup resistors on pins. P21.mode(PullUp); P22.mode(PullUp); P23.mode(PullUp); P24.mode(PullUp); // Fix Mbed library bug, see http://mbed.org/forum/bugs-suggestions/topic/1498 LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2); - // Attach callbacks. + // Attach InterruptIn pin callbacks. P21.rise(&pin21Rise); P21.fall(&pin21Fall); P22.rise(&pin22Rise); P22.fall(&pin22Fall); P23.rise(&pin23Rise); P23.fall(&pin23Fall);