Eli Hughes
/
RobotPowerLogger-V2
For Terrance
Code/DataTypes.c@0:085749c8446f, 2012-06-13 (annotated)
- Committer:
- emh203
- Date:
- Wed Jun 13 15:10:06 2012 +0000
- Revision:
- 0:085749c8446f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emh203 | 0:085749c8446f | 1 | #include "System.h" |
emh203 | 0:085749c8446f | 2 | |
emh203 | 0:085749c8446f | 3 | CHAR StringBuffer[256]; |
emh203 | 0:085749c8446f | 4 | |
emh203 | 0:085749c8446f | 5 | SIGNED_WORD ByteStackPush(ByteStack * Stack,BYTE Val) { |
emh203 | 0:085749c8446f | 6 | if (Stack->Ptr == Stack->Size-1) { |
emh203 | 0:085749c8446f | 7 | return STACK_FULL; |
emh203 | 0:085749c8446f | 8 | } else { |
emh203 | 0:085749c8446f | 9 | Stack->Ptr++; |
emh203 | 0:085749c8446f | 10 | Stack->StackSpace[Stack->Ptr] = Val; |
emh203 | 0:085749c8446f | 11 | return STACK_PUSH_OK; |
emh203 | 0:085749c8446f | 12 | } |
emh203 | 0:085749c8446f | 13 | } |
emh203 | 0:085749c8446f | 14 | |
emh203 | 0:085749c8446f | 15 | SIGNED_WORD ByteStackPOP(ByteStack * Stack) { |
emh203 | 0:085749c8446f | 16 | if (Stack->Ptr == 0) { |
emh203 | 0:085749c8446f | 17 | return STACK_EMPTY; |
emh203 | 0:085749c8446f | 18 | } else { |
emh203 | 0:085749c8446f | 19 | Stack->Ptr--; |
emh203 | 0:085749c8446f | 20 | return Stack->StackSpace[Stack->Ptr]; |
emh203 | 0:085749c8446f | 21 | } |
emh203 | 0:085749c8446f | 22 | } |
emh203 | 0:085749c8446f | 23 | |
emh203 | 0:085749c8446f | 24 | SIGNED_BYTE BitStackPush(BitStack * Stack,BOOL Val) { |
emh203 | 0:085749c8446f | 25 | WORD Offset; |
emh203 | 0:085749c8446f | 26 | BYTE Mask; |
emh203 | 0:085749c8446f | 27 | |
emh203 | 0:085749c8446f | 28 | if (Stack->Ptr == Stack->Size-1) { |
emh203 | 0:085749c8446f | 29 | return STACK_FULL; |
emh203 | 0:085749c8446f | 30 | } else { |
emh203 | 0:085749c8446f | 31 | |
emh203 | 0:085749c8446f | 32 | Stack->Ptr++; |
emh203 | 0:085749c8446f | 33 | Offset = (Stack->Ptr)>>3; |
emh203 | 0:085749c8446f | 34 | Mask = 0x01<<(Stack->Ptr&0x07); |
emh203 | 0:085749c8446f | 35 | |
emh203 | 0:085749c8446f | 36 | if (Val) { |
emh203 | 0:085749c8446f | 37 | Stack->StackSpace[Offset] |= Mask; |
emh203 | 0:085749c8446f | 38 | } else { |
emh203 | 0:085749c8446f | 39 | Stack->StackSpace[Offset] &= ~Mask; |
emh203 | 0:085749c8446f | 40 | } |
emh203 | 0:085749c8446f | 41 | |
emh203 | 0:085749c8446f | 42 | return STACK_PUSH_OK; |
emh203 | 0:085749c8446f | 43 | } |
emh203 | 0:085749c8446f | 44 | } |
emh203 | 0:085749c8446f | 45 | |
emh203 | 0:085749c8446f | 46 | SIGNED_BYTE BitStackPop(BitStack * Stack) { |
emh203 | 0:085749c8446f | 47 | WORD Offset; |
emh203 | 0:085749c8446f | 48 | BYTE Mask; |
emh203 | 0:085749c8446f | 49 | |
emh203 | 0:085749c8446f | 50 | if (Stack->Ptr == 0) { |
emh203 | 0:085749c8446f | 51 | return STACK_EMPTY; |
emh203 | 0:085749c8446f | 52 | } else { |
emh203 | 0:085749c8446f | 53 | |
emh203 | 0:085749c8446f | 54 | Stack->Ptr++; |
emh203 | 0:085749c8446f | 55 | Offset = (Stack->Ptr)>>3; |
emh203 | 0:085749c8446f | 56 | Mask = 0x01<<(Stack->Ptr&0x07); |
emh203 | 0:085749c8446f | 57 | |
emh203 | 0:085749c8446f | 58 | if (Stack->StackSpace[Offset] | Mask) { |
emh203 | 0:085749c8446f | 59 | return TRUE; |
emh203 | 0:085749c8446f | 60 | } else { |
emh203 | 0:085749c8446f | 61 | return FALSE; |
emh203 | 0:085749c8446f | 62 | } |
emh203 | 0:085749c8446f | 63 | } |
emh203 | 0:085749c8446f | 64 | } |
emh203 | 0:085749c8446f | 65 | |
emh203 | 0:085749c8446f | 66 | #ifndef INLINE_BITPLANE_PUT |
emh203 | 0:085749c8446f | 67 | void BitPlane_Put(BitPlane * BP, WORD X,WORD Y, BOOL Value) |
emh203 | 0:085749c8446f | 68 | { |
emh203 | 0:085749c8446f | 69 | WORD Offset; |
emh203 | 0:085749c8446f | 70 | BYTE Mask; |
emh203 | 0:085749c8446f | 71 | |
emh203 | 0:085749c8446f | 72 | Offset = (Y * ((BP->SizeX)>>3)) + (X>>3); |
emh203 | 0:085749c8446f | 73 | Mask = 0x01 << (X & 0x07); |
emh203 | 0:085749c8446f | 74 | |
emh203 | 0:085749c8446f | 75 | if(Value) |
emh203 | 0:085749c8446f | 76 | { |
emh203 | 0:085749c8446f | 77 | BP->BitPlaneSpace[Offset] |= Mask; |
emh203 | 0:085749c8446f | 78 | } |
emh203 | 0:085749c8446f | 79 | else |
emh203 | 0:085749c8446f | 80 | { |
emh203 | 0:085749c8446f | 81 | BP->BitPlaneSpace[Offset] &= ~Mask; |
emh203 | 0:085749c8446f | 82 | } |
emh203 | 0:085749c8446f | 83 | } |
emh203 | 0:085749c8446f | 84 | #endif |
emh203 | 0:085749c8446f | 85 | |
emh203 | 0:085749c8446f | 86 | #ifndef INLINE_BITPLANE_GET |
emh203 | 0:085749c8446f | 87 | BOOL BitPlane_Get(BitPlane * BP, WORD X,WORD Y) |
emh203 | 0:085749c8446f | 88 | { |
emh203 | 0:085749c8446f | 89 | WORD Offset; |
emh203 | 0:085749c8446f | 90 | BYTE Mask; |
emh203 | 0:085749c8446f | 91 | |
emh203 | 0:085749c8446f | 92 | Offset = (Y * ((BP->SizeX)>>3)) + (X>>3); |
emh203 | 0:085749c8446f | 93 | Mask = 0x01 << (X & 0x07); |
emh203 | 0:085749c8446f | 94 | |
emh203 | 0:085749c8446f | 95 | if((BP->BitPlaneSpace[Offset])&Mask) |
emh203 | 0:085749c8446f | 96 | { |
emh203 | 0:085749c8446f | 97 | return TRUE; |
emh203 | 0:085749c8446f | 98 | } |
emh203 | 0:085749c8446f | 99 | else |
emh203 | 0:085749c8446f | 100 | { |
emh203 | 0:085749c8446f | 101 | return FALSE; |
emh203 | 0:085749c8446f | 102 | } |
emh203 | 0:085749c8446f | 103 | } |
emh203 | 0:085749c8446f | 104 | #endif |
emh203 | 0:085749c8446f | 105 | void BitPlane_Clear(BitPlane * BP) { |
emh203 | 0:085749c8446f | 106 | WORD PlaneSpaceSize; |
emh203 | 0:085749c8446f | 107 | WORD i; |
emh203 | 0:085749c8446f | 108 | |
emh203 | 0:085749c8446f | 109 | PlaneSpaceSize = ((BP->SizeX)>>3) * BP->SizeY; |
emh203 | 0:085749c8446f | 110 | |
emh203 | 0:085749c8446f | 111 | for (i=0;i<PlaneSpaceSize;i++) { |
emh203 | 0:085749c8446f | 112 | BP->BitPlaneSpace[i] = 0; |
emh203 | 0:085749c8446f | 113 | } |
emh203 | 0:085749c8446f | 114 | } |
emh203 | 0:085749c8446f | 115 | |
emh203 | 0:085749c8446f | 116 | |
emh203 | 0:085749c8446f | 117 | |
emh203 | 0:085749c8446f | 118 | void InitByteQueue(ByteQueue *BQ,WORD Size,BYTE * Storage) { |
emh203 | 0:085749c8446f | 119 | WORD i; |
emh203 | 0:085749c8446f | 120 | |
emh203 | 0:085749c8446f | 121 | BQ->QueueSize = Size; |
emh203 | 0:085749c8446f | 122 | BQ->ReadPtr=0; |
emh203 | 0:085749c8446f | 123 | BQ->WritePtr=0; |
emh203 | 0:085749c8446f | 124 | BQ->QueueStorage = Storage; |
emh203 | 0:085749c8446f | 125 | |
emh203 | 0:085749c8446f | 126 | for (i=0;i<BQ->QueueSize;i++) { |
emh203 | 0:085749c8446f | 127 | BQ->QueueStorage[i] = 0; |
emh203 | 0:085749c8446f | 128 | } |
emh203 | 0:085749c8446f | 129 | } |
emh203 | 0:085749c8446f | 130 | |
emh203 | 0:085749c8446f | 131 | WORD BytesInQueue(ByteQueue *BQ) { |
emh203 | 0:085749c8446f | 132 | if (BQ->ReadPtr > BQ->WritePtr) { |
emh203 | 0:085749c8446f | 133 | return (BQ->QueueSize - BQ->ReadPtr + BQ->WritePtr); |
emh203 | 0:085749c8446f | 134 | } else if (BQ->WritePtr > BQ->ReadPtr) { |
emh203 | 0:085749c8446f | 135 | return (BQ->WritePtr - BQ->ReadPtr); |
emh203 | 0:085749c8446f | 136 | } else { |
emh203 | 0:085749c8446f | 137 | return 0; |
emh203 | 0:085749c8446f | 138 | } |
emh203 | 0:085749c8446f | 139 | } |
emh203 | 0:085749c8446f | 140 | |
emh203 | 0:085749c8446f | 141 | SIGNED_WORD ByteEnqueue(ByteQueue *BQ,BYTE Val) { |
emh203 | 0:085749c8446f | 142 | if (BytesInQueue(BQ) == BQ->QueueSize) { |
emh203 | 0:085749c8446f | 143 | return QUEUE_FULL; |
emh203 | 0:085749c8446f | 144 | } else { |
emh203 | 0:085749c8446f | 145 | BQ->QueueStorage[BQ->WritePtr] = Val; |
emh203 | 0:085749c8446f | 146 | BQ->WritePtr++; |
emh203 | 0:085749c8446f | 147 | |
emh203 | 0:085749c8446f | 148 | if (BQ->WritePtr >= BQ->QueueSize) { |
emh203 | 0:085749c8446f | 149 | BQ->WritePtr = 0; |
emh203 | 0:085749c8446f | 150 | } |
emh203 | 0:085749c8446f | 151 | return QUEUE_OK; |
emh203 | 0:085749c8446f | 152 | } |
emh203 | 0:085749c8446f | 153 | } |
emh203 | 0:085749c8446f | 154 | |
emh203 | 0:085749c8446f | 155 | SIGNED_WORD ByteArrayEnqueue(ByteQueue *BQ,BYTE *Buf,WORD Len) { |
emh203 | 0:085749c8446f | 156 | WORD i; |
emh203 | 0:085749c8446f | 157 | for (i=0;i<Len;i++) { |
emh203 | 0:085749c8446f | 158 | ByteEnqueue(BQ,Buf[i]); |
emh203 | 0:085749c8446f | 159 | } |
emh203 | 0:085749c8446f | 160 | return QUEUE_OK; |
emh203 | 0:085749c8446f | 161 | } |
emh203 | 0:085749c8446f | 162 | |
emh203 | 0:085749c8446f | 163 | SIGNED_WORD PrintfEnqueue(ByteQueue *BQ, const char *FormatString,...) |
emh203 | 0:085749c8446f | 164 | { |
emh203 | 0:085749c8446f | 165 | |
emh203 | 0:085749c8446f | 166 | va_list argptr; |
emh203 | 0:085749c8446f | 167 | va_start(argptr,FormatString); |
emh203 | 0:085749c8446f | 168 | vsprintf((CHAR *)StringBuffer,FormatString,argptr); |
emh203 | 0:085749c8446f | 169 | va_end(argptr); |
emh203 | 0:085749c8446f | 170 | |
emh203 | 0:085749c8446f | 171 | ByteArrayEnqueue(BQ,(BYTE *)StringBuffer,strlen(StringBuffer)); |
emh203 | 0:085749c8446f | 172 | |
emh203 | 0:085749c8446f | 173 | return QUEUE_OK; |
emh203 | 0:085749c8446f | 174 | } |
emh203 | 0:085749c8446f | 175 | |
emh203 | 0:085749c8446f | 176 | |
emh203 | 0:085749c8446f | 177 | |
emh203 | 0:085749c8446f | 178 | |
emh203 | 0:085749c8446f | 179 | SIGNED_WORD ByteDequeue(ByteQueue *BQ,BYTE *Val) { |
emh203 | 0:085749c8446f | 180 | |
emh203 | 0:085749c8446f | 181 | if (BytesInQueue(BQ) == 0) { |
emh203 | 0:085749c8446f | 182 | return QUEUE_EMPTY; |
emh203 | 0:085749c8446f | 183 | } else { |
emh203 | 0:085749c8446f | 184 | *Val = BQ->QueueStorage[BQ->ReadPtr]; |
emh203 | 0:085749c8446f | 185 | |
emh203 | 0:085749c8446f | 186 | BQ->ReadPtr++; |
emh203 | 0:085749c8446f | 187 | |
emh203 | 0:085749c8446f | 188 | if (BQ->ReadPtr >= BQ->QueueSize) { |
emh203 | 0:085749c8446f | 189 | BQ->ReadPtr = 0; |
emh203 | 0:085749c8446f | 190 | } |
emh203 | 0:085749c8446f | 191 | return QUEUE_OK; |
emh203 | 0:085749c8446f | 192 | } |
emh203 | 0:085749c8446f | 193 | } |