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