V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RESIZE.cpp Source File

RESIZE.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "MODSERIAL.h"
00024 #include "MACROS.h"
00025 
00026 namespace AjK {
00027 
00028 int
00029 MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check)
00030 {    
00031     // Make sure the ISR cannot use the buffers while we are manipulating them.
00032     NVIC_DisableIRQ(_IRQ);
00033     
00034     // If the requested size is the same as the current size there's nothing to do,
00035     // just continue to use the same buffer as it's fine as it is.
00036     if (buffer_size[type] == size)
00037     {
00038         NVIC_EnableIRQ(_IRQ);  
00039         return Ok ;
00040     }
00041     
00042     // is new buffer is big enough?
00043     if (size <= buffer_count[type])
00044     {
00045         NVIC_EnableIRQ(_IRQ);  
00046         return BufferOversize ;
00047     }
00048     
00049     // allocate new buffer
00050     char * newBuffer = (char*)malloc(size);
00051     
00052     // allocation failed?
00053     if (newBuffer == (char*)NULL)
00054     {
00055         if (memory_check)
00056             error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
00057             
00058         return NoMemory ;
00059     }
00060     
00061     // copy old buffer content to new one
00062     moveRingBuffer(newBuffer, type);
00063     
00064     // free old buffer and reset ring buffer cursor
00065     free((char*)buffer[type]);
00066         
00067     buffer[type]      = newBuffer;
00068     buffer_size[type] = size;
00069     buffer_in[type]   = buffer_count[type];
00070     buffer_out[type]  = 0;    
00071     
00072     // Start the ISR system again with the new buffers.
00073     NVIC_EnableIRQ(_IRQ);    
00074     return Ok ;
00075 }
00076 
00077 void MODSERIAL::moveRingBuffer(char * newBuffer, IrqType type)
00078 {   
00079     // copy old buffer content to new one
00080     if(buffer_in[type] > buffer_out[type])      
00081     {   // content in the middle of the ring buffer
00082         memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], buffer_count[type]);
00083     }  
00084     else if(buffer_in[type] < buffer_out[type]) 
00085     {   // content split, free space in the middle
00086         // copy last part of the old buffer
00087         int end_count= buffer_size[type] - buffer_out[type];
00088         memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], end_count);
00089         
00090         // copy first part of old buffer
00091         memcpy(&newBuffer[end_count], (char*)buffer[type], buffer_in[type]);
00092     }
00093     // else old buffer empty
00094 }
00095 
00096 }; // namespace AjK ends
00097