lhiggs CSUM
/
UM6_IMU_AHRS_2012
Communication program for the chrobotics UM6 9-DOF IMU AHRS.
MODSERIAL/RESIZE.cpp@0:03c649c76388, 2012-09-28 (annotated)
- Committer:
- lhiggs
- Date:
- Fri Sep 28 00:40:29 2012 +0000
- Revision:
- 0:03c649c76388
A UM6 IMU AHRS INTERFACE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lhiggs | 0:03c649c76388 | 1 | /* |
lhiggs | 0:03c649c76388 | 2 | Copyright (c) 2010 Andy Kirkham |
lhiggs | 0:03c649c76388 | 3 | |
lhiggs | 0:03c649c76388 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
lhiggs | 0:03c649c76388 | 5 | of this software and associated documentation files (the "Software"), to deal |
lhiggs | 0:03c649c76388 | 6 | in the Software without restriction, including without limitation the rights |
lhiggs | 0:03c649c76388 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
lhiggs | 0:03c649c76388 | 8 | copies of the Software, and to permit persons to whom the Software is |
lhiggs | 0:03c649c76388 | 9 | furnished to do so, subject to the following conditions: |
lhiggs | 0:03c649c76388 | 10 | |
lhiggs | 0:03c649c76388 | 11 | The above copyright notice and this permission notice shall be included in |
lhiggs | 0:03c649c76388 | 12 | all copies or substantial portions of the Software. |
lhiggs | 0:03c649c76388 | 13 | |
lhiggs | 0:03c649c76388 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
lhiggs | 0:03c649c76388 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
lhiggs | 0:03c649c76388 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
lhiggs | 0:03c649c76388 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
lhiggs | 0:03c649c76388 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
lhiggs | 0:03c649c76388 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
lhiggs | 0:03c649c76388 | 20 | THE SOFTWARE. |
lhiggs | 0:03c649c76388 | 21 | */ |
lhiggs | 0:03c649c76388 | 22 | |
lhiggs | 0:03c649c76388 | 23 | #include "MODSERIAL.h" |
lhiggs | 0:03c649c76388 | 24 | #include "MACROS.h" |
lhiggs | 0:03c649c76388 | 25 | |
lhiggs | 0:03c649c76388 | 26 | namespace AjK { |
lhiggs | 0:03c649c76388 | 27 | |
lhiggs | 0:03c649c76388 | 28 | int |
lhiggs | 0:03c649c76388 | 29 | MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check) |
lhiggs | 0:03c649c76388 | 30 | { |
lhiggs | 0:03c649c76388 | 31 | int rval = Ok; |
lhiggs | 0:03c649c76388 | 32 | |
lhiggs | 0:03c649c76388 | 33 | // If the requested size is the same as the current size there's nothing to do, |
lhiggs | 0:03c649c76388 | 34 | // just continue to use the same buffer as it's fine as it is. |
lhiggs | 0:03c649c76388 | 35 | if (buffer_size[type] == size) return rval; |
lhiggs | 0:03c649c76388 | 36 | |
lhiggs | 0:03c649c76388 | 37 | // Make sure the ISR cannot use the buffers while we are manipulating them. |
lhiggs | 0:03c649c76388 | 38 | disableIrq(); |
lhiggs | 0:03c649c76388 | 39 | |
lhiggs | 0:03c649c76388 | 40 | // If the requested buffer size is larger than the current size, |
lhiggs | 0:03c649c76388 | 41 | // attempt to create a new buffer and use it. |
lhiggs | 0:03c649c76388 | 42 | if (buffer_size[type] < size) { |
lhiggs | 0:03c649c76388 | 43 | rval = upSizeBuffer(size, type, memory_check); |
lhiggs | 0:03c649c76388 | 44 | } |
lhiggs | 0:03c649c76388 | 45 | else if (buffer_size[type] > size) { |
lhiggs | 0:03c649c76388 | 46 | rval = downSizeBuffer(size, type, memory_check); |
lhiggs | 0:03c649c76388 | 47 | } |
lhiggs | 0:03c649c76388 | 48 | |
lhiggs | 0:03c649c76388 | 49 | // Start the ISR system again with the new buffers. |
lhiggs | 0:03c649c76388 | 50 | enableIrq(); |
lhiggs | 0:03c649c76388 | 51 | |
lhiggs | 0:03c649c76388 | 52 | return rval; |
lhiggs | 0:03c649c76388 | 53 | } |
lhiggs | 0:03c649c76388 | 54 | |
lhiggs | 0:03c649c76388 | 55 | int |
lhiggs | 0:03c649c76388 | 56 | MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check) |
lhiggs | 0:03c649c76388 | 57 | { |
lhiggs | 0:03c649c76388 | 58 | if (size >= buffer_count[type]) { |
lhiggs | 0:03c649c76388 | 59 | return BufferOversize; |
lhiggs | 0:03c649c76388 | 60 | } |
lhiggs | 0:03c649c76388 | 61 | |
lhiggs | 0:03c649c76388 | 62 | char *s = (char *)malloc(size); |
lhiggs | 0:03c649c76388 | 63 | |
lhiggs | 0:03c649c76388 | 64 | if (s == (char *)NULL) { |
lhiggs | 0:03c649c76388 | 65 | if (memory_check) { |
lhiggs | 0:03c649c76388 | 66 | error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX"); |
lhiggs | 0:03c649c76388 | 67 | } |
lhiggs | 0:03c649c76388 | 68 | return NoMemory; |
lhiggs | 0:03c649c76388 | 69 | } |
lhiggs | 0:03c649c76388 | 70 | |
lhiggs | 0:03c649c76388 | 71 | int c, new_in = 0; |
lhiggs | 0:03c649c76388 | 72 | |
lhiggs | 0:03c649c76388 | 73 | do { |
lhiggs | 0:03c649c76388 | 74 | c = __getc(false); |
lhiggs | 0:03c649c76388 | 75 | if (c != -1) s[new_in++] = (char)c; |
lhiggs | 0:03c649c76388 | 76 | if (new_in >= size) new_in = 0; |
lhiggs | 0:03c649c76388 | 77 | } |
lhiggs | 0:03c649c76388 | 78 | while (c != -1); |
lhiggs | 0:03c649c76388 | 79 | |
lhiggs | 0:03c649c76388 | 80 | free((char *)buffer[type]); |
lhiggs | 0:03c649c76388 | 81 | buffer[type] = s; |
lhiggs | 0:03c649c76388 | 82 | buffer_in[type] = new_in; |
lhiggs | 0:03c649c76388 | 83 | buffer_out[type] = 0; |
lhiggs | 0:03c649c76388 | 84 | return Ok; |
lhiggs | 0:03c649c76388 | 85 | } |
lhiggs | 0:03c649c76388 | 86 | |
lhiggs | 0:03c649c76388 | 87 | int |
lhiggs | 0:03c649c76388 | 88 | MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check) |
lhiggs | 0:03c649c76388 | 89 | { |
lhiggs | 0:03c649c76388 | 90 | char *s = (char *)malloc(size); |
lhiggs | 0:03c649c76388 | 91 | |
lhiggs | 0:03c649c76388 | 92 | if (s == (char *)NULL) { |
lhiggs | 0:03c649c76388 | 93 | if (memory_check) { |
lhiggs | 0:03c649c76388 | 94 | error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX"); |
lhiggs | 0:03c649c76388 | 95 | } |
lhiggs | 0:03c649c76388 | 96 | return NoMemory; |
lhiggs | 0:03c649c76388 | 97 | } |
lhiggs | 0:03c649c76388 | 98 | |
lhiggs | 0:03c649c76388 | 99 | if (buffer_count[type] == 0) { // Current buffer empty? |
lhiggs | 0:03c649c76388 | 100 | free((char *)buffer[type]); |
lhiggs | 0:03c649c76388 | 101 | buffer[type] = s; |
lhiggs | 0:03c649c76388 | 102 | buffer_in[type] = 0; |
lhiggs | 0:03c649c76388 | 103 | buffer_out[type] = 0; |
lhiggs | 0:03c649c76388 | 104 | } |
lhiggs | 0:03c649c76388 | 105 | else { // Copy the current contents into the new buffer. |
lhiggs | 0:03c649c76388 | 106 | int c, new_in = 0; |
lhiggs | 0:03c649c76388 | 107 | do { |
lhiggs | 0:03c649c76388 | 108 | c = __getc(false); |
lhiggs | 0:03c649c76388 | 109 | if (c != -1) s[new_in++] = (char)c; |
lhiggs | 0:03c649c76388 | 110 | if (new_in >= size) new_in = 0; // Shouldn't happen, but be sure. |
lhiggs | 0:03c649c76388 | 111 | } |
lhiggs | 0:03c649c76388 | 112 | while (c != -1); |
lhiggs | 0:03c649c76388 | 113 | free((char *)buffer[type]); |
lhiggs | 0:03c649c76388 | 114 | buffer[type] = s; |
lhiggs | 0:03c649c76388 | 115 | buffer_in[type] = new_in; |
lhiggs | 0:03c649c76388 | 116 | buffer_out[type] = 0; |
lhiggs | 0:03c649c76388 | 117 | } |
lhiggs | 0:03c649c76388 | 118 | |
lhiggs | 0:03c649c76388 | 119 | buffer_size[type] = size; |
lhiggs | 0:03c649c76388 | 120 | return Ok; |
lhiggs | 0:03c649c76388 | 121 | } |
lhiggs | 0:03c649c76388 | 122 | |
lhiggs | 0:03c649c76388 | 123 | }; // namespace AjK ends |