Guillermo Stedile / RA8875

Dependencies:   GPS

Dependents:   SNOCC_V1 SNOCC_V2

Fork of RA8875 by SNOCC

Committer:
WiredHome
Date:
Sun Mar 23 16:27:55 2014 +0000
Revision:
63:ed787f5fcdc4
Adding PNG support - which works for tiny png files but memory requirements may exclude it for larger images.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 63:ed787f5fcdc4 1
WiredHome 63:ed787f5fcdc4 2 //#include <windows.h>
WiredHome 63:ed787f5fcdc4 3 #include "SlidingWindow.h"
WiredHome 63:ed787f5fcdc4 4 #include <mbed.h>
WiredHome 63:ed787f5fcdc4 5 //#include <stdio.h>
WiredHome 63:ed787f5fcdc4 6 //#include <stdlib.h>
WiredHome 63:ed787f5fcdc4 7
WiredHome 63:ed787f5fcdc4 8 // Define this to implement tracking code.
WiredHome 63:ed787f5fcdc4 9 //#define M_TRACK
WiredHome 63:ed787f5fcdc4 10
WiredHome 63:ed787f5fcdc4 11 SlidingWindow::SlidingWindow(int desiredSize, int allowedSize)
WiredHome 63:ed787f5fcdc4 12 {
WiredHome 63:ed787f5fcdc4 13 m_forewardUsed = 0;
WiredHome 63:ed787f5fcdc4 14 m_backwardUsed = 0;
WiredHome 63:ed787f5fcdc4 15 m_desiredSize = desiredSize;
WiredHome 63:ed787f5fcdc4 16 m_allowedSize = allowedSize;
WiredHome 63:ed787f5fcdc4 17 m_buf = (unsigned char *)malloc(allowedSize);
WiredHome 63:ed787f5fcdc4 18 if (m_buf) {
WiredHome 63:ed787f5fcdc4 19 m_status = noerror;
WiredHome 63:ed787f5fcdc4 20 } else {
WiredHome 63:ed787f5fcdc4 21 m_status = outofmemory;
WiredHome 63:ed787f5fcdc4 22 }
WiredHome 63:ed787f5fcdc4 23 #ifdef M_TRACK
WiredHome 63:ed787f5fcdc4 24 m_used = (unsigned char *)malloc(allowedSize);
WiredHome 63:ed787f5fcdc4 25 for (int i=0; i<allowedSize; i++)
WiredHome 63:ed787f5fcdc4 26 m_used[i] = 0;
WiredHome 63:ed787f5fcdc4 27 #endif
WiredHome 63:ed787f5fcdc4 28 }
WiredHome 63:ed787f5fcdc4 29 SlidingWindow::~SlidingWindow()
WiredHome 63:ed787f5fcdc4 30 {
WiredHome 63:ed787f5fcdc4 31 //char buf[100];
WiredHome 63:ed787f5fcdc4 32 //sprintf_s(buf, sizeof(buf), "forewardUsed: %d\n", m_forewardUsed);
WiredHome 63:ed787f5fcdc4 33 //OutputDebugString(buf);
WiredHome 63:ed787f5fcdc4 34 //sprintf_s(buf, sizeof(buf), "backwardUsed: %d\n", m_backwardUsed);
WiredHome 63:ed787f5fcdc4 35 //OutputDebugString(buf);
WiredHome 63:ed787f5fcdc4 36 free(m_buf);
WiredHome 63:ed787f5fcdc4 37 #ifdef M_TRACK
WiredHome 63:ed787f5fcdc4 38 free(m_used);
WiredHome 63:ed787f5fcdc4 39 #endif
WiredHome 63:ed787f5fcdc4 40 }
WiredHome 63:ed787f5fcdc4 41
WiredHome 63:ed787f5fcdc4 42
WiredHome 63:ed787f5fcdc4 43
WiredHome 63:ed787f5fcdc4 44 bool SlidingWindow::set(int x, unsigned char value)
WiredHome 63:ed787f5fcdc4 45 {
WiredHome 63:ed787f5fcdc4 46 if (x >= 0)
WiredHome 63:ed787f5fcdc4 47 {
WiredHome 63:ed787f5fcdc4 48 if (x >= m_allowedSize)
WiredHome 63:ed787f5fcdc4 49 {
WiredHome 63:ed787f5fcdc4 50 x %= m_allowedSize;
WiredHome 63:ed787f5fcdc4 51 }
WiredHome 63:ed787f5fcdc4 52
WiredHome 63:ed787f5fcdc4 53 m_buf[x] = value;
WiredHome 63:ed787f5fcdc4 54 #ifdef M_TRACK
WiredHome 63:ed787f5fcdc4 55 m_used[x] = 1;
WiredHome 63:ed787f5fcdc4 56 #endif
WiredHome 63:ed787f5fcdc4 57 if (x > m_forewardUsed)
WiredHome 63:ed787f5fcdc4 58 m_forewardUsed = x;
WiredHome 63:ed787f5fcdc4 59 return true;
WiredHome 63:ed787f5fcdc4 60 }
WiredHome 63:ed787f5fcdc4 61 else
WiredHome 63:ed787f5fcdc4 62 {
WiredHome 63:ed787f5fcdc4 63 m_buf[m_allowedSize + x] = value;
WiredHome 63:ed787f5fcdc4 64 #ifdef M_TRACK
WiredHome 63:ed787f5fcdc4 65 m_used[m_allowedSize + x] = 1;
WiredHome 63:ed787f5fcdc4 66 #endif
WiredHome 63:ed787f5fcdc4 67 return true;
WiredHome 63:ed787f5fcdc4 68 }
WiredHome 63:ed787f5fcdc4 69 }
WiredHome 63:ed787f5fcdc4 70 unsigned char SlidingWindow::get(int foreDistance, int backDistance)
WiredHome 63:ed787f5fcdc4 71 {
WiredHome 63:ed787f5fcdc4 72 int d = foreDistance - backDistance;
WiredHome 63:ed787f5fcdc4 73
WiredHome 63:ed787f5fcdc4 74 if (d >= 0)
WiredHome 63:ed787f5fcdc4 75 {
WiredHome 63:ed787f5fcdc4 76 if (d > m_allowedSize)
WiredHome 63:ed787f5fcdc4 77 d %= m_allowedSize; // instead of this, if m_allowedSize < m_desiredSize, and if memory is available, it could reallocate and fix itself
WiredHome 63:ed787f5fcdc4 78 #ifdef M_TRACK
WiredHome 63:ed787f5fcdc4 79 if (!m_used[d])
WiredHome 63:ed787f5fcdc4 80 ERR("buffer location %d was never set\n", d);
WiredHome 63:ed787f5fcdc4 81 #endif
WiredHome 63:ed787f5fcdc4 82 return m_buf[d];
WiredHome 63:ed787f5fcdc4 83 }
WiredHome 63:ed787f5fcdc4 84 else
WiredHome 63:ed787f5fcdc4 85 {
WiredHome 63:ed787f5fcdc4 86 if (d < m_backwardUsed)
WiredHome 63:ed787f5fcdc4 87 m_backwardUsed = d;
WiredHome 63:ed787f5fcdc4 88 while (d < -m_allowedSize)
WiredHome 63:ed787f5fcdc4 89 d %= m_allowedSize;
WiredHome 63:ed787f5fcdc4 90 //if (!m_used[m_allowedSize + d])
WiredHome 63:ed787f5fcdc4 91 //{
WiredHome 63:ed787f5fcdc4 92 // char buf[100];
WiredHome 63:ed787f5fcdc4 93 // sprintf_s(buf, sizeof(buf), "Accessing unitialized location [%d]\n", foreDistance - backDistance);
WiredHome 63:ed787f5fcdc4 94 // OutputDebugString(buf);
WiredHome 63:ed787f5fcdc4 95 //}
WiredHome 63:ed787f5fcdc4 96 return m_buf[m_allowedSize + d];
WiredHome 63:ed787f5fcdc4 97 }
WiredHome 63:ed787f5fcdc4 98 }