
HIT Project #3 https://community.freescale.com/docs/DOC-99621
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
MonkeyDo!
These are the demo files for Freescale HIT project #3: Monkey Do. It uses a FRDM-AUTO + a FRDM-K64F to demo websockets for a simple IoT application.
See the main MonkeyDo page for all of the schematics, videos, GitHub links, etc for everything else!
https://community.freescale.com/docs/DOC-99621
GLUE/Queue.cpp@0:29f58b9daa2c, 2014-07-04 (annotated)
- Committer:
- emh203
- Date:
- Fri Jul 04 22:55:47 2014 +0000
- Revision:
- 0:29f58b9daa2c
1st add
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emh203 | 0:29f58b9daa2c | 1 | #include "stdint.h" |
emh203 | 0:29f58b9daa2c | 2 | #include "Queue.h" |
emh203 | 0:29f58b9daa2c | 3 | #include <string.h> |
emh203 | 0:29f58b9daa2c | 4 | |
emh203 | 0:29f58b9daa2c | 5 | static char StringBuffer[256]; |
emh203 | 0:29f58b9daa2c | 6 | |
emh203 | 0:29f58b9daa2c | 7 | void InitByteQueue(ByteQueue *BQ,uint16_t Size,uint8_t * Storage) { |
emh203 | 0:29f58b9daa2c | 8 | uint16_t i; |
emh203 | 0:29f58b9daa2c | 9 | |
emh203 | 0:29f58b9daa2c | 10 | BQ->QueueSize = Size; |
emh203 | 0:29f58b9daa2c | 11 | BQ->ReadPtr=0; |
emh203 | 0:29f58b9daa2c | 12 | BQ->WritePtr=0; |
emh203 | 0:29f58b9daa2c | 13 | BQ->QueueStorage = Storage; |
emh203 | 0:29f58b9daa2c | 14 | |
emh203 | 0:29f58b9daa2c | 15 | for (i=0;i<BQ->QueueSize;i++) { |
emh203 | 0:29f58b9daa2c | 16 | BQ->QueueStorage[i] = 0; |
emh203 | 0:29f58b9daa2c | 17 | } |
emh203 | 0:29f58b9daa2c | 18 | } |
emh203 | 0:29f58b9daa2c | 19 | |
emh203 | 0:29f58b9daa2c | 20 | uint16_t BytesInQueue(ByteQueue *BQ) { |
emh203 | 0:29f58b9daa2c | 21 | if (BQ->ReadPtr > BQ->WritePtr) { |
emh203 | 0:29f58b9daa2c | 22 | return (BQ->QueueSize - BQ->ReadPtr + BQ->WritePtr); |
emh203 | 0:29f58b9daa2c | 23 | } else if (BQ->WritePtr > BQ->ReadPtr) { |
emh203 | 0:29f58b9daa2c | 24 | return (BQ->WritePtr - BQ->ReadPtr); |
emh203 | 0:29f58b9daa2c | 25 | } else { |
emh203 | 0:29f58b9daa2c | 26 | return 0; |
emh203 | 0:29f58b9daa2c | 27 | } |
emh203 | 0:29f58b9daa2c | 28 | } |
emh203 | 0:29f58b9daa2c | 29 | |
emh203 | 0:29f58b9daa2c | 30 | int16_t ByteEnqueue(ByteQueue *BQ,uint8_t Val) { |
emh203 | 0:29f58b9daa2c | 31 | if (BytesInQueue(BQ) == BQ->QueueSize - 1) { |
emh203 | 0:29f58b9daa2c | 32 | return QUEUE_FULL; |
emh203 | 0:29f58b9daa2c | 33 | } else { |
emh203 | 0:29f58b9daa2c | 34 | BQ->QueueStorage[BQ->WritePtr] = Val; |
emh203 | 0:29f58b9daa2c | 35 | BQ->WritePtr++; |
emh203 | 0:29f58b9daa2c | 36 | |
emh203 | 0:29f58b9daa2c | 37 | if (BQ->WritePtr >= BQ->QueueSize) { |
emh203 | 0:29f58b9daa2c | 38 | BQ->WritePtr = 0; |
emh203 | 0:29f58b9daa2c | 39 | } |
emh203 | 0:29f58b9daa2c | 40 | return QUEUE_OK; |
emh203 | 0:29f58b9daa2c | 41 | } |
emh203 | 0:29f58b9daa2c | 42 | } |
emh203 | 0:29f58b9daa2c | 43 | |
emh203 | 0:29f58b9daa2c | 44 | int16_t ByteArrayEnqueue(ByteQueue *BQ,uint8_t *Buf,uint16_t Len) { |
emh203 | 0:29f58b9daa2c | 45 | uint16_t i; |
emh203 | 0:29f58b9daa2c | 46 | for (i=0;i<Len;i++) { |
emh203 | 0:29f58b9daa2c | 47 | ByteEnqueue(BQ,Buf[i]); |
emh203 | 0:29f58b9daa2c | 48 | } |
emh203 | 0:29f58b9daa2c | 49 | return QUEUE_OK; |
emh203 | 0:29f58b9daa2c | 50 | } |
emh203 | 0:29f58b9daa2c | 51 | |
emh203 | 0:29f58b9daa2c | 52 | |
emh203 | 0:29f58b9daa2c | 53 | int16_t Qprintf(ByteQueue *BQ, const char *FormatString,...) |
emh203 | 0:29f58b9daa2c | 54 | { |
emh203 | 0:29f58b9daa2c | 55 | |
emh203 | 0:29f58b9daa2c | 56 | va_list argptr; |
emh203 | 0:29f58b9daa2c | 57 | va_start(argptr,FormatString); |
emh203 | 0:29f58b9daa2c | 58 | vsprintf((char *)StringBuffer,FormatString,argptr); |
emh203 | 0:29f58b9daa2c | 59 | va_end(argptr); |
emh203 | 0:29f58b9daa2c | 60 | |
emh203 | 0:29f58b9daa2c | 61 | return ByteArrayEnqueue(BQ,(uint8_t *)StringBuffer,strlen(StringBuffer)); |
emh203 | 0:29f58b9daa2c | 62 | } |
emh203 | 0:29f58b9daa2c | 63 | |
emh203 | 0:29f58b9daa2c | 64 | |
emh203 | 0:29f58b9daa2c | 65 | int16_t ByteDequeue(ByteQueue *BQ,uint8_t *Val) { |
emh203 | 0:29f58b9daa2c | 66 | |
emh203 | 0:29f58b9daa2c | 67 | if (BytesInQueue(BQ) == 0) { |
emh203 | 0:29f58b9daa2c | 68 | return QUEUE_EMPTY; |
emh203 | 0:29f58b9daa2c | 69 | } else { |
emh203 | 0:29f58b9daa2c | 70 | *Val = BQ->QueueStorage[BQ->ReadPtr]; |
emh203 | 0:29f58b9daa2c | 71 | |
emh203 | 0:29f58b9daa2c | 72 | BQ->ReadPtr++; |
emh203 | 0:29f58b9daa2c | 73 | |
emh203 | 0:29f58b9daa2c | 74 | if (BQ->ReadPtr >= BQ->QueueSize) { |
emh203 | 0:29f58b9daa2c | 75 | BQ->ReadPtr = 0; |
emh203 | 0:29f58b9daa2c | 76 | } |
emh203 | 0:29f58b9daa2c | 77 | return QUEUE_OK; |
emh203 | 0:29f58b9daa2c | 78 | } |
emh203 | 0:29f58b9daa2c | 79 | } |
emh203 | 0:29f58b9daa2c | 80 | |
emh203 | 0:29f58b9daa2c | 81 | uint8_t ForcedByteDequeue(ByteQueue *BQ) |
emh203 | 0:29f58b9daa2c | 82 | { |
emh203 | 0:29f58b9daa2c | 83 | uint8_t RetVal; |
emh203 | 0:29f58b9daa2c | 84 | |
emh203 | 0:29f58b9daa2c | 85 | if (BytesInQueue(BQ) == 0) { |
emh203 | 0:29f58b9daa2c | 86 | return 0; |
emh203 | 0:29f58b9daa2c | 87 | } else { |
emh203 | 0:29f58b9daa2c | 88 | RetVal = BQ->QueueStorage[BQ->ReadPtr]; |
emh203 | 0:29f58b9daa2c | 89 | |
emh203 | 0:29f58b9daa2c | 90 | BQ->ReadPtr++; |
emh203 | 0:29f58b9daa2c | 91 | |
emh203 | 0:29f58b9daa2c | 92 | if (BQ->ReadPtr >= BQ->QueueSize) { |
emh203 | 0:29f58b9daa2c | 93 | BQ->ReadPtr = 0; |
emh203 | 0:29f58b9daa2c | 94 | } |
emh203 | 0:29f58b9daa2c | 95 | return RetVal; |
emh203 | 0:29f58b9daa2c | 96 | } |
emh203 | 0:29f58b9daa2c | 97 | } |
emh203 | 0:29f58b9daa2c | 98 |