Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | #if !defined(__MENU_H__) |
2 | #define __MENU_H__ |
||
3 | |||
4 | #include <inttypes.h> |
||
5 | #include "screenitem.h" |
||
6 | #include "screen.h" |
||
7 | #include "controls.h" |
||
8 | |||
9 | typedef void (*MenuFunctionPtr)(); |
||
10 | |||
11 | struct MenuItem |
||
12 | { |
||
13 | ScreenItem *screenItem; |
||
14 | MenuFunctionPtr ptrFunction; |
||
15 | }; |
||
16 | |||
17 | class Menu : public ControlEvents |
||
18 | { |
||
19 | uint8_t numItems; |
||
20 | uint8_t itemSelected; |
||
21 | uint8_t screenId; |
||
22 | MenuItem *menuItems; |
||
23 | StateMachine *returnState; |
||
24 | |||
25 | public: |
||
26 | Menu(uint8_t scr, uint8_t num_items, MenuItem *items, uint8_t sel = 0, StateMachine *s = NULL) : |
||
27 | screenId(scr), |
||
28 | numItems(num_items), |
||
29 | menuItems(items), |
||
30 | itemSelected(sel), |
||
31 | returnState(s) |
||
32 | {}; |
||
33 | |||
34 | void SetReturnState(StateMachine *s) { returnState = s; }; |
||
35 | |||
36 | virtual bool OnInitialState(); |
||
37 | virtual bool OnEncoderChanged(int8_t value); |
||
38 | virtual bool OnEncButtonDown(); |
||
39 | virtual bool OnExtButtonDown(); |
||
40 | }; |
||
41 | |||
42 | typedef void (*SpinFunction)(DataItemBase *data_item); |
||
43 | |||
44 | template <class ITEM_TYPE, class VALUE_TYPE> |
||
45 | class IntSpin : public ControlEvents |
||
46 | { |
||
47 | VALUE_TYPE minValue; |
||
48 | VALUE_TYPE maxValue; |
||
49 | VALUE_TYPE incValue; |
||
50 | |||
51 | ITEM_TYPE *dataItem; |
||
52 | StateMachine *returnState; |
||
53 | SpinFunction pFunction; |
||
54 | |||
55 | public: |
||
56 | IntSpin(VALUE_TYPE min_val, VALUE_TYPE max_val, VALUE_TYPE inc_val, ITEM_TYPE *item, SpinFunction f) : |
||
57 | minValue(min_val), |
||
58 | maxValue(max_val), |
||
59 | incValue(inc_val), |
||
60 | dataItem(item), |
||
61 | returnState(NULL), |
||
62 | pFunction(f) |
||
63 | { |
||
64 | }; |
||
65 | |||
66 | void SetConstraints(VALUE_TYPE min_val, VALUE_TYPE max_val, VALUE_TYPE inc_val) |
||
67 | { |
||
68 | minValue = min_val; |
||
69 | maxValue = max_val; |
||
70 | incValue = inc_val; |
||
71 | }; |
||
72 | |||
73 | void SetReturnState(StateMachine *s) { returnState = s; }; |
||
74 | |||
75 | virtual bool OnEncoderChanged(int8_t value) |
||
76 | { |
||
77 | int16_t new_val = dataItem->Get() + value * incValue; |
||
78 | |||
79 | if (new_val > maxValue) |
||
80 | new_val = maxValue; |
||
81 | else if (new_val < minValue) |
||
82 | new_val = minValue; |
||
83 | |||
84 | dataItem->Set((VALUE_TYPE)new_val); |
||
85 | dataItem->SetUpdated(true); |
||
86 | |||
87 | return true; |
||
88 | }; |
||
89 | |||
90 | virtual bool OnEncButtonDown() |
||
91 | { |
||
92 | if (pFunction) |
||
93 | pFunction((DataItemBase*)dataItem); |
||
94 | |||
95 | if (returnState) |
||
96 | StateMachine::SetState(returnState); |
||
97 | return true; |
||
98 | }; |
||
99 | }; |
||
100 | |||
101 | template <class ITEM_TYPE, class VALUE_TYPE> |
||
102 | class EEPROMListIntSpin : public ControlEvents |
||
103 | { |
||
104 | EEPROMByteList *valueList; |
||
105 | ITEM_TYPE *dataItem; |
||
106 | StateMachine *returnState; |
||
107 | SpinFunction pFunction; |
||
108 | |||
109 | public: |
||
110 | EEPROMListIntSpin(EEPROMByteList *list, ITEM_TYPE *item, SpinFunction pf) : |
||
111 | valueList(list), |
||
112 | dataItem(item), |
||
113 | returnState(NULL), |
||
114 | pFunction(pf) |
||
115 | { |
||
116 | }; |
||
117 | |||
118 | void SetReturnState(StateMachine *s) { returnState = s; }; |
||
119 | |||
120 | virtual bool OnEncoderChanged(int8_t value) |
||
121 | { |
||
122 | if (valueList->GetSize() < 1) |
||
123 | return true; |
||
124 | |||
125 | VALUE_TYPE new_value; |
||
126 | |||
127 | if (value < 0) |
||
128 | new_value = valueList->GetPrev(dataItem->Get(), -value); |
||
129 | else |
||
130 | new_value = valueList->GetNext(dataItem->Get(), value); |
||
131 | |||
132 | dataItem->Set(new_value); |
||
133 | dataItem->SetUpdated(true); |
||
134 | |||
135 | return true; |
||
136 | }; |
||
137 | |||
138 | virtual bool OnEncButtonDown() |
||
139 | { |
||
140 | if (pFunction) |
||
141 | pFunction((DataItemBase*)dataItem); |
||
142 | |||
143 | if (returnState) |
||
144 | StateMachine::SetState(returnState); |
||
145 | |||
146 | return true; |
||
147 | }; |
||
148 | }; |
||
149 | |||
150 | |||
151 | template <class ITEM_TYPE, class VALUE_TYPE, class VALUE_LIST_TYPE> |
||
152 | class SRAMListIntSpin : public ControlEvents |
||
153 | { |
||
154 | VALUE_LIST_TYPE *valueList; |
||
155 | ITEM_TYPE *dataItem; |
||
156 | StateMachine *returnState; |
||
157 | SpinFunction pFunction; |
||
158 | |||
159 | public: |
||
160 | SRAMListIntSpin(VALUE_LIST_TYPE *list, ITEM_TYPE *item, SpinFunction pf) : |
||
161 | valueList(list), |
||
162 | dataItem(item), |
||
163 | returnState(NULL), |
||
164 | pFunction(pf) |
||
165 | { |
||
166 | }; |
||
167 | |||
168 | void SetReturnState(StateMachine *s) { returnState = s; }; |
||
169 | |||
170 | virtual bool OnEncoderChanged(int8_t value) |
||
171 | { |
||
172 | if (valueList->GetSize() < 1) |
||
173 | return true; |
||
174 | |||
175 | VALUE_TYPE new_value; |
||
176 | |||
177 | if (value < 0) |
||
178 | new_value = valueList->GetPrev(dataItem->Get(), -value); |
||
179 | else |
||
180 | new_value = valueList->GetNext(dataItem->Get(), value); |
||
181 | |||
182 | dataItem->Set(new_value); |
||
183 | dataItem->SetUpdated(true); |
||
184 | |||
185 | return true; |
||
186 | }; |
||
187 | |||
188 | virtual bool OnEncButtonDown() |
||
189 | { |
||
190 | if (pFunction) |
||
191 | pFunction((DataItemBase*)dataItem); |
||
192 | |||
193 | if (returnState) |
||
194 | StateMachine::SetState(returnState); |
||
195 | |||
196 | return true; |
||
197 | }; |
||
198 | }; |
||
199 | |||
200 | #endif // __MENU_H__ |