init
ComQueue.cpp@5:b2bc6e87890f, 2021-11-04 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Thu Nov 04 18:01:57 2021 +0000
- Revision:
- 5:b2bc6e87890f
- Parent:
- 0:c9f3777fe0b4
pub
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 0:c9f3777fe0b4 | 1 | //------------------------------------------------------------------------------------ |
gert_lauritsen | 0:c9f3777fe0b4 | 2 | // Title : General Q |
gert_lauritsen | 0:c9f3777fe0b4 | 3 | // Filename : Queue.c |
gert_lauritsen | 0:c9f3777fe0b4 | 4 | // Author : Gert Lauritsen |
gert_lauritsen | 0:c9f3777fe0b4 | 5 | // Origin Date : 21/04/2015 |
gert_lauritsen | 0:c9f3777fe0b4 | 6 | // Version : 1.000 |
gert_lauritsen | 0:c9f3777fe0b4 | 7 | // Compiler : Keil |
gert_lauritsen | 0:c9f3777fe0b4 | 8 | // Target : |
gert_lauritsen | 0:c9f3777fe0b4 | 9 | // Notes : None |
gert_lauritsen | 0:c9f3777fe0b4 | 10 | // |
gert_lauritsen | 0:c9f3777fe0b4 | 11 | //------------------------------------------------------------------------------------ |
gert_lauritsen | 0:c9f3777fe0b4 | 12 | //----------------------- MODULE REVISION LOG ---------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 13 | // |
gert_lauritsen | 0:c9f3777fe0b4 | 14 | // Date Software Version Initials Description |
gert_lauritsen | 0:c9f3777fe0b4 | 15 | // 21/04/2015 1.0.0.0 GL Module Created. |
gert_lauritsen | 0:c9f3777fe0b4 | 16 | // 22/05/2015 1.0.0.1 GL Translated to Mbed |
gert_lauritsen | 0:c9f3777fe0b4 | 17 | // |
gert_lauritsen | 0:c9f3777fe0b4 | 18 | //------------------------------------------------------------------------------------ |
gert_lauritsen | 0:c9f3777fe0b4 | 19 | // @file ComQueue.cpp |
gert_lauritsen | 0:c9f3777fe0b4 | 20 | // @brief This module contains the |
gert_lauritsen | 0:c9f3777fe0b4 | 21 | // @A que funtion for serial communiaction |
gert_lauritsen | 0:c9f3777fe0b4 | 22 | // |
gert_lauritsen | 0:c9f3777fe0b4 | 23 | //------------------------------------------------------------------------------------ |
gert_lauritsen | 0:c9f3777fe0b4 | 24 | // Includes |
gert_lauritsen | 0:c9f3777fe0b4 | 25 | //------------------------------------------------------------------------------------ |
gert_lauritsen | 0:c9f3777fe0b4 | 26 | #include <ComQueue.h> |
gert_lauritsen | 0:c9f3777fe0b4 | 27 | |
gert_lauritsen | 0:c9f3777fe0b4 | 28 | #define QSIZE 250 |
gert_lauritsen | 0:c9f3777fe0b4 | 29 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 30 | // Global VARIABLES |
gert_lauritsen | 0:c9f3777fe0b4 | 31 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 32 | typedef struct{ |
gert_lauritsen | 0:c9f3777fe0b4 | 33 | int cnt; |
gert_lauritsen | 0:c9f3777fe0b4 | 34 | int front; |
gert_lauritsen | 0:c9f3777fe0b4 | 35 | int rear; |
gert_lauritsen | 0:c9f3777fe0b4 | 36 | unsigned char que[QSIZE]; |
gert_lauritsen | 0:c9f3777fe0b4 | 37 | }QueueType; |
gert_lauritsen | 0:c9f3777fe0b4 | 38 | |
gert_lauritsen | 0:c9f3777fe0b4 | 39 | QueueType Serial; |
gert_lauritsen | 0:c9f3777fe0b4 | 40 | unsigned char Q_crc; |
gert_lauritsen | 0:c9f3777fe0b4 | 41 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 42 | // Queue Functions |
gert_lauritsen | 0:c9f3777fe0b4 | 43 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 44 | |
gert_lauritsen | 0:c9f3777fe0b4 | 45 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 46 | // Init_q |
gert_lauritsen | 0:c9f3777fe0b4 | 47 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 48 | void init_q(void) |
gert_lauritsen | 0:c9f3777fe0b4 | 49 | { |
gert_lauritsen | 0:c9f3777fe0b4 | 50 | Serial.cnt = 0; // set initial values |
gert_lauritsen | 0:c9f3777fe0b4 | 51 | Serial.front = 0; |
gert_lauritsen | 0:c9f3777fe0b4 | 52 | Serial.rear = 0; |
gert_lauritsen | 0:c9f3777fe0b4 | 53 | } |
gert_lauritsen | 0:c9f3777fe0b4 | 54 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 55 | // Put into Q |
gert_lauritsen | 0:c9f3777fe0b4 | 56 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 57 | unsigned char Put(unsigned char ch) |
gert_lauritsen | 0:c9f3777fe0b4 | 58 | { |
gert_lauritsen | 0:c9f3777fe0b4 | 59 | if(Serial.cnt >= QSIZE) return 0; // Put fails |
gert_lauritsen | 0:c9f3777fe0b4 | 60 | Serial.que[Serial.rear++] = ch; // copy character |
gert_lauritsen | 0:c9f3777fe0b4 | 61 | Q_crc ^=ch; // Runing calc of CRC |
gert_lauritsen | 0:c9f3777fe0b4 | 62 | if(Serial.rear >= QSIZE) Serial.rear = 0; |
gert_lauritsen | 0:c9f3777fe0b4 | 63 | Serial.cnt++; // increase count |
gert_lauritsen | 0:c9f3777fe0b4 | 64 | return 1; // report sucess |
gert_lauritsen | 0:c9f3777fe0b4 | 65 | } |
gert_lauritsen | 0:c9f3777fe0b4 | 66 | |
gert_lauritsen | 0:c9f3777fe0b4 | 67 | unsigned char Put16(unsigned int Wch) |
gert_lauritsen | 0:c9f3777fe0b4 | 68 | { |
gert_lauritsen | 0:c9f3777fe0b4 | 69 | union { |
gert_lauritsen | 0:c9f3777fe0b4 | 70 | char bytes[2]; |
gert_lauritsen | 0:c9f3777fe0b4 | 71 | unsigned int val; |
gert_lauritsen | 0:c9f3777fe0b4 | 72 | } Int2Byte; |
gert_lauritsen | 0:c9f3777fe0b4 | 73 | |
gert_lauritsen | 0:c9f3777fe0b4 | 74 | Int2Byte.val=Wch; |
gert_lauritsen | 0:c9f3777fe0b4 | 75 | Put(Int2Byte.bytes[0]); |
gert_lauritsen | 0:c9f3777fe0b4 | 76 | Put(Int2Byte.bytes[1]); |
gert_lauritsen | 0:c9f3777fe0b4 | 77 | |
gert_lauritsen | 0:c9f3777fe0b4 | 78 | return 1; |
gert_lauritsen | 0:c9f3777fe0b4 | 79 | } |
gert_lauritsen | 0:c9f3777fe0b4 | 80 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 81 | // Get From Q |
gert_lauritsen | 0:c9f3777fe0b4 | 82 | //----------------------------------------------------------------------------- |
gert_lauritsen | 0:c9f3777fe0b4 | 83 | unsigned char Get(unsigned char *ch) |
gert_lauritsen | 0:c9f3777fe0b4 | 84 | { |
gert_lauritsen | 0:c9f3777fe0b4 | 85 | if(Serial.cnt <= 0) return 0; // Get fails |
gert_lauritsen | 0:c9f3777fe0b4 | 86 | *ch = Serial.que[Serial.front++]; // remove front item |
gert_lauritsen | 0:c9f3777fe0b4 | 87 | if(Serial.front >= QSIZE) Serial.front = 0; |
gert_lauritsen | 0:c9f3777fe0b4 | 88 | Serial.cnt--; // reduce count |
gert_lauritsen | 0:c9f3777fe0b4 | 89 | return 1; // return sucess |
gert_lauritsen | 0:c9f3777fe0b4 | 90 | } |
gert_lauritsen | 0:c9f3777fe0b4 | 91 | |
gert_lauritsen | 0:c9f3777fe0b4 | 92 |