Dependencies:   PinDetect TextLCD mbed mRotaryEncoder

Committer:
cicklaus
Date:
Mon Feb 13 02:11:20 2012 +0000
Revision:
0:afb2650fb49a

        

Who changed what in which revision?

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