AWind
TimeSerieBuffer.h
1 /*
2  AWind.h - Arduino window library support for Color TFT LCD Boards
3  Copyright (C)2015 Andrei Degtiarev. All right reserved
4 
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/AWind
8 
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the CC BY-NC-SA 3.0 license.
12  Please see the included documents for further information.
13 
14  Commercial use of this library requires you to buy a license that
15  will allow commercial use. This includes using the library,
16  modified or not, as a tool to sell products.
17 
18  The license applies to all part of the library including the
19  examples and tools supplied with the library.
20 */
21 #pragma once
22 
23 #include <math.h>
24 #include "IDataBuffer.h"
25 
27 class TimeSerieBuffer : public IDataBuffer
28 {
29  int _reserved_size;
30  int _size;
31  int *_data_y;
32  float _time_step;
33  float _factor_y;
34 public:
36 
42  TimeSerieBuffer(float time_step,float factor_y,int reserved_size,int size)
43  {
44  initialize(time_step,factor_y,reserved_size,size);
45  }
47 
52  TimeSerieBuffer(float time_step,float factor_y,int size)
53  {
54  initialize(time_step,factor_y,size,size);
55 
56  }
57 protected:
59  void initialize(float time_step,float factor_y,int reserved_size,int size)
60  {
61  _factor_y=factor_y;
62  _size=size;
63  _reserved_size=reserved_size;
64  _time_step=time_step;
65  _data_y=new int[reserved_size];
66  for(int i=0;i<_size;i++)
67  _data_y[i]=0;
68  }
69 public:
71  bool SetSize(int size)
72  {
73  if(size<=_reserved_size)
74  _size=size;
75  else
76  {
77  out<<F("Error: buffer size is too big")<<endln;
78  return false;
79  }
80  return true;
81  }
83  float SetTimeStep(float time_step)
84  {
85  _time_step=time_step;
86  }
88  float SetFactorY(float factor_y)
89  {
90  _factor_y=factor_y;
91  }
93 
97  void Set(unsigned int index,float value)
98  {
99  if(index>=Size())
100  {
101  out<<F("Error: index outside of array bounds: ")<<index<<endln;
102  return;
103  }
104  _data_y[index]=(value*_factor_y);
105  }
107  int *Y()
108  {
109  return _data_y;
110  }
112  void MinMax(float &min_x,float &max_x,float &min_y,float &max_y)
113  {
114  min_x=0;
115  max_x=_time_step*_size;
116  min_y=_data_y[_size-1];
117  max_y=_data_y[_size-1];
118  for(int i=0;i<_size;i++)
119  {
120  min_y=min(min_y,_data_y[i]);
121  max_y=max(max_y,_data_y[i]);
122  }
123  min_y/=_factor_y;
124  max_y/=_factor_y;
125  }
127  float X(unsigned int index)
128  {
129  if(index>=Size())
130  {
131  out<<F("Error: index outside of array bounds: ")<<index<<endln;
132  return 0;
133  }
134  return _time_step*index;
135  }
137  float Y(unsigned int index)
138  {
139  if(index>=Size())
140  {
141  out<<F("Error: index outside of array bounds: ")<<index<<endln;
142  return 0;
143  }
144  return _data_y[index]/_factor_y;
145  }
147  unsigned int StartIndex()
148  {
149  return 0;
150  }
152  unsigned int Size()
153  {
154  return _size;
155  }
156 };
Buffer for sensor data. It is used by chart control.
Definition: TimeSerieBuffer.h:27
int * Y()
Returns point to vaues array. It is used for direct access to internal buffer.
Definition: TimeSerieBuffer.h:107
void MinMax(float &min_x, float &max_x, float &min_y, float &max_y)
Returns min and max value for buffer data.
Definition: TimeSerieBuffer.h:112
bool SetSize(int size)
Sets actual buffer size. It has to be less or equal to reserved_size.
Definition: TimeSerieBuffer.h:71
float X(unsigned int index)
Returns X that is calculated from index in buffer (see time_step parameter)
Definition: TimeSerieBuffer.h:127
void initialize(float time_step, float factor_y, int reserved_size, int size)
Initialize internal buffer.
Definition: TimeSerieBuffer.h:59
unsigned int StartIndex()
Returns start index in buffer. For this class is always 0.
Definition: TimeSerieBuffer.h:147
float SetTimeStep(float time_step)
Sets time interval between samples in buffer.
Definition: TimeSerieBuffer.h:83
float Y(unsigned int index)
Returns scaled value Y from buffer for specified index.
Definition: TimeSerieBuffer.h:137
unsigned int Size()
Returns buffer size.
Definition: TimeSerieBuffer.h:152
float SetFactorY(float factor_y)
Sets scale factor for mesaurements.
Definition: TimeSerieBuffer.h:88
void Set(unsigned int index, float value)
Puts value into buffer.
Definition: TimeSerieBuffer.h:97
TimeSerieBuffer(float time_step, float factor_y, int reserved_size, int size)
Constructor.
Definition: TimeSerieBuffer.h:42
TimeSerieBuffer(float time_step, float factor_y, int size)
Constructor.
Definition: TimeSerieBuffer.h:52