forked from switchdoclabs/OurWeatherWeatherPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSDL_RasPiGraphLibrary.cpp
106 lines (86 loc) · 2.67 KB
/
SDL_RasPiGraphLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
SDL_RasPiGraphLibrary.cpp - Class Library for RasPiConnect Graph Format.
Created by SwitchDoc Labs August 4, 2014.
Released into the public domain.
*/
#include "Arduino.h"
#include "SDL_RasPiGraphLibrary.h"
SDL_RasPiGraphLibrary::SDL_RasPiGraphLibrary(int maxItemCount, int Mode) {
_Mode = SDL_MODE_LABELS;
_currentItemCount = 0;
_maxItemCount = maxItemCount;
// Set up the value array.
// Calculate the size of the buffer to be able to store the number of elements.
// Allocate enough memory for "newelements" number of elements.
_myFloatArray = (float *) malloc(_maxItemCount * sizeof(float));
}
int SDL_RasPiGraphLibrary::add_value(float value) {
if (_currentItemCount == _maxItemCount) {
// We need to remove the first and add the last.
int i;
for (i = 1; i < _currentItemCount; i++) {
_myFloatArray[i - 1] = _myFloatArray[i];
}
_myFloatArray[_currentItemCount - 1] = value;
} else {
// Just add it on.
_myFloatArray[_currentItemCount] = value;
_currentItemCount++;
}
}
void SDL_RasPiGraphLibrary::getRasPiString(char returnRasPiString[], char *buffer) {
int i;
strcpy(returnRasPiString, "");
for (i = 0; i < _currentItemCount; i++) {
// Serial.print("_currentItemCount=");
// Serial.println(_currentItemCount);
char tempString[30];
char floatString[20];
dtostrf((double) _myFloatArray[i], 6, 2, floatString);
sprintf(tempString, "%s^^", floatString);
strcat(returnRasPiString, tempString);
// Serial.print("returnRasPiString =");
// Serial.println(returnRasPiString);
}
if (strlen(returnRasPiString) > 2) {
returnRasPiString[strlen(returnRasPiString) - 2] = '\0';
}
// Now add the labels.
strcat(returnRasPiString, "||");
for (i = 0; i < _currentItemCount; i++) {
char tempString[20];
sprintf(tempString, "%i^^", i);
strcat(returnRasPiString, tempString);
}
if (strlen(returnRasPiString) > 2) {
returnRasPiString[strlen(returnRasPiString) - 2] = '\0';
}
// Serial.print("returnRasPiString=");
// Serial.println(returnRasPiString);
strcpy(returnRasPiString, buffer);
}
void SDL_RasPiGraphLibrary::supply_labels(char *labels) {
}
float SDL_RasPiGraphLibrary::returnMaxValue() {
#define MINFLOAT ((float)-3.40282346638528860e+38)
float Maximum;
Maximum = MINFLOAT;
int i;
for (i = 0; i < _currentItemCount; i++) {
if (_myFloatArray[i] > Maximum) {
Maximum = _myFloatArray[i];
}
}
return Maximum;
}
float SDL_RasPiGraphLibrary::returnMinValue() {
float Minimum;
Minimum = MAXFLOAT;
int i;
for (i = 0; i < _currentItemCount; i++) {
if (_myFloatArray[i] < Minimum) {
Minimum = _myFloatArray[i];
}
}
return Minimum;
}