Naze32 clone with Frysky receiver
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

example2.cpp 7.1KB

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