Adafruit Library
Adafruit_seesaw.h
Go to the documentation of this file.
1 
21 #ifndef LIB_SEESAW_H
22 #define LIB_SEESAW_H
23 
24 #include "Adafruit_I2CDevice.h"
25 #include <Arduino.h>
26 #include <Wire.h>
27 
28 /*=========================================================================
29  I2C ADDRESS/BITS
30  -----------------------------------------------------------------------*/
31 #define SEESAW_ADDRESS (0x49)
32 /*=========================================================================*/
33 
34 /*=========================================================================
35  REGISTERS
36  -----------------------------------------------------------------------*/
37 
41 enum {
42  SEESAW_STATUS_BASE = 0x00,
43  SEESAW_GPIO_BASE = 0x01,
44  SEESAW_SERCOM0_BASE = 0x02,
45 
46  SEESAW_TIMER_BASE = 0x08,
47  SEESAW_ADC_BASE = 0x09,
48  SEESAW_DAC_BASE = 0x0A,
49  SEESAW_INTERRUPT_BASE = 0x0B,
50  SEESAW_DAP_BASE = 0x0C,
51  SEESAW_EEPROM_BASE = 0x0D,
52  SEESAW_NEOPIXEL_BASE = 0x0E,
53  SEESAW_TOUCH_BASE = 0x0F,
54  SEESAW_KEYPAD_BASE = 0x10,
55  SEESAW_ENCODER_BASE = 0x11,
56  SEESAW_SPECTRUM_BASE = 0x12,
57 };
58 
61 enum {
62  SEESAW_GPIO_DIRSET_BULK = 0x02,
63  SEESAW_GPIO_DIRCLR_BULK = 0x03,
64  SEESAW_GPIO_BULK = 0x04,
65  SEESAW_GPIO_BULK_SET = 0x05,
66  SEESAW_GPIO_BULK_CLR = 0x06,
67  SEESAW_GPIO_BULK_TOGGLE = 0x07,
68  SEESAW_GPIO_INTENSET = 0x08,
69  SEESAW_GPIO_INTENCLR = 0x09,
70  SEESAW_GPIO_INTFLAG = 0x0A,
71  SEESAW_GPIO_PULLENSET = 0x0B,
72  SEESAW_GPIO_PULLENCLR = 0x0C,
73 };
74 
77 enum {
78  SEESAW_STATUS_HW_ID = 0x01,
79  SEESAW_STATUS_VERSION = 0x02,
80  SEESAW_STATUS_OPTIONS = 0x03,
81  SEESAW_STATUS_TEMP = 0x04,
82  SEESAW_STATUS_SWRST = 0x7F,
83 };
84 
87 enum {
88  SEESAW_TIMER_STATUS = 0x00,
89  SEESAW_TIMER_PWM = 0x01,
90  SEESAW_TIMER_FREQ = 0x02,
91 };
92 
95 enum {
96  SEESAW_ADC_STATUS = 0x00,
97  SEESAW_ADC_INTEN = 0x02,
98  SEESAW_ADC_INTENCLR = 0x03,
99  SEESAW_ADC_WINMODE = 0x04,
100  SEESAW_ADC_WINTHRESH = 0x05,
101  SEESAW_ADC_CHANNEL_OFFSET = 0x07,
102 };
103 
106 enum {
107  SEESAW_SERCOM_STATUS = 0x00,
108  SEESAW_SERCOM_INTEN = 0x02,
109  SEESAW_SERCOM_INTENCLR = 0x03,
110  SEESAW_SERCOM_BAUD = 0x04,
111  SEESAW_SERCOM_DATA = 0x05,
112 };
113 
116 enum {
117  SEESAW_NEOPIXEL_STATUS = 0x00,
118  SEESAW_NEOPIXEL_PIN = 0x01,
119  SEESAW_NEOPIXEL_SPEED = 0x02,
120  SEESAW_NEOPIXEL_BUF_LENGTH = 0x03,
121  SEESAW_NEOPIXEL_BUF = 0x04,
122  SEESAW_NEOPIXEL_SHOW = 0x05,
123 };
124 
127 enum {
128  SEESAW_TOUCH_CHANNEL_OFFSET = 0x10,
129 };
130 
133 enum {
134  SEESAW_KEYPAD_STATUS = 0x00,
135  SEESAW_KEYPAD_EVENT = 0x01,
136  SEESAW_KEYPAD_INTENSET = 0x02,
137  SEESAW_KEYPAD_INTENCLR = 0x03,
138  SEESAW_KEYPAD_COUNT = 0x04,
139  SEESAW_KEYPAD_FIFO = 0x10,
140 };
141 
144 enum {
145  SEESAW_KEYPAD_EDGE_HIGH = 0,
146  SEESAW_KEYPAD_EDGE_LOW,
147  SEESAW_KEYPAD_EDGE_FALLING,
148  SEESAW_KEYPAD_EDGE_RISING,
149 };
150 
153 enum {
154  SEESAW_ENCODER_STATUS = 0x00,
155  SEESAW_ENCODER_INTENSET = 0x10,
156  SEESAW_ENCODER_INTENCLR = 0x20,
157  SEESAW_ENCODER_POSITION = 0x30,
158  SEESAW_ENCODER_DELTA = 0x40,
159 };
160 
163 enum {
164  SEESAW_SPECTRUM_RESULTS_LOWER = 0x00, // Audio spectrum bins 0-31
165  SEESAW_SPECTRUM_RESULTS_UPPER = 0x01, // Audio spectrum bins 32-63
166  // If some future device supports a larger spectrum, can add additional
167  // "bins" working upward from here. Configurable setting registers then
168  // work downward from the top to avoid collision between spectrum bins
169  // and configurables.
170  SEESAW_SPECTRUM_CHANNEL = 0xFD,
171  SEESAW_SPECTRUM_RATE = 0xFE,
172  SEESAW_SPECTRUM_STATUS = 0xFF,
173 };
174 
175 #define ADC_INPUT_0_PIN 2
176 #define ADC_INPUT_1_PIN 3
177 #define ADC_INPUT_2_PIN 4
178 #define ADC_INPUT_3_PIN 5
179 
180 #define PWM_0_PIN 4
181 #define PWM_1_PIN 5
182 #define PWM_2_PIN 6
183 #define PWM_3_PIN 7
184 
185 #ifndef INPUT_PULLDOWN
186 #define INPUT_PULLDOWN \
187  0x03
188 #endif
190 
191 /*=========================================================================*/
192 // clang-format off
193 #define SEESAW_HW_ID_CODE_SAMD09 0x55
194 #define SEESAW_HW_ID_CODE_TINY806 0x84
195 #define SEESAW_HW_ID_CODE_TINY807 0x85
196 #define SEESAW_HW_ID_CODE_TINY816 0x86
197 #define SEESAW_HW_ID_CODE_TINY817 0x87
198 #define SEESAW_HW_ID_CODE_TINY1616 0x88
199 #define SEESAW_HW_ID_CODE_TINY1617 0x89
200 // clang-format on
201 
203 union keyEventRaw {
204  struct {
205  uint8_t EDGE : 2;
206  uint8_t NUM : 6;
207  } bit;
208  uint8_t reg;
209 };
210 
212 union keyEvent {
213  struct {
214  uint8_t EDGE : 2;
215  uint16_t NUM : 14;
216  } bit;
217  uint16_t reg;
218 };
219 
221 union keyState {
222  struct {
223  uint8_t STATE : 1;
224  uint8_t ACTIVE : 4;
225  } bit;
226  uint8_t reg;
227 };
228 
229 /**************************************************************************/
234 /**************************************************************************/
235 class Adafruit_seesaw : public Print {
236 public:
237  // constructors
238  Adafruit_seesaw(TwoWire *Wi = NULL);
239  ~Adafruit_seesaw(void){};
240 
241  bool begin(uint8_t addr = SEESAW_ADDRESS, int8_t flow = -1,
242  bool reset = true);
243  uint32_t getOptions();
244  uint32_t getVersion();
245  bool getProdDatecode(uint16_t *pid, uint8_t *year, uint8_t *mon,
246  uint8_t *day);
247 
248  bool SWReset();
249 
250  void pinMode(uint8_t pin, uint8_t mode);
251  void pinModeBulk(uint32_t pins, uint8_t mode);
252  void pinModeBulk(uint32_t pinsa, uint32_t pinsb, uint8_t mode);
253  virtual void analogWrite(uint8_t pin, uint16_t value, uint8_t width = 8);
254  void digitalWrite(uint8_t pin, uint8_t value);
255  void digitalWriteBulk(uint32_t port_values);
256  void digitalWriteBulk(uint32_t pins, uint8_t value);
257  void digitalWriteBulk(uint32_t pinsa, uint32_t pinsb, uint8_t value);
258 
259  bool digitalRead(uint8_t pin);
260  uint32_t digitalReadBulk(uint32_t pins);
261  uint32_t digitalReadBulkB(uint32_t pins);
262 
263  void setGPIOInterrupts(uint32_t pins, bool enabled);
264 
265  virtual uint16_t analogRead(uint8_t pin);
266 
267  uint16_t touchRead(uint8_t pin);
268 
269  virtual void setPWMFreq(uint8_t pin, uint16_t freq);
270 
271  void enableSercomDataRdyInterrupt(uint8_t sercom = 0);
272  void disableSercomDataRdyInterrupt(uint8_t sercom = 0);
273 
274  char readSercomData(uint8_t sercom = 0);
275 
276  void EEPROMWrite8(uint8_t addr, uint8_t val);
277  void EEPROMWrite(uint8_t addr, uint8_t *buf, uint8_t size);
278  uint8_t EEPROMRead8(uint8_t addr);
279 
280  void setI2CAddr(uint8_t addr);
281  uint8_t getI2CAddr();
282 
283  void UARTSetBaud(uint32_t baud);
284 
285  void setKeypadEvent(uint8_t key, uint8_t edge, bool enable = true);
286  void enableKeypadInterrupt();
287  void disableKeypadInterrupt();
288  uint8_t getKeypadCount();
289  bool readKeypad(keyEventRaw *buf, uint8_t count);
290 
291  float getTemp();
292 
293  int32_t getEncoderPosition(uint8_t encoder = 0);
294  int32_t getEncoderDelta(uint8_t encoder = 0);
295  bool enableEncoderInterrupt(uint8_t encoder = 0);
296  bool disableEncoderInterrupt(uint8_t encoder = 0);
297  void setEncoderPosition(int32_t pos, uint8_t encoder = 0);
298 
299  virtual size_t write(uint8_t);
300  virtual size_t write(const char *str);
301 
302 protected:
303  TwoWire *_i2cbus;
304  Adafruit_I2CDevice *_i2c_dev = NULL;
305 
306  int8_t _flow;
308  uint8_t _hardwaretype = 0;
309  uint8_t getI2CaddrEEPROMloc();
310 
311  bool write8(byte regHigh, byte regLow, byte value);
312  uint8_t read8(byte regHigh, byte regLow, uint16_t delay = 250);
313 
314  bool read(uint8_t regHigh, uint8_t regLow, uint8_t *buf, uint8_t num,
315  uint16_t delay = 250);
316  bool write(uint8_t regHigh, uint8_t regLow, uint8_t *buf, uint8_t num);
317 
318  /*=========================================================================
319  REGISTER BITFIELDS
320  -----------------------------------------------------------------------*/
321 
324  union sercom_inten {
325  struct {
326  uint8_t DATA_RDY : 1;
327  } bit;
328  uint8_t reg;
329  };
331 
332  /*=========================================================================*/
333 };
334 
335 #endif
Definition: Adafruit_seesaw.h:221
uint8_t DATA_RDY
this bit is set when data becomes available
Definition: Adafruit_seesaw.h:326
int8_t _flow
Definition: Adafruit_seesaw.h:306
Definition: Adafruit_seesaw.h:324
struct keyEventRaw::@12 bit
bitfield format
uint8_t NUM
the event number
Definition: Adafruit_seesaw.h:206
uint8_t reg
register format
Definition: Adafruit_seesaw.h:208
uint8_t ACTIVE
the registered events for that key
Definition: Adafruit_seesaw.h:224
uint8_t STATE
the current state of the key
Definition: Adafruit_seesaw.h:223
uint16_t NUM
the event number
Definition: Adafruit_seesaw.h:215
Class that stores state and functions for interacting with seesaw helper IC.
Definition: Adafruit_seesaw.h:235
uint16_t reg
register format
Definition: Adafruit_seesaw.h:217
uint8_t EDGE
the edge that was triggered
Definition: Adafruit_seesaw.h:205
uint8_t reg
full register
Definition: Adafruit_seesaw.h:328
#define SEESAW_ADDRESS
Default Seesaw I2C address.
Definition: Adafruit_seesaw.h:31
TwoWire * _i2cbus
Definition: Adafruit_seesaw.h:303
Definition: Adafruit_seesaw.h:212
uint8_t EDGE
the edge that was triggered
Definition: Adafruit_seesaw.h:214
Definition: Adafruit_seesaw.h:203
uint8_t reg
register format
Definition: Adafruit_seesaw.h:226
sercom_inten _sercom_inten
sercom interrupt enable register instance
Definition: Adafruit_seesaw.h:330