A talking braille-input text message device!!

Dependencies:   Braille_In PinDetect mbed

Committer:
aganger3
Date:
Fri Oct 12 18:05:42 2012 +0000
Revision:
1:4e1b66d0799f
Parent:
0:c79379695e18
Changed title in comment;

Who changed what in which revision?

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