libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
neotrellis.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_NEO_TRELLIS_H
3#define DSY_NEO_TRELLIS_H
4
5#include "dev/neopixel.h"
6
7#define NEO_TRELLIS_ADDR 0x2E
8
9#define NEO_TRELLIS_NEOPIX_PIN 3
10
11#define NEO_TRELLIS_NUM_ROWS 4
12#define NEO_TRELLIS_NUM_COLS 4
13#define NEO_TRELLIS_NUM_KEYS (NEO_TRELLIS_NUM_ROWS * NEO_TRELLIS_NUM_COLS)
14
15#define NEO_TRELLIS_MAX_CALLBACKS 32
16
17#define NEO_TRELLIS_KEY(x) (((x) / 4) * 8 + ((x) % 4))
18#define NEO_TRELLIS_SEESAW_KEY(x) (((x) / 8) * 4 + ((x) % 8))
19
20#define NEO_TRELLIS_X(k) ((k) % 4)
21#define NEO_TRELLIS_Y(k) ((k) / 4)
22
23#define NEO_TRELLIS_XY(x, y) ((y)*NEO_TRELLIS_NUM_ROWS + (x))
24
25namespace daisy
26{
33{
34 public:
37
58
59 inline void Init(Config config)
60 {
61 config_ = config;
62
63 I2CHandle::Config i2c_config;
65 i2c_config.periph = config.periph;
66 i2c_config.speed = config.speed;
67
68 i2c_config.pin_config.scl = config.scl;
69 i2c_config.pin_config.sda = config.sda;
70
71 error_ |= I2CHandle::Result::OK != i2c_.Init(i2c_config);
72 }
73
75 {
76 error_ |= I2CHandle::Result::OK
77 != i2c_.TransmitBlocking(config_.address, data, size, 10);
78 }
79
81 {
82 error_ |= I2CHandle::Result::OK
83 != i2c_.ReceiveBlocking(config_.address, data, size, 10);
84 }
85
90 int delay)
91 {
92 uint8_t reg[2] = {reg_high, reg_low};
93 Write(reg, 2);
94
96
97 Read(buff, size);
98 }
99
105 {
106 uint8_t buffer[3];
107
108 buffer[0] = reg_high;
109 buffer[1] = reg_low;
110 buffer[2] = value;
111
112 Write(buffer, 3);
113 }
114
125
126 bool GetError()
127 {
128 bool tmp = error_;
129 error_ = false;
130 return tmp;
131 }
132
133 private:
134 I2CHandle i2c_;
135 Config config_;
136
137 // true if error has occured since last check
138 bool error_;
139};
140
145template <typename Transport>
147{
148 public:
151
173
174
185
195
197 {
198 struct
199 {
202 } bit;
204 };
205
208 {
209 struct Bit
210 {
213 } bit;
215 };
216
219 {
220 struct
221 {
224 } bit;
226 };
227
236
237 struct Config
238 {
239 typename Transport::Config transport_config;
240 NeoPixelI2C::Config pixels_conf;
241
243 };
244
246 {
247 OK = 0,
248 ERR
249 };
250
251 typedef void (*TrellisCallback)(keyEvent evt); //< Trellis Callback typedef
252
257 {
258 config_ = config;
259
260 // init neopixels
262 {
263 return ERR;
264 }
265
266 transport_.Init(config_.transport_config);
267
268 // 10 ms delay
269 System::Delay(10);
270
272
273 return GetTransportError();
274 }
275
281 {
282 return transport_.Write8(reg_high, reg_low, value);
283 }
284
290 {
291 return transport_.Read8(reg_high, reg_low, delay);
292 }
293
296 uint8_t *buff,
297 uint8_t len,
298 int delay)
299 {
300 transport_.ReadLen(reg_high, reg_low, buff, len, delay);
301 }
302
306 Result GetTransportError() { return transport_.GetError() ? ERR : OK; }
307
312 void SWReset()
313 {
315 }
316
322 void ActivateKey(uint8_t x, uint8_t y, uint8_t edge, bool enable)
323 {
324 int xkey = NEO_TRELLIS_X(x);
325 int ykey
327
330 }
331
336 void Process(bool polling = true)
337 {
339 System::DelayUs(500);
340 if(count > 0)
341 {
342 if(polling)
343 count = count + 2;
346 for(int i = 0; i < count; i++)
347 {
348 e[i].bit.NUM = NEO_TRELLIS_SEESAW_KEY(e[i].bit.NUM);
349 if(e[i].bit.NUM < NEO_TRELLIS_NUM_KEYS)
350 {
351 keyEvent evt = {e[i].bit.EDGE, e[i].bit.NUM};
352
353 state_[evt.bit.NUM]
354 = evt.bit.EDGE == HIGH || evt.bit.EDGE == RISING;
355 rising_[evt.bit.NUM] = evt.bit.EDGE == RISING;
356 falling_[evt.bit.NUM] = evt.bit.EDGE == FALLING;
357
358 // call any callbacks associated with the key
359 if(_callbacks[e[i].bit.NUM] != NULL)
360 {
361 _callbacks[e[i].bit.NUM](evt);
362 }
363 }
364 }
365 }
366 }
367
373 {
375 {
376 return state_[idx];
377 }
378
379 return false;
380 }
381
387 {
389 {
390 bool tmp = rising_[idx];
391 rising_[idx] = false;
392 return tmp;
393 }
394
395 return false;
396 }
397
403 {
405 {
406 bool tmp = falling_[idx];
407 falling_[idx] = false;
408 return tmp;
409 }
410
411 return false;
412 }
413
415 {
418 (uint8_t *)buf,
419 count,
420 1000);
421 }
422
430
437 {
438 keyState ks;
439 ks.bit.STATE = enable;
440 ks.bit.ACTIVE = (1 << edge);
442 transport_.Write(cmd, 4);
443 }
444
450
457 {
458 int xkey = NEO_TRELLIS_X(x);
459 int ykey
461
462 _callbacks[NEO_TRELLIS_XY(xkey, ykey)] = cb;
463 }
464
470 {
471 int xkey = NEO_TRELLIS_X(x);
472 int ykey
474
475 _callbacks[NEO_TRELLIS_XY(xkey, ykey)] = NULL;
476 }
477
479
480 private:
481 Config config_;
482 Transport transport_;
483
484 bool state_[NEO_TRELLIS_NUM_KEYS];
485 bool rising_[NEO_TRELLIS_NUM_KEYS];
486 bool falling_[NEO_TRELLIS_NUM_KEYS];
487
489 keyEvent);
490
491}; // namespace daisy
492
496} // namespace daisy
497#endif
Definition i2c.h:25
Result Init(const Config &config)
Result ReceiveBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Result TransmitBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Definition leddriver.h:33
void Init(I2CHandle i2c, const uint8_t(&addresses)[numDrivers], DmaBuffer dma_buffer_a, DmaBuffer dma_buffer_b, dsy_gpio_pin oe_pin={DSY_GPIOX, 0})
Definition leddriver.h:65
Result Init(Config config)
Definition neopixel.h:266
@ ERR
Definition neopixel.h:215
Device support for the Adafruit Neotrellis device.
Definition neotrellis.h:147
KeypadEdge
Definition neotrellis.h:230
@ HIGH
Definition neotrellis.h:231
@ FALLING
Definition neotrellis.h:233
@ LOW
Definition neotrellis.h:232
@ RISING
Definition neotrellis.h:234
bool GetRising(uint8_t idx)
Definition neotrellis.h:386
void(* TrellisCallback)(keyEvent evt)
Definition neotrellis.h:251
void UnregisterCallback(uint8_t x, uint8_t y)
Definition neotrellis.h:469
NeoPixelI2C pixels
Definition neotrellis.h:478
~NeoTrellis()
Definition neotrellis.h:150
Result Init(Config config)
Definition neotrellis.h:256
ModuleBaseAddress
Definition neotrellis.h:156
@ SEESAW_INTERRUPT_BASE
Definition neotrellis.h:164
@ SEESAW_TOUCH_BASE
Definition neotrellis.h:168
@ SEESAW_GPIO_BASE
Definition neotrellis.h:158
@ SEESAW_SPECTRUM_BASE
Definition neotrellis.h:171
@ SEESAW_ADC_BASE
Definition neotrellis.h:162
@ SEESAW_DAP_BASE
Definition neotrellis.h:165
@ SEESAW_ENCODER_BASE
Definition neotrellis.h:170
@ SEESAW_TIMER_BASE
Definition neotrellis.h:161
@ SEESAW_STATUS_BASE
Definition neotrellis.h:157
@ SEESAW_DAC_BASE
Definition neotrellis.h:163
@ SEESAW_SERCOM0_BASE
Definition neotrellis.h:159
@ SEESAW_NEOPIXEL_BASE
Definition neotrellis.h:167
@ SEESAW_KEYPAD_BASE
Definition neotrellis.h:169
@ SEESAW_EEPROM_BASE
Definition neotrellis.h:166
void Write8(uint8_t reg_high, uint8_t reg_low, uint8_t value)
Definition neotrellis.h:280
void ReadLen(uint8_t reg_high, uint8_t reg_low, uint8_t *buff, uint8_t len, int delay)
Definition neotrellis.h:294
void ActivateKey(uint8_t x, uint8_t y, uint8_t edge, bool enable)
Definition neotrellis.h:322
uint8_t Read8(uint8_t reg_high, uint8_t reg_low, int delay)
Definition neotrellis.h:289
void SetKeypadEvent(uint8_t key, uint8_t edge, bool enable)
Definition neotrellis.h:436
void EnableKeypadInterrupt()
Definition neotrellis.h:446
Result
Definition neotrellis.h:246
@ ERR
Definition neotrellis.h:248
@ OK
Definition neotrellis.h:247
NeoTrellis()
Definition neotrellis.h:149
void RegisterCallback(uint8_t x, uint8_t y, TrellisCallback(*cb)(keyEvent))
Definition neotrellis.h:456
void Process(bool polling=true)
Definition neotrellis.h:336
KeypadFuncAddRegs
Definition neotrellis.h:177
@ SEESAW_KEYPAD_EVENT
Definition neotrellis.h:179
@ SEESAW_KEYPAD_FIFO
Definition neotrellis.h:183
@ SEESAW_KEYPAD_INTENSET
Definition neotrellis.h:180
@ SEESAW_KEYPAD_INTENCLR
Definition neotrellis.h:181
@ SEESAW_KEYPAD_STATUS
Definition neotrellis.h:178
@ SEESAW_KEYPAD_COUNT
Definition neotrellis.h:182
bool GetFalling(uint8_t idx)
Definition neotrellis.h:402
void ReadKeypad(keyEventRaw *buf, uint8_t count)
Definition neotrellis.h:414
void SWReset()
Definition neotrellis.h:312
uint8_t GetKeypadCount()
Definition neotrellis.h:426
Result GetTransportError()
Definition neotrellis.h:306
StatusFuncAddRegs
Definition neotrellis.h:188
@ SEESAW_STATUS_SWRST
Definition neotrellis.h:193
@ SEESAW_STATUS_TEMP
Definition neotrellis.h:192
@ SEESAW_STATUS_VERSION
Definition neotrellis.h:190
@ SEESAW_STATUS_HW_ID
Definition neotrellis.h:189
@ SEESAW_STATUS_OPTIONS
Definition neotrellis.h:191
bool GetState(uint8_t idx)
Definition neotrellis.h:372
Definition neotrellis.h:33
NeoTrellisI2CTransport()
Definition neotrellis.h:35
bool GetError()
Definition neotrellis.h:126
void Write8(uint8_t reg_high, uint8_t reg_low, uint8_t value)
Definition neotrellis.h:104
void Write(uint8_t *data, uint16_t size)
Definition neotrellis.h:74
~NeoTrellisI2CTransport()
Definition neotrellis.h:36
void Init(Config config)
Definition neotrellis.h:59
void Read(uint8_t *data, uint16_t size)
Definition neotrellis.h:80
uint8_t Read8(uint8_t reg_high, uint8_t reg_low, int delay)
Definition neotrellis.h:119
void ReadLen(uint8_t reg_high, uint8_t reg_low, uint8_t *buff, uint16_t size, int delay)
Definition neotrellis.h:86
static void DelayUs(uint32_t delay_us)
static void Delay(uint32_t delay_ms)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTB
Definition daisy_core.h:246
#define NEO_TRELLIS_SEESAW_KEY(x)
Definition neotrellis.h:18
#define NEO_TRELLIS_NUM_KEYS
Definition neotrellis.h:13
#define NEO_TRELLIS_X(k)
Definition neotrellis.h:20
#define NEO_TRELLIS_ADDR
Definition neotrellis.h:7
#define NEO_TRELLIS_KEY(x)
Definition neotrellis.h:17
#define NEO_TRELLIS_Y(k)
Definition neotrellis.h:21
#define NEO_TRELLIS_XY(x, y)
Definition neotrellis.h:23
#define NEO_TRELLIS_NUM_ROWS
Definition neotrellis.h:11
#define NEO_TRELLIS_NUM_COLS
Definition neotrellis.h:12
Definition i2c.h:29
Mode mode
Definition i2c.h:64
Speed
Definition i2c.h:50
Speed speed
Definition i2c.h:63
struct daisy::I2CHandle::Config::@13 pin_config
Peripheral periph
Definition i2c.h:56
Peripheral
Definition i2c.h:39
Definition neotrellis.h:238
NeoPixelI2C::Config pixels_conf
Definition neotrellis.h:240
Config()
Definition neotrellis.h:242
Transport::Config transport_config
Definition neotrellis.h:239
Definition neotrellis.h:210
uint16_t NUM
the event number
Definition neotrellis.h:212
uint8_t EDGE
the edge that was triggered
Definition neotrellis.h:211
Definition neotrellis.h:39
I2CHandle::Config::Peripheral periph
Definition neotrellis.h:40
I2CHandle::Config::Speed speed
Definition neotrellis.h:41
Config()
Definition neotrellis.h:47
Pin sda
Definition neotrellis.h:43
uint8_t address
Definition neotrellis.h:45
Pin scl
Definition neotrellis.h:42
representation of hardware port/pin combination
Definition daisy_core.h:261
Definition neotrellis.h:208
uint16_t reg
register format
Definition neotrellis.h:214
struct daisy::NeoTrellis::keyEvent::Bit bit
bitfield format
Definition neotrellis.h:197
uint8_t reg
register format
Definition neotrellis.h:203
uint8_t EDGE
the edge that was triggered
Definition neotrellis.h:200
uint8_t NUM
the event number
Definition neotrellis.h:201
struct daisy::NeoTrellis::keyEventRaw::@7 bit
bitfield format
Definition neotrellis.h:219
uint8_t STATE
the current state of the key
Definition neotrellis.h:222
struct daisy::NeoTrellis::keyState::@8 bit
bitfield format
uint8_t ACTIVE
the registered events for that key
Definition neotrellis.h:223
uint8_t reg
register format
Definition neotrellis.h:225