Windows Virtual Shields for Arduino
SensorModels.h
Go to the documentation of this file.
1 /*
2  Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.
3 
4  The MIT License(MIT)
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files(the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions :
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  THE SOFTWARE.
23 */
24 
25 #ifndef SensorModels_h
26 #define SensorModels_h
27 
28 typedef unsigned int UINT;
29 
31 {
32  Stop = 0,
33  Once = 1,
34  Start = 2,
36 };
37 
39 {
40  None = 0,
41  ProgPtr = 1,
42  MemPtr = 2,
43  Int = 3,
44  Uint = 4,
45  Double = 5,
46  Long = 6,
47  Bool = 7,
48  Char = 8,
50  ArrayEnd = 10,
51  ValueOnly = 11,
52  Format = 12,
53  Parse = 13
54 };
55 
56 union ARGB
57 {
58  uint32_t color;
59  struct
60  {
61  uint8_t blue, green, red, alpha;
62  };
63 
64  ARGB(byte alpha, byte red, byte green, byte blue) :
65  red(red), green(green), blue(blue), alpha(alpha)
66  {
67  }
68 
69  ARGB(byte red, byte green, byte blue) :
70  red(red), green(green), blue(blue), alpha(0)
71  {
72  }
73 
74  ARGB(unsigned long color) : color(color) {}
75 
76  ARGB(String hex) : ARGB((unsigned long)strtol(&hex[hex[0] == '#'], NULL, 16))
77  {
78  }
79 
80  void hex(char* hexSource)
81  {
82  char hex[9] =
83  { alpha >> 4, alpha & 0x0F,
84  red >> 4, red & 0x0F,
85  green >> 4, green & 0x0F,
86  blue >> 4, blue & 0x0F };
87 
88  for (int i = 0; i < 8; i++)
89  {
90  hexSource[i] = hex[i] + (hex[i] > 0x09 ? 0x37 : 0x30);
91  }
92 
93  hexSource[8] = 0;
94 
95  return;
96  }
97 };
98 
99 const bool AsText = true;
100 
101 struct EPtr
102 {
104  const char* key = 0;
105  union
106  {
107  const char* value = 0;
108  double doubleValue;
109  uint32_t uintValue;
110  int intValue;
111  long longValue;
112  bool boolValue;
113  char charValue;
114  };
115 
116  int length;
117  bool keyIsMem = false;
118  bool asText = false;
119  bool encoded = false;
120  EPtr* eptrs = 0;
121 
125  EPtr() {}
126 
131  EPtr(EPtrType ptrType) : ptrType(ptrType) {}
132 
137  EPtr(EPtrType ptrType, const char* key, EPtr* eptrs, int len) : ptrType(ptrType), key(key), intValue(len), eptrs(eptrs), asText(true) {}
138 
144  EPtr(EPtrType ptrType, const char* key) : ptrType(ptrType), key(key) {}
145 
152  EPtr(EPtrType ptrType, const char* key, const char* value) : ptrType(ptrType), key(key), value(value), asText(true), length(-1) {}
153 
159  EPtr(const char* key, const char* value) : key(key), value(value), asText(true), ptrType(ProgPtr) {}
160 
166  EPtr(const char* key, String value) : key(key), asText(true), ptrType(value ? MemPtr : None), length(-1)
167  {
168  this->value = value.c_str();
169  }
170 
176  EPtr(const char* key, const char value) : key(key), asText(true), ptrType(value ? Char : None), charValue(value) {}
177 
184  EPtr(const char* key, int value, EPtrType ptrType = Int) : key(key), intValue(value), ptrType(ptrType) {}
185 
192  EPtr(const char* key, uint32_t value, EPtrType ptrType = Uint) : key(key), uintValue(value), ptrType(ptrType) {}
193 
200  EPtr(const char* key, long value, EPtrType ptrType = Long) : key(key), longValue(value), ptrType(ptrType) {}
201 
208  EPtr(const char* key, double value, bool asText = false) : key(key), doubleValue(value), asText(asText), ptrType(Double) {}
209 
215  EPtr(const char* key, bool value) : key(key), boolValue(value), ptrType(Bool) {}
216 
217  EPtr(const char* key, const char* value, int length) : key(key), value(value), ptrType(MemPtr), length(length) {}
218 
219  static int parse(const char* text, EPtr* eptrs, int length, const char separator = '|', int eptrStartIndex = 0)
220  {
221  int index = 0;
222  int start = 0;
223  int count = 0;
224  while (text[index] || index>start)
225  {
226  if (!text[index] || text[index] == separator)
227  {
228  eptrs[eptrStartIndex++] = EPtr(0, text + start, index - start);
229  start = index + 1;
230 
231  if (++count == length || !text[index])
232  {
233  break;
234  }
235  }
236 
237  index = index + 1;
238  }
239 
240  return count;
241  }
242 };
243 
244 #endif
Definition: SensorModels.h:34
ARGB(byte red, byte green, byte blue)
Definition: SensorModels.h:69
EPtr(const char *key, const char *value)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:159
SensorAction
Definition: SensorModels.h:30
bool keyIsMem
Definition: SensorModels.h:117
ARGB(unsigned long color)
Definition: SensorModels.h:74
Definition: SensorModels.h:50
EPtr(const char *key, bool value)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:215
Definition: SensorModels.h:52
bool encoded
Definition: SensorModels.h:119
int length
Definition: SensorModels.h:116
const char * value
Definition: SensorModels.h:107
EPtr(EPtrType ptrType, const char *key)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:144
EPtr(const char *key, double value, bool asText=false)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:208
Definition: SensorModels.h:42
Definition: SensorModels.h:46
unsigned int UINT
Definition: SensorModels.h:28
double doubleValue
Definition: SensorModels.h:108
ARGB(byte alpha, byte red, byte green, byte blue)
Definition: SensorModels.h:64
EPtr(const char *key, const char value)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:176
uint8_t blue
Definition: SensorModels.h:61
Definition: SensorModels.h:43
EPtr * eptrs
Definition: SensorModels.h:120
const char * key
Definition: SensorModels.h:104
Definition: SensorModels.h:44
EPtr(const char *key, long value, EPtrType ptrType=Long)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:200
Definition: SensorModels.h:56
int intValue
Definition: SensorModels.h:110
char charValue
Definition: SensorModels.h:113
const bool AsText
Definition: SensorModels.h:99
EPtr(const char *key, uint32_t value, EPtrType ptrType=Uint)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:192
static int parse(const char *text, EPtr *eptrs, int length, const char separator= '|', int eptrStartIndex=0)
Definition: SensorModels.h:219
EPtr()
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:125
Definition: SensorModels.h:53
Definition: SensorModels.h:101
Definition: SensorModels.h:33
uint8_t green
Definition: SensorModels.h:61
Definition: SensorModels.h:47
Definition: SensorModels.h:40
uint32_t color
Definition: SensorModels.h:58
uint8_t alpha
Definition: SensorModels.h:61
EPtr(const char *key, int value, EPtrType ptrType=Int)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:184
EPtr(const char *key, String value)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:166
Definition: SensorModels.h:41
EPtr(EPtrType ptrType)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:131
long longValue
Definition: SensorModels.h:111
Definition: SensorModels.h:49
EPtrType ptrType
Definition: SensorModels.h:103
bool boolValue
Definition: SensorModels.h:112
Definition: SensorModels.h:48
EPtr(EPtrType ptrType, const char *key, const char *value)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:152
EPtr(EPtrType ptrType, const char *key, EPtr *eptrs, int len)
Initializes a new instance of the EPtr struct.
Definition: SensorModels.h:137
Definition: SensorModels.h:32
uint8_t red
Definition: SensorModels.h:61
Definition: SensorModels.h:51
Definition: SensorModels.h:45
Definition: SensorModels.h:35
EPtr(const char *key, const char *value, int length)
Definition: SensorModels.h:217
ARGB(String hex)
Definition: SensorModels.h:76
EPtrType
Definition: SensorModels.h:38
uint32_t uintValue
Definition: SensorModels.h:109
void hex(char *hexSource)
Definition: SensorModels.h:80
bool asText
Definition: SensorModels.h:118