AWind
SensorWindow.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 "SensorManager.h"
24 #include "TextBoxNumber.h"
25 #include "TextBoxString.h"
26 #include "ChartWindow.h"
27 #include "ViewModusWindow.h"
28 #include "ISensorHasDataEventReceiver.h"
29 
30 
31 extern uint8_t ArialNumFontPlus[];
32 extern uint8_t BigFont[];
33 extern uint8_t SmallFont[];
34 
36 class SensorWindow : public Window, ISensorHasDataEventReceiver
37 {
38 public:
39  static const int Margin=10;
40  static const int BigWindowHeight=70+Margin*2;
41  static const int SmallWindowHeight=BigWindowHeight/2-Margin/4;
42  static const int BigWindowWidth=135+Margin*2;
43  static const int SmallWindowWidth=BigWindowWidth/2-Margin/4;
44 
45  enum WindowSize
46  {
47  Big=0,
48  Small=1,
49  User
50  };
51 
52 protected:
53  enum VisMode
54  {
55  Text,
56  ChartSec,
57  ChartMin,
58  ChartHowr
59  };
60  SensorManager *_sensorManager;
61  TextBoxNumber *_textValue;
62  TextBoxFString *_textName;
63  TextBoxFString *_textChartAxis;
64  ChartWindow *_chartWnd;
65  VisMode _mode;
66 
67 public:
69 
76  SensorWindow(const __FlashStringHelper * name,SensorManager *sensorManager,int left,int top,WindowSize size=Big):Window(name,left,top,size == Big?BigWindowWidth:SmallWindowWidth,size == Big?BigWindowHeight:SmallWindowHeight)
77  {
78  Initialize(name,sensorManager,size,Width());
79  }
80  SensorWindow(const __FlashStringHelper * name,SensorManager *sensorManager,int left,int top,int width,int height):Window(name,left,top,width,height)
81  {
82  Initialize(name,sensorManager,User,BigWindowWidth);
83  }
84 protected:
85  void Initialize(const __FlashStringHelper * name,SensorManager *sensorManager,WindowSize size,int textBoxWidth)
86  {
87  _sensorManager = sensorManager;
88  _sensorManager->RegisterHasDataEventReceiver(this);
89  _mode = Text;
90  int offset = size != Small?Margin:Margin/2;
91  int first_font_height=size != Small?70:30;
92 
93 
94  _textValue = new TextBoxNumber(offset,offset,textBoxWidth-offset,first_font_height-offset,_sensorManager->Sensor()->Precission());
95  _textValue->SetFont(size != Small?ArialNumFontPlus:BigFont);
96  _textValue->SetStatus(false);
97 
98  _textName = new TextBoxFString(offset,first_font_height,textBoxWidth,1,name);
99  _textName->SetFont(size != Small?BigFont:SmallFont);
100 
101  int chart_height=Height()-15;
102  _textChartAxis=new TextBoxFString(Width()/2,chart_height,textBoxWidth,1,F(""));
103  _textChartAxis->SetFont(size != Small?BigFont:SmallFont);
104  int chart_offset_x=0;
105  _chartWnd = new ChartWindow(NULL,NULL,chart_offset_x,0,Width()-chart_offset_x,chart_height);
106  _chartWnd->SetVisible(false);
107 
108  this->AddChild(_textValue);
109  this->AddChild(_textName);
110  this->AddChild(_chartWnd);
111  this->AddChild(_textChartAxis);
112  }
113 public:
115  {
116  return true;
117  }
119  void SetDecorators(DecoratorList *decorators)
120  {
121  Window::SetDecorators(((ViewModusWindow *)Parent())->NormalSensorWndDecorators());
122  _textValue->SetDecorators(decorators);
123  _textName->SetDecorators(decorators);
124  _textChartAxis->SetDecorators(decorators);
125  _chartWnd->SetDecorators(((ViewModusWindow *)Parent())->ChartDecorators());
126  }
128  virtual bool OnTouch(int x, int y)
129  {
130  if(_mode == ChartHowr)
131  {
132  _textValue->SetVisible(true);
133  _textName->SetVisible(true);
134  _chartWnd->SetVisible(false);
135  _textChartAxis->SetVisible(false);
136  _mode=Text;
137  }
138  else if(_mode == Text)
139  {
140  _textValue->SetVisible(false);
141  _textName->SetVisible(false);
142  _chartWnd->SetVisible(true);
143  _textChartAxis->SetVisible(true);
144  _chartWnd->SetBuffer(_sensorManager->SecBuffer());
145  _textChartAxis->SetText(F("sec"));
146  _mode=ChartSec;
147  }
148  else if(_mode == ChartSec)
149  {
150  _chartWnd->SetBuffer(_sensorManager->MinBuffer());
151  _textChartAxis->SetText(F("min"));
152  _mode=ChartMin;
153  }
154  else if(_mode == ChartMin)
155  {
156  _chartWnd->SetBuffer(_sensorManager->HowrsBuffer());
157  _textChartAxis->SetText(F("howr"));
158  _mode=ChartHowr;
159  }
160  _textChartAxis->SetVisible(!_textValue->IsVisible());
161  Invalidate();
162  return true;
163  }
165  void NotifySensorHasData(SensorManager *sensorManager)
166  {
167  _textValue->SetStatus(_sensorManager->Status()!=Error);
168  float value=_sensorManager->GetData();
169  if(_sensorManager->Status()!=Error)
170  {
171  ViewModusWindow *viewModus=(ViewModusWindow *)Parent();
172  DecoratorList *prevDec=GetDecorators();
173  if(_sensorManager->Status() == ApplicationAlarm)
174  {
175  SetDecorators(viewModus->AlarmDecorators());
176  Window::SetDecorators(viewModus->AlarmDecorators());
177  }
178  else
179  SetDecorators(viewModus->NormalDecorators());
180  //_textValue->SetDecorators(GetDecorators());
181  if(prevDec != GetDecorators() && _mode == Text)
182  Invalidate();
183  }
184  if(_sensorManager->Status()!=Error && _textValue->GetNumber()!=value)
185  {
186  _textValue->SetNumber(value);
187  _mode == Text?_textValue->Invalidate():_chartWnd->Invalidate();
188  }
189  else if(_sensorManager->Status()!=Error && _mode == ChartSec)
190  _chartWnd->Invalidate();
191  }
192 };
virtual bool OnTouch(int x, int y)
Process touch event.
Definition: SensorWindow.h:128
DecoratorList * GetDecorators()
Returns window decorators list.
Definition: Window.h:81
Base class for all window objects. Provides basic window functionality.
Definition: Window.h:34
void SetText(T *text)
Initialize window with text.
Definition: TextBoxString.h:64
void SetFont(uint8_t *font)
Sets font.
Definition: TextBox.h:64
Main window that controls child window appearance (day/night/alarm) for windows like sensor window...
Definition: ViewModusWindow.h:27
Window * Parent()
Returns Parent window.
Definition: Window.h:200
Text box for numbers.
Definition: TextBoxNumber.h:24
Chart window implement XY plots.
Definition: ChartWindow.h:30
int Height()
Returns window height.
Definition: Window.h:189
void Invalidate()
If function is called than the window manager updates the window.
Definition: Window.h:157
bool IsAwaitTouch()
Returns true if window await touch action (like button) or false if touch manager should ignore this ...
Definition: SensorWindow.h:114
Window(const __FlashStringHelper *name, int left, int top, int width, int height)
Constructor.
Definition: Window.h:60
void NotifySensorHasData(SensorManager *sensorManager)
If sensor data was changed this notification is call.
Definition: SensorWindow.h:165
bool IsVisible()
Returns true if window visible and false is hidden.
Definition: Window.h:211
void AddChild(Window *window)
Adds window child window.
Definition: Window.h:194
void SetBuffer(IDataBuffer *buffer)
Sets data buffer.
Definition: ChartWindow.h:54
Window that visualizes data from sensor in from from text or chart with different time scala (See exa...
Definition: SensorWindow.h:36
void SetNumber(float number)
Initialize window with number.
Definition: TextBoxNumber.h:74
void SetVisible(bool isVisible)
Sets window visibility status.
Definition: Window.h:205
virtual void SetDecorators(DecoratorList *decorators)
Sets window decorators list.
Definition: Window.h:76
void SetDecorators(DecoratorList *decorators)
Sets appearance settings.
Definition: SensorWindow.h:119
int Width()
Returns window width.
Definition: Window.h:184
SensorWindow(const __FlashStringHelper *name, SensorManager *sensorManager, int left, int top, WindowSize size=Big)
Constructor.
Definition: SensorWindow.h:76