AskSin++
ChannelList.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __CHANNELLIST_H__
7 #define __CHANNELLIST_H__
8 
9 #include "Storage.h"
10 #include "HMID.h"
11 
12 namespace as {
13 
14 class BaseList {
15  uint16_t addr;
16 
17 public:
18  BaseList (uint16_t a) : addr(a) {}
19 
20  uint16_t address () const { return addr; }
21 
22  bool valid () const { return addr != 0; }
23 
24  uint8_t getByte (uint8_t offset) const {
25  return storage().getByte(addr + offset);
26  }
27 
28  uint8_t getByte (uint8_t offset, uint8_t mask, uint8_t shift) const {
29  return (getByte(offset) & mask) >> shift;
30  }
31 
32  bool setByte (uint8_t offset, uint8_t data) const {
33  return storage().setByte(addr + offset, data);
34  }
35 
36  bool setByte (uint8_t offset, uint8_t data, uint8_t mask, uint8_t shift) const {
37  uint8_t tmp = getByte(offset) & ~mask;
38  tmp |= (data << shift) & mask;
39  return setByte(offset, tmp);
40  }
41 
42  bool isBitSet (uint8_t offset, uint8_t bit) const {
43  return (storage().getByte(addr + offset) & bit) == bit;
44  }
45 
46  bool setBit (uint8_t offset, uint8_t bit, bool value) const {
47  if( value == true ) {
48  return storage().setBits(addr + offset, bit);
49  }
50  return storage().clearBits(addr + offset, bit);
51  }
52 
53  bool setData (uint8_t offset,uint8_t* buf,uint16_t size) const {
54  return storage().setData(addr + offset,buf,size);
55  }
56 
57  bool getData (uint8_t offset,uint8_t* buf,uint16_t size) const {
58  return storage().getData(addr + offset,buf,size);
59  }
60 
61  void clear (uint8_t offset,uint16_t size) {
62  storage().clearData(addr + offset,size);
63  }
64 
65  void init (const uint8_t* data,uint16_t size) {
66  for(uint16_t idx=0; idx<size; ++idx) {
67  storage().setByte(addr + idx,pgm_read_byte(data + idx));
68  }
69  }
70 };
71 
72 class GenericList : public BaseList {
73  uint8_t size;
74  uint8_t (*getregister) (uint8_t off);
75  uint8_t (*getoffset) (uint8_t reg);
76 public:
77  GenericList () : BaseList(0), size(0), getregister(0), getoffset(0) {}
78  GenericList (uint16_t a,uint8_t s,uint8_t (*getreg) (uint8_t off), uint8_t (*getoff) (uint8_t reg)) : BaseList(a), size(s), getregister(getreg), getoffset(getoff) {}
79 
80  uint8_t getOffset (uint8_t reg) const {
81  return getoffset(reg);
82  }
83 
84  uint8_t getRegister (uint8_t offset) const {
85  return getregister(offset);
86  }
87 
88  bool writeRegister (uint8_t reg, uint8_t value) const {
89  bool result = false;
90  uint8_t offset = getOffset(reg);
91  if( offset != 0xff ) {
92  result = setByte(offset,value);
93  }
94  return result;
95  }
96 
97  uint8_t readRegister (uint8_t reg) const {
98  uint8_t value = 0;
99  uint8_t offset = getOffset(reg);
100  if( offset != 0xff ) {
101  value = getByte(offset);
102  }
103  return value;
104  }
105 
106  uint8_t getSize () const {
107  return size;
108  }
109 
110  void dump () const {
111  DHEX(address());
112  DPRINT(F(" - "));
113  storage().dump(address(),getSize());
114  }
115 
116 
117 };
118 
119 template<class DataType>
120 class ChannelList : public BaseList {
121 protected:
122  ~ChannelList () {}
123 public:
124  ChannelList (uint16_t a) : BaseList(a) {}
125 
126  static uint8_t getOffset (uint8_t reg) {
127  return DataType::getOffset(reg);
128  }
129 
130  static uint8_t getRegister (uint8_t offset) {
131  return DataType::getRegister(offset);
132  }
133 
134  static uint8_t size () {
135  return sizeof(DataType);
136  }
137 
138  bool writeRegister (uint8_t reg, uint8_t value) const {
139  bool result = false;
140  uint8_t offset = getOffset(reg);
141  if( offset != 0xff ) {
142  result = setByte(offset,value);
143  }
144  return result;
145  }
146 
147  uint8_t readRegister (uint8_t reg) const {
148  uint8_t value = 0;
149  uint8_t offset = getOffset(reg);
150  if( offset != 0xff ) {
151  value = getByte(offset);
152  }
153  return value;
154  }
155 
156  void dump () const {
157  DHEX(address());
158  DPRINT(F(" - "));
159  storage().dump(address(),size());
160  }
161 
162  operator GenericList () const {
163  return GenericList(address(),size(),getRegister,getOffset);
164  }
165 };
166 
167 
169 public:
170  static uint8_t getOffset(__attribute__((unused)) uint8_t reg) { return 0xff; }
171  static uint8_t getRegister(__attribute__((unused)) uint8_t reg) { return 0xff; }
172 };
173 
174 class EmptyList : public ChannelList<EmptyListData> {
175 public:
176  EmptyList(uint16_t a) : ChannelList(a) {}
177 
178  void defaults () {}
179  void single () {}
180  void even () {}
181  void odd () {}
182 };
183 
184 
185 class List0Data {
186 public:
187  uint8_t data :8; // 0x02, s:0, e:8
188  uint8_t master1 :8; // 0x0A, s:0, e:8
189  uint8_t master2 :8; // 0x0B, s:0, e:8
190  uint8_t master3 :8; // 0x0C, s:0, e:8
191 
192  static uint8_t getOffset(uint8_t reg) {
193  switch (reg) {
194  case 0x02: return 0;
195  case 0x0A: return 1;
196  case 0x0B: return 2;
197  case 0x0C: return 3;
198  default: break;
199  }
200  return 0xff;
201  }
202 
203  static uint8_t getRegister(uint8_t offset) {
204  switch (offset) {
205  case 0: return 0x02;
206  case 1: return 0x0A;
207  case 2: return 0x0B;
208  case 3: return 0x0C;
209  default: break;
210  }
211  return 0xff;
212  }
213 };
214 
215 class List0 : public ChannelList<List0Data> {
216 public:
217  List0(uint16_t a) : ChannelList(a) {}
218 
219  HMID masterid () { return HMID(getByte(1),getByte(2),getByte(3)); }
220  void masterid (const HMID& mid) {
221  setByte(1,mid.id0());
222  setByte(2,mid.id1());
223  setByte(3,mid.id2());
224  };
225  bool aesActive () const { return false; }
226  bool sabotageMsg () const { return false; }
227  bool localResetDisable () const { return false; }
228  uint8_t ledMode () const { return 0x01; } // default LED is on
229 
230 
231  void defaults () {
232  setByte(0,0x01);
233  setByte(1,0x00);
234  setByte(2,0x00);
235  setByte(3,0x00);
236  }
237 
238  uint8_t transmitDevTryMax () const { return 6; }
239 
240 };
241 
242 
243 class List1Data {
244 public:
245  uint8_t AesActive :1; // 0x08, s:0, e:1
246  uint8_t notused :7;
247 
248  static uint8_t getOffset(uint8_t reg) {
249  switch (reg) {
250  case 0x08: return 0;
251  default: break;
252  }
253  return 0xff;
254  }
255 
256  static uint8_t getRegister(uint8_t offset) {
257  switch (offset) {
258  case 0: return 0x08;
259  default: break;
260  }
261  return 0xff;
262  }
263 };
264 
265 class List1 : public ChannelList<List1Data> {
266 public:
267  List1(uint16_t a) : ChannelList(a) {}
268 
269  bool aesActive () const { return isBitSet(0,0x01); }
270  bool aesActive (bool s) const { return setBit(0,0x01,s); }
271 
272  void defaults () {
273  setByte(0,0x00);
274  }
275 };
276 
277 
278 class List4Data {
279 public:
280  uint8_t burst :1; // 0x01, s:0, e:1
281  uint8_t notused :7;
282 
283  static uint8_t getOffset(uint8_t reg) {
284  switch (reg) {
285  case 0x01: return 0;
286  default: break;
287  }
288  return 0xff;
289  }
290 
291  static uint8_t getRegister(uint8_t offset) {
292  switch (offset) {
293  case 0: return 0x01;
294  default: break;
295  }
296  return 0xff;
297  }
298 };
299 
300 class List4 : public ChannelList<List4Data> {
301 public:
302  List4(uint16_t a) : ChannelList(a) {}
303 
304  bool burst () const { return isBitSet(0,0x01); }
305  bool burst (bool s) const { return setBit(0,0x01,s); }
306 
307  void defaults () {
308  setByte(0,0x00);
309  }
310 };
311 
312 }
313 
314 #endif
as::List4Data
Definition: ChannelList.h:278
as::EmptyListData
Definition: ChannelList.h:168
as::List0
Definition: ChannelList.h:215
as::List0Data
Definition: ChannelList.h:185
as::BaseList
Definition: ChannelList.h:14
as::List4
Definition: ChannelList.h:300
as::EmptyList
Definition: ChannelList.h:174
as::List1
Definition: ChannelList.h:265
as::GenericList
Definition: ChannelList.h:72
as::List1Data
Definition: ChannelList.h:243
as::ChannelList
Definition: ChannelList.h:120
as::HMID
Definition: HMID.h:14