123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
-
-
-
-
-
- #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
-
- #include "mbed.h"
- #include "MODSERIAL.h"
-
- #define MESSAGE_BUFFER_SIZE 32
-
- DigitalOut led1(LED1);
- DigitalOut led2(LED2);
- DigitalOut led3(LED3);
- DigitalOut led4(LED4);
-
- InterruptIn P21(p21);
- InterruptIn P22(p22);
- InterruptIn P23(p23);
- InterruptIn P24(p24);
-
- MODSERIAL messageSystem(USBTX, USBRX);
-
- char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
- char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
- bool messageReceived;
-
- void messageReceive(MODSERIAL_IRQ_INFO *q) {
- MODSERIAL *sys = q->serial;
- sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
- messageReceived = true;
- return 0;
- }
-
- void messageProcess(void) {
- if (!strncmp(messageBufferIncoming, "LED1:1", sizeof("LED1:1")-1)) led1 = 1;
- else if (!strncmp(messageBufferIncoming, "LED1:0", sizeof("LED1:0")-1)) led1 = 0;
- else if (!strncmp(messageBufferIncoming, "LED1:2", sizeof("LED1:2")-1)) led1 = !led1;
-
- else if (!strncmp(messageBufferIncoming, "LED2:1", sizeof("LED2:1")-1)) led2 = 1;
- else if (!strncmp(messageBufferIncoming, "LED2:0", sizeof("LED2:0")-1)) led2 = 0;
- else if (!strncmp(messageBufferIncoming, "LED2:2", sizeof("LED2:2")-1)) led2 = !led2;
-
- else if (!strncmp(messageBufferIncoming, "LED3:1", sizeof("LED3:1")-1)) led3 = 1;
- else if (!strncmp(messageBufferIncoming, "LED3:0", sizeof("LED3:0")-1)) led3 = 0;
- else if (!strncmp(messageBufferIncoming, "LED3:2", sizeof("LED3:2")-1)) led3 = !led3;
-
- else if (!strncmp(messageBufferIncoming, "LED4:1", sizeof("LED4:1")-1)) led4 = 1;
- else if (!strncmp(messageBufferIncoming, "LED4:0", sizeof("LED4:0")-1)) led4 = 0;
- else if (!strncmp(messageBufferIncoming, "LED4:2", sizeof("LED4:2")-1)) led4 = !led4;
-
- messageReceived = false;
- }
-
- #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); }
- void pin22Rise(void) { PIN_MESSAGE_SEND(22, 1); }
- void pin22Fall(void) { PIN_MESSAGE_SEND(22, 0); }
- void pin23Rise(void) { PIN_MESSAGE_SEND(23, 1); }
- void pin23Fall(void) { PIN_MESSAGE_SEND(23, 0); }
- void pin24Rise(void) { PIN_MESSAGE_SEND(24, 1); }
- void pin24Fall(void) { PIN_MESSAGE_SEND(24, 0); }
-
- int main() {
-
- messageReceived = false;
- messageSystem.baud(115200);
- messageSystem.attach(&messageReceive, MODSERIAL::RxAutoDetect);
- messageSystem.autoDetectChar('\n');
-
-
- P21.mode(PullUp); P22.mode(PullUp); P23.mode(PullUp); P24.mode(PullUp);
-
-
- LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2);
-
-
- P21.rise(&pin21Rise); P21.fall(&pin21Fall);
- P22.rise(&pin22Rise); P22.fall(&pin22Fall);
- P23.rise(&pin23Rise); P23.fall(&pin23Fall);
- P24.rise(&pin24Rise); P24.fall(&pin24Fall);
-
- while(1) {
-
- if (messageReceived) messageProcess();
- }
- }
-
- #endif
|