Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | #if !defined(__DATAITEM_H__) |
2 | #define __DATAITEM_H__ |
||
3 | |||
4 | #include <inttypes.h> |
||
5 | #include <canoneos.h> |
||
6 | #include <SimpleTimer.h> |
||
7 | #include <valuelist.h> |
||
8 | |||
9 | #define MUL10( a ) (((a) << 3 ) + ((a) << 1 )) |
||
10 | |||
11 | char* itoa2(int val, uint8_t buf_size, char *buf, int8_t base, char c); |
||
12 | char* itoa2(uint8_t val, uint8_t buf_size, char *buf, int8_t base, char c); |
||
13 | char* itoa2(uint16_t val, uint8_t buf_size, char *buf, int8_t base, char c); |
||
14 | |||
15 | class DataItemBase |
||
16 | { |
||
17 | protected: |
||
18 | bool isUpdated; |
||
19 | |||
20 | public: |
||
21 | virtual void GetText(char** str, bool &is_pgm) = 0; |
||
22 | |||
23 | bool IsUpdated() { return isUpdated; }; |
||
24 | void SetUpdated(bool upd) { isUpdated = upd; }; |
||
25 | }; |
||
26 | |||
27 | class TimeSpanDataItem : public DataItemBase |
||
28 | { |
||
29 | uint32_t dataValue; |
||
30 | |||
31 | static char textValue[9]; |
||
32 | |||
33 | public: |
||
34 | TimeSpanDataItem(uint32_t s) : dataValue(s) {}; |
||
35 | |||
36 | virtual void GetText(char** str, bool &is_pgm) |
||
37 | { |
||
38 | { |
||
39 | uint16_t h = dataValue / 3600; |
||
40 | itoa2((uint16_t) h, 3, (char*)&textValue, 10, '0'); |
||
41 | } |
||
42 | { |
||
43 | textValue[2] = ':'; |
||
44 | uint16_t s = dataValue % 3600; |
||
45 | itoa2((uint16_t) s / 60, 3, (char*)(textValue+3), 10, '0'); |
||
46 | textValue[5] = ':'; |
||
47 | itoa2((uint16_t) s % 60, 3, (char*)(textValue+6), 10, '0'); |
||
48 | } |
||
49 | *str = textValue; |
||
50 | is_pgm = false; |
||
51 | }; |
||
52 | virtual void Set(uint32_t &val) |
||
53 | { |
||
54 | dataValue = val; |
||
55 | isUpdated = true; |
||
56 | }; |
||
57 | }; |
||
58 | |||
59 | #define MUL60( a ) (((a) << 6 ) - ((a) << 2 )) |
||
60 | #define MUL3600( a ) (((a) << 12 ) - ((a) << 9 ) + ((a) << 4 )) |
||
61 | |||
62 | class TimerDataItem : public DataItemBase |
||
63 | { |
||
64 | SimpleTimer &dataValue; |
||
65 | |||
66 | static char textValue[9]; |
||
67 | |||
68 | public: |
||
69 | TimerDataItem(SimpleTimer &t) : dataValue(t) {}; |
||
70 | |||
71 | virtual void GetText(char** str, bool &is_pgm) |
||
72 | { |
||
73 | uint16_t time_left = dataValue.TimeLeft() / 1000; |
||
74 | |||
75 | uint16_t ss = time_left % 60; |
||
76 | time_left /= 60; |
||
77 | uint16_t mm = time_left % 60; |
||
78 | time_left /= 60; |
||
79 | uint16_t hh = time_left; |
||
80 | |||
81 | itoa2((uint16_t)hh, 3, (char*)&textValue, 10, '0'); |
||
82 | textValue[2] = ':'; |
||
83 | itoa2((uint16_t)mm, 3, (char*)(textValue+3), 10, '0'); |
||
84 | textValue[5] = ':'; |
||
85 | itoa2((uint16_t)ss, 3, (char*)(textValue+6), 10, '0'); |
||
86 | *str = textValue; |
||
87 | is_pgm = false; |
||
88 | }; |
||
89 | |||
90 | }; |
||
91 | |||
92 | template <class VALUE_TYPE, const uint8_t TABLE_SIZE, const uint8_t TEXT_SIZE> |
||
93 | class KeyValuePairDataItem : public DataItemBase |
||
94 | { |
||
95 | VALUE_TYPE dataValue; |
||
96 | const ValueTitle<VALUE_TYPE, TEXT_SIZE> *ptrTitles; |
||
97 | |||
98 | public: |
||
99 | KeyValuePairDataItem(VALUE_TYPE val, const ValueTitle<VALUE_TYPE, TEXT_SIZE> *p) : dataValue(val), ptrTitles(p) |
||
100 | {}; |
||
101 | |||
102 | virtual void GetText(char** str, bool &is_pgm) |
||
103 | { |
||
104 | *str = (char*)FindTitle<VALUE_TYPE, TEXT_SIZE>(TABLE_SIZE, ptrTitles, dataValue); |
||
105 | is_pgm = true; |
||
106 | }; |
||
107 | |||
108 | VALUE_TYPE Get() { return dataValue; }; |
||
109 | void Set(VALUE_TYPE val) { dataValue = val; isUpdated = true; }; |
||
110 | }; |
||
111 | |||
112 | template <class VALUE_TYPE, const uint8_t TEXT_LEN> |
||
113 | class IntDataItem : public DataItemBase |
||
114 | { |
||
115 | VALUE_TYPE dataValue; |
||
116 | static char valString[TEXT_LEN]; |
||
117 | |||
118 | public: |
||
119 | IntDataItem() : dataValue(0) { isUpdated = true; }; |
||
120 | IntDataItem(VALUE_TYPE data) : dataValue(data) {}; |
||
121 | |||
122 | virtual void Set(VALUE_TYPE data) |
||
123 | { |
||
124 | dataValue = data; |
||
125 | isUpdated = true; |
||
126 | }; |
||
127 | |||
128 | VALUE_TYPE Get() { return dataValue; }; |
||
129 | |||
130 | virtual void GetText(char** str, bool &is_pgm) |
||
131 | { |
||
132 | *str = itoa2(dataValue, TEXT_LEN, (char*)&valString, 10, '0'); |
||
133 | is_pgm = false; |
||
134 | }; |
||
135 | void operator ++(int val) |
||
136 | { |
||
137 | dataValue += val; |
||
138 | isUpdated = true; |
||
139 | }; |
||
140 | void operator --(int val) |
||
141 | { |
||
142 | dataValue -= val; |
||
143 | isUpdated = true; |
||
144 | }; |
||
145 | }; |
||
146 | |||
147 | template <class VALUE_TYPE, const uint8_t TEXT_LEN> |
||
148 | char IntDataItem<VALUE_TYPE,TEXT_LEN>::valString[TEXT_LEN] = ""; |
||
149 | |||
150 | class PgmStringDataItem : public DataItemBase |
||
151 | { |
||
152 | const char *pStr; |
||
153 | |||
154 | public: |
||
155 | PgmStringDataItem(const char *str) : pStr(str) { isUpdated = true; }; |
||
156 | |||
157 | void SetText(const char *str) { pStr = str; isUpdated = true; }; |
||
158 | |||
159 | virtual void GetText(char** str, bool &is_pgm) |
||
160 | { |
||
161 | *str = (char*)pStr; |
||
162 | is_pgm = true; |
||
163 | }; |
||
164 | }; |
||
165 | |||
166 | template <const uint8_t STRLEN> |
||
167 | class StringDataItem : public DataItemBase |
||
168 | { |
||
169 | char theString[STRLEN]; |
||
170 | |||
171 | void CopyString(char *src, char *dst) |
||
172 | { |
||
173 | char *s = src, *d = dst; |
||
174 | |||
175 | for (uint8_t cnt = 0; *s && cnt < STRLEN-1; cnt++, s++, d++) |
||
176 | *d = *s; |
||
177 | |||
178 | *d = 0; |
||
179 | }; |
||
180 | public: |
||
181 | StringDataItem() { theString[0] = 0; isUpdated = true; }; |
||
182 | StringDataItem(char *str) { CopyString(str, &theString); isUpdated = true; }; |
||
183 | |||
184 | void SetText(char *str) { CopyString(str, &theString); isUpdated = true; }; |
||
185 | |||
186 | virtual void GetText(char** str, bool &is_pgm) |
||
187 | { |
||
188 | *str = &theString; |
||
189 | is_pgm = false; |
||
190 | }; |
||
191 | }; |
||
192 | |||
193 | |||
194 | #endif // __DATAITEM_H__ |