niklas ekstrand
/
co657_ass5_aggregation_and_visualisation
iotass5
main.cpp@0:0f5f8d4652d3, 2016-01-21 (annotated)
- Committer:
- co657_ne75
- Date:
- Thu Jan 21 23:25:46 2016 +0000
- Revision:
- 0:0f5f8d4652d3
- Child:
- 1:469f8bbbd5f0
Done, i think.
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 | 0:0f5f8d4652d3 | 15 | |
co657_ne75 | 0:0f5f8d4652d3 | 16 | volatile int bufferInsert = 0; |
co657_ne75 | 0:0f5f8d4652d3 | 17 | volatile int currentBufferSize = 0; |
co657_ne75 | 0:0f5f8d4652d3 | 18 | const int bufferSize = 512; |
co657_ne75 | 0:0f5f8d4652d3 | 19 | char rxBuffer[bufferSize]; |
co657_ne75 | 0:0f5f8d4652d3 | 20 | //Ascii code for "-", it is used to end messages |
co657_ne75 | 0:0f5f8d4652d3 | 21 | const char dash = 45; |
co657_ne75 | 0:0f5f8d4652d3 | 22 | |
co657_ne75 | 0:0f5f8d4652d3 | 23 | void rxInterrupt(void){ |
co657_ne75 | 0:0f5f8d4652d3 | 24 | //Reads all available data from the serial into the buffer as long as the buffer is not full |
co657_ne75 | 0:0f5f8d4652d3 | 25 | while ((host.readable()) && (currentBufferSize != bufferSize)){ |
co657_ne75 | 0:0f5f8d4652d3 | 26 | rxBuffer[bufferInsert] = host.getc(); |
co657_ne75 | 0:0f5f8d4652d3 | 27 | bufferInsert = (bufferInsert + 1) % bufferSize; |
co657_ne75 | 0:0f5f8d4652d3 | 28 | currentBufferSize++; |
co657_ne75 | 0:0f5f8d4652d3 | 29 | } |
co657_ne75 | 0:0f5f8d4652d3 | 30 | int index = (bufferInsert - currentBufferSize) % bufferSize; |
co657_ne75 | 0:0f5f8d4652d3 | 31 | string message = ""; |
co657_ne75 | 0:0f5f8d4652d3 | 32 | //Looks for a message in buffer |
co657_ne75 | 0:0f5f8d4652d3 | 33 | while (index != bufferInsert){ |
co657_ne75 | 0:0f5f8d4652d3 | 34 | if(rxBuffer[index] == dash){ |
co657_ne75 | 0:0f5f8d4652d3 | 35 | //everything between (bufferInsert - currentBufferSize) and index is the message |
co657_ne75 | 0:0f5f8d4652d3 | 36 | //Removes the message from the buffer |
co657_ne75 | 0:0f5f8d4652d3 | 37 | while(((bufferInsert - currentBufferSize) % bufferSize) != index){ |
co657_ne75 | 0:0f5f8d4652d3 | 38 | message.append(string(1, rxBuffer[((bufferInsert - currentBufferSize) % bufferSize)])); |
co657_ne75 | 0:0f5f8d4652d3 | 39 | currentBufferSize--; |
co657_ne75 | 0:0f5f8d4652d3 | 40 | } |
co657_ne75 | 0:0f5f8d4652d3 | 41 | //The last character will be "-", so it removes it from the buffer |
co657_ne75 | 0:0f5f8d4652d3 | 42 | currentBufferSize--; |
co657_ne75 | 0:0f5f8d4652d3 | 43 | break; |
co657_ne75 | 0:0f5f8d4652d3 | 44 | } |
co657_ne75 | 0:0f5f8d4652d3 | 45 | index = (index + 1) % bufferSize; |
co657_ne75 | 0:0f5f8d4652d3 | 46 | } |
co657_ne75 | 0:0f5f8d4652d3 | 47 | if(message == "jordan ping" || message == "arez ping" || message == "niklas ping"){ |
co657_ne75 | 0:0f5f8d4652d3 | 48 | lcd.cls(); |
co657_ne75 | 0:0f5f8d4652d3 | 49 | lcd.locate(0,10); |
co657_ne75 | 0:0f5f8d4652d3 | 50 | lcd.printf("%sed you", message.c_str()); |
co657_ne75 | 0:0f5f8d4652d3 | 51 | for (float i=2000.0f; i<10000.0f; i+=100) { |
co657_ne75 | 0:0f5f8d4652d3 | 52 | speaker.period(1.0f/i); |
co657_ne75 | 0:0f5f8d4652d3 | 53 | speaker=0.5; |
co657_ne75 | 0:0f5f8d4652d3 | 54 | wait(0.02); |
co657_ne75 | 0:0f5f8d4652d3 | 55 | } |
co657_ne75 | 0:0f5f8d4652d3 | 56 | speaker=0.0; |
co657_ne75 | 0:0f5f8d4652d3 | 57 | } |
co657_ne75 | 0:0f5f8d4652d3 | 58 | } |
co657_ne75 | 0:0f5f8d4652d3 | 59 | |
co657_ne75 | 0:0f5f8d4652d3 | 60 | void sw3interrupt(void){ |
co657_ne75 | 0:0f5f8d4652d3 | 61 | host.printf("niklas ping-"); |
co657_ne75 | 0:0f5f8d4652d3 | 62 | } |
co657_ne75 | 0:0f5f8d4652d3 | 63 | |
co657_ne75 | 0:0f5f8d4652d3 | 64 | int main(void){ |
co657_ne75 | 0:0f5f8d4652d3 | 65 | host.baud(38400); |
co657_ne75 | 0:0f5f8d4652d3 | 66 | |
co657_ne75 | 0:0f5f8d4652d3 | 67 | sw3.mode(PullUp); |
co657_ne75 | 0:0f5f8d4652d3 | 68 | sw3.fall(&sw3interrupt); |
co657_ne75 | 0:0f5f8d4652d3 | 69 | |
co657_ne75 | 0:0f5f8d4652d3 | 70 | host.attach(&rxInterrupt, Serial::RxIrq); |
co657_ne75 | 0:0f5f8d4652d3 | 71 | |
co657_ne75 | 0:0f5f8d4652d3 | 72 | for(;;){ |
co657_ne75 | 0:0f5f8d4652d3 | 73 | wait(0.1); |
co657_ne75 | 0:0f5f8d4652d3 | 74 | } |
co657_ne75 | 0:0f5f8d4652d3 | 75 | } |