
iotass5
main.cpp@2:abf92acbdd90, 2016-01-22 (annotated)
- Committer:
- co657_ne75
- Date:
- Fri Jan 22 00:15:06 2016 +0000
- Revision:
- 2:abf92acbdd90
- Parent:
- 1:469f8bbbd5f0
- Child:
- 3:3914abc1330d
super final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co657_ne75 | 0:0f5f8d4652d3 | 1 | #include "mbed.h" |
co657_ne75 | 0:0f5f8d4652d3 | 2 | #include "C12832.h" |
co657_ne75 | 0:0f5f8d4652d3 | 3 | #include <string> |
co657_ne75 | 0:0f5f8d4652d3 | 4 | |
co657_ne75 | 0:0f5f8d4652d3 | 5 | using namespace std; |
co657_ne75 | 0:0f5f8d4652d3 | 6 | |
co657_ne75 | 0:0f5f8d4652d3 | 7 | /* |
co657_ne75 | 0:0f5f8d4652d3 | 8 | * Outputs |
co657_ne75 | 0:0f5f8d4652d3 | 9 | */ |
co657_ne75 | 0:0f5f8d4652d3 | 10 | C12832 lcd(D11, D13, D12, D7, D10); |
co657_ne75 | 0:0f5f8d4652d3 | 11 | Serial host (USBTX, USBRX); |
co657_ne75 | 0:0f5f8d4652d3 | 12 | PwmOut speaker(D6); |
co657_ne75 | 0:0f5f8d4652d3 | 13 | |
co657_ne75 | 0:0f5f8d4652d3 | 14 | InterruptIn sw3(PTA4); |
co657_ne75 | 2:abf92acbdd90 | 15 | InterruptIn sw2(PTC6); |
co657_ne75 | 0:0f5f8d4652d3 | 16 | |
co657_ne75 | 0:0f5f8d4652d3 | 17 | volatile int bufferInsert = 0; |
co657_ne75 | 0:0f5f8d4652d3 | 18 | volatile int currentBufferSize = 0; |
co657_ne75 | 0:0f5f8d4652d3 | 19 | const int bufferSize = 512; |
co657_ne75 | 0:0f5f8d4652d3 | 20 | char rxBuffer[bufferSize]; |
co657_ne75 | 0:0f5f8d4652d3 | 21 | //Ascii code for "-", it is used to end messages |
co657_ne75 | 0:0f5f8d4652d3 | 22 | const char dash = 45; |
co657_ne75 | 0:0f5f8d4652d3 | 23 | |
co657_ne75 | 0:0f5f8d4652d3 | 24 | void rxInterrupt(void){ |
co657_ne75 | 0:0f5f8d4652d3 | 25 | //Reads all available data from the serial into the buffer as long as the buffer is not full |
co657_ne75 | 0:0f5f8d4652d3 | 26 | while ((host.readable()) && (currentBufferSize != bufferSize)){ |
co657_ne75 | 0:0f5f8d4652d3 | 27 | rxBuffer[bufferInsert] = host.getc(); |
co657_ne75 | 0:0f5f8d4652d3 | 28 | bufferInsert = (bufferInsert + 1) % bufferSize; |
co657_ne75 | 0:0f5f8d4652d3 | 29 | currentBufferSize++; |
co657_ne75 | 0:0f5f8d4652d3 | 30 | } |
co657_ne75 | 0:0f5f8d4652d3 | 31 | int index = (bufferInsert - currentBufferSize) % bufferSize; |
co657_ne75 | 0:0f5f8d4652d3 | 32 | string message = ""; |
co657_ne75 | 0:0f5f8d4652d3 | 33 | //Looks for a message in buffer |
co657_ne75 | 0:0f5f8d4652d3 | 34 | while (index != bufferInsert){ |
co657_ne75 | 0:0f5f8d4652d3 | 35 | if(rxBuffer[index] == dash){ |
co657_ne75 | 0:0f5f8d4652d3 | 36 | //everything between (bufferInsert - currentBufferSize) and index is the message |
co657_ne75 | 0:0f5f8d4652d3 | 37 | //Removes the message from the buffer |
co657_ne75 | 0:0f5f8d4652d3 | 38 | while(((bufferInsert - currentBufferSize) % bufferSize) != index){ |
co657_ne75 | 0:0f5f8d4652d3 | 39 | message.append(string(1, rxBuffer[((bufferInsert - currentBufferSize) % bufferSize)])); |
co657_ne75 | 0:0f5f8d4652d3 | 40 | currentBufferSize--; |
co657_ne75 | 0:0f5f8d4652d3 | 41 | } |
co657_ne75 | 0:0f5f8d4652d3 | 42 | //The last character will be "-", so it removes it from the buffer |
co657_ne75 | 0:0f5f8d4652d3 | 43 | currentBufferSize--; |
co657_ne75 | 0:0f5f8d4652d3 | 44 | break; |
co657_ne75 | 0:0f5f8d4652d3 | 45 | } |
co657_ne75 | 0:0f5f8d4652d3 | 46 | index = (index + 1) % bufferSize; |
co657_ne75 | 0:0f5f8d4652d3 | 47 | } |
co657_ne75 | 1:469f8bbbd5f0 | 48 | if(message == "jordan ping" || message == "arez ping"){ |
co657_ne75 | 0:0f5f8d4652d3 | 49 | lcd.cls(); |
co657_ne75 | 0:0f5f8d4652d3 | 50 | lcd.locate(0,10); |
co657_ne75 | 0:0f5f8d4652d3 | 51 | lcd.printf("%sed you", message.c_str()); |
co657_ne75 | 0:0f5f8d4652d3 | 52 | for (float i=2000.0f; i<10000.0f; i+=100) { |
co657_ne75 | 0:0f5f8d4652d3 | 53 | speaker.period(1.0f/i); |
co657_ne75 | 0:0f5f8d4652d3 | 54 | speaker=0.5; |
co657_ne75 | 0:0f5f8d4652d3 | 55 | wait(0.02); |
co657_ne75 | 0:0f5f8d4652d3 | 56 | } |
co657_ne75 | 0:0f5f8d4652d3 | 57 | speaker=0.0; |
co657_ne75 | 0:0f5f8d4652d3 | 58 | } |
co657_ne75 | 0:0f5f8d4652d3 | 59 | } |
co657_ne75 | 0:0f5f8d4652d3 | 60 | |
co657_ne75 | 0:0f5f8d4652d3 | 61 | void sw3interrupt(void){ |
co657_ne75 | 0:0f5f8d4652d3 | 62 | host.printf("niklas ping-"); |
co657_ne75 | 0:0f5f8d4652d3 | 63 | } |
co657_ne75 | 0:0f5f8d4652d3 | 64 | |
co657_ne75 | 2:abf92acbdd90 | 65 | void sw2interrupt(void){ |
co657_ne75 | 2:abf92acbdd90 | 66 | lcd.cls(); |
co657_ne75 | 2:abf92acbdd90 | 67 | } |
co657_ne75 | 2:abf92acbdd90 | 68 | |
co657_ne75 | 0:0f5f8d4652d3 | 69 | int main(void){ |
co657_ne75 | 0:0f5f8d4652d3 | 70 | host.baud(38400); |
co657_ne75 | 0:0f5f8d4652d3 | 71 | |
co657_ne75 | 0:0f5f8d4652d3 | 72 | sw3.mode(PullUp); |
co657_ne75 | 0:0f5f8d4652d3 | 73 | sw3.fall(&sw3interrupt); |
co657_ne75 | 0:0f5f8d4652d3 | 74 | |
co657_ne75 | 2:abf92acbdd90 | 75 | sw2.mode(PullUp); |
co657_ne75 | 2:abf92acbdd90 | 76 | sw2.fall(&sw2interrupt); |
co657_ne75 | 2:abf92acbdd90 | 77 | |
co657_ne75 | 0:0f5f8d4652d3 | 78 | host.attach(&rxInterrupt, Serial::RxIrq); |
co657_ne75 | 0:0f5f8d4652d3 | 79 | |
co657_ne75 | 0:0f5f8d4652d3 | 80 | for(;;){ |
co657_ne75 | 0:0f5f8d4652d3 | 81 | wait(0.1); |
co657_ne75 | 0:0f5f8d4652d3 | 82 | } |
co657_ne75 | 0:0f5f8d4652d3 | 83 | } |