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 
193 #define SEESAW_HW_ID_CODE_SAMD09 0x55
194 
195 #define SEESAW_HW_ID_CODE_TINY8X7 0x87
196 
197 #define SEESAW_EEPROM_I2C_ADDR \
198  0x3F
199 
201 
202 union keyEventRaw {
203  struct {
204  uint8_t EDGE : 2;
205  uint8_t NUM : 6;
206  } bit;
207  uint8_t reg;
208 };
209 
211 union keyEvent {
212  struct {
213  uint8_t EDGE : 2;
214  uint16_t NUM : 14;
215  } bit;
216  uint16_t reg;
217 };
218 
220 union keyState {
221  struct {
222  uint8_t STATE : 1;
223  uint8_t ACTIVE : 4;
224  } bit;
225  uint8_t reg;
226 };
227 
228 /**************************************************************************/
233 /**************************************************************************/
234 class Adafruit_seesaw : public Print {
235 public:
236  // constructors
237  Adafruit_seesaw(TwoWire *Wi = NULL);
238  ~Adafruit_seesaw(void){};
239 
240  bool begin(uint8_t addr = SEESAW_ADDRESS, int8_t flow = -1,
241  bool reset = true);
242  uint32_t getOptions();
243  uint32_t getVersion();
244  bool getProdDatecode(uint16_t *pid, uint8_t *year, uint8_t *mon,
245  uint8_t *day);
246 
247  bool SWReset();
248 
249  void pinMode(uint8_t pin, uint8_t mode);
250  void pinModeBulk(uint32_t pins, uint8_t mode);
251  void pinModeBulk(uint32_t pinsa, uint32_t pinsb, uint8_t mode);
252  virtual void analogWrite(uint8_t pin, uint16_t value, uint8_t width = 8);
253  void digitalWrite(uint8_t pin, uint8_t value);
254  void digitalWriteBulk(uint32_t pins, uint8_t value);
255  void digitalWriteBulk(uint32_t pinsa, uint32_t pinsb, uint8_t value);
256 
257  bool digitalRead(uint8_t pin);
258  uint32_t digitalReadBulk(uint32_t pins);
259  uint32_t digitalReadBulkB(uint32_t pins);
260 
261  void setGPIOInterrupts(uint32_t pins, bool enabled);
262 
263  virtual uint16_t analogRead(uint8_t pin);
264 
265  uint16_t touchRead(uint8_t pin);
266 
267  virtual void setPWMFreq(uint8_t pin, uint16_t freq);
268 
269  void enableSercomDataRdyInterrupt(uint8_t sercom = 0);
270  void disableSercomDataRdyInterrupt(uint8_t sercom = 0);
271 
272  char readSercomData(uint8_t sercom = 0);
273 
274  void EEPROMWrite8(uint8_t addr, uint8_t val);
275  void EEPROMWrite(uint8_t addr, uint8_t *buf, uint8_t size);
276  uint8_t EEPROMRead8(uint8_t addr);
277 
278  void setI2CAddr(uint8_t addr);
279  uint8_t getI2CAddr();
280 
281  void UARTSetBaud(uint32_t baud);
282 
283  void setKeypadEvent(uint8_t key, uint8_t edge, bool enable = true);
284  void enableKeypadInterrupt();
285  void disableKeypadInterrupt();
286  uint8_t getKeypadCount();
287  bool readKeypad(keyEventRaw *buf, uint8_t count);
288 
289  float getTemp();
290 
291  int32_t getEncoderPosition(uint8_t encoder = 0);
292  int32_t getEncoderDelta(uint8_t encoder = 0);
293  bool enableEncoderInterrupt(uint8_t encoder = 0);
294  bool disableEncoderInterrupt(uint8_t encoder = 0);
295  void setEncoderPosition(int32_t pos, uint8_t encoder = 0);
296 
297  virtual size_t write(uint8_t);
298  virtual size_t write(const char *str);
299 
300 protected:
301  TwoWire *_i2cbus;
302  Adafruit_I2CDevice *_i2c_dev = NULL;
303 
304  int8_t _flow;
306  uint8_t _hardwaretype = 0;
308  bool write8(byte regHigh, byte regLow, byte value);
309  uint8_t read8(byte regHigh, byte regLow, uint16_t delay = 250);
310 
311  bool read(uint8_t regHigh, uint8_t regLow, uint8_t *buf, uint8_t num,
312  uint16_t delay = 250);
313  bool write(uint8_t regHigh, uint8_t regLow, uint8_t *buf, uint8_t num);
314 
315  /*=========================================================================
316  REGISTER BITFIELDS
317  -----------------------------------------------------------------------*/
318 
321  union sercom_inten {
322  struct {
323  uint8_t DATA_RDY : 1;
324  } bit;
325  uint8_t reg;
326  };
328 
329  /*=========================================================================*/
330 };
331 
332 #endif
Definition: Adafruit_seesaw.h:220
uint8_t DATA_RDY
this bit is set when data becomes available
Definition: Adafruit_seesaw.h:323
int8_t _flow
Definition: Adafruit_seesaw.h:304
Definition: Adafruit_seesaw.h:321
struct keyEventRaw::@12 bit
bitfield format
uint8_t NUM
the event number
Definition: Adafruit_seesaw.h:205
uint8_t reg
register format
Definition: Adafruit_seesaw.h:207
uint8_t ACTIVE
the registered events for that key
Definition: Adafruit_seesaw.h:223
uint8_t STATE
the current state of the key
Definition: Adafruit_seesaw.h:222
uint16_t NUM
the event number
Definition: Adafruit_seesaw.h:214
Class that stores state and functions for interacting with seesaw helper IC.
Definition: Adafruit_seesaw.h:234
uint16_t reg
register format
Definition: Adafruit_seesaw.h:216
uint8_t EDGE
the edge that was triggered
Definition: Adafruit_seesaw.h:204
uint8_t reg
full register
Definition: Adafruit_seesaw.h:325
#define SEESAW_ADDRESS
Default Seesaw I2C address.
Definition: Adafruit_seesaw.h:31
TwoWire * _i2cbus
Definition: Adafruit_seesaw.h:301
Definition: Adafruit_seesaw.h:211
uint8_t EDGE
the edge that was triggered
Definition: Adafruit_seesaw.h:213
Definition: Adafruit_seesaw.h:202
uint8_t reg
register format
Definition: Adafruit_seesaw.h:225
sercom_inten _sercom_inten
sercom interrupt enable register instance
Definition: Adafruit_seesaw.h:327