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 #if (ARDUINO >= 100)
25 #include "Arduino.h"
26 #else
27 #include "WProgram.h"
28 #endif
29 
30 #include <Wire.h>
31 
32 /*=========================================================================
33  I2C ADDRESS/BITS
34  -----------------------------------------------------------------------*/
35 #define SEESAW_ADDRESS (0x49)
36 /*=========================================================================*/
37 
38 /*=========================================================================
39  REGISTERS
40  -----------------------------------------------------------------------*/
41 
45 enum {
46  SEESAW_STATUS_BASE = 0x00,
47  SEESAW_GPIO_BASE = 0x01,
48  SEESAW_SERCOM0_BASE = 0x02,
49 
50  SEESAW_TIMER_BASE = 0x08,
51  SEESAW_ADC_BASE = 0x09,
52  SEESAW_DAC_BASE = 0x0A,
53  SEESAW_INTERRUPT_BASE = 0x0B,
54  SEESAW_DAP_BASE = 0x0C,
55  SEESAW_EEPROM_BASE = 0x0D,
56  SEESAW_NEOPIXEL_BASE = 0x0E,
57  SEESAW_TOUCH_BASE = 0x0F,
58  SEESAW_KEYPAD_BASE = 0x10,
59  SEESAW_ENCODER_BASE = 0x11,
60 };
61 
64 enum {
65  SEESAW_GPIO_DIRSET_BULK = 0x02,
66  SEESAW_GPIO_DIRCLR_BULK = 0x03,
67  SEESAW_GPIO_BULK = 0x04,
68  SEESAW_GPIO_BULK_SET = 0x05,
69  SEESAW_GPIO_BULK_CLR = 0x06,
70  SEESAW_GPIO_BULK_TOGGLE = 0x07,
71  SEESAW_GPIO_INTENSET = 0x08,
72  SEESAW_GPIO_INTENCLR = 0x09,
73  SEESAW_GPIO_INTFLAG = 0x0A,
74  SEESAW_GPIO_PULLENSET = 0x0B,
75  SEESAW_GPIO_PULLENCLR = 0x0C,
76 };
77 
80 enum {
81  SEESAW_STATUS_HW_ID = 0x01,
82  SEESAW_STATUS_VERSION = 0x02,
83  SEESAW_STATUS_OPTIONS = 0x03,
84  SEESAW_STATUS_TEMP = 0x04,
85  SEESAW_STATUS_SWRST = 0x7F,
86 };
87 
90 enum {
91  SEESAW_TIMER_STATUS = 0x00,
92  SEESAW_TIMER_PWM = 0x01,
93  SEESAW_TIMER_FREQ = 0x02,
94 };
95 
98 enum {
99  SEESAW_ADC_STATUS = 0x00,
100  SEESAW_ADC_INTEN = 0x02,
101  SEESAW_ADC_INTENCLR = 0x03,
102  SEESAW_ADC_WINMODE = 0x04,
103  SEESAW_ADC_WINTHRESH = 0x05,
104  SEESAW_ADC_CHANNEL_OFFSET = 0x07,
105 };
106 
109 enum {
110  SEESAW_SERCOM_STATUS = 0x00,
111  SEESAW_SERCOM_INTEN = 0x02,
112  SEESAW_SERCOM_INTENCLR = 0x03,
113  SEESAW_SERCOM_BAUD = 0x04,
114  SEESAW_SERCOM_DATA = 0x05,
115 };
116 
119 enum {
120  SEESAW_NEOPIXEL_STATUS = 0x00,
121  SEESAW_NEOPIXEL_PIN = 0x01,
122  SEESAW_NEOPIXEL_SPEED = 0x02,
123  SEESAW_NEOPIXEL_BUF_LENGTH = 0x03,
124  SEESAW_NEOPIXEL_BUF = 0x04,
125  SEESAW_NEOPIXEL_SHOW = 0x05,
126 };
127 
130 enum {
131  SEESAW_TOUCH_CHANNEL_OFFSET = 0x10,
132 };
133 
136 enum {
137  SEESAW_KEYPAD_STATUS = 0x00,
138  SEESAW_KEYPAD_EVENT = 0x01,
139  SEESAW_KEYPAD_INTENSET = 0x02,
140  SEESAW_KEYPAD_INTENCLR = 0x03,
141  SEESAW_KEYPAD_COUNT = 0x04,
142  SEESAW_KEYPAD_FIFO = 0x10,
143 };
144 
147 enum {
148  SEESAW_KEYPAD_EDGE_HIGH = 0,
149  SEESAW_KEYPAD_EDGE_LOW,
150  SEESAW_KEYPAD_EDGE_FALLING,
151  SEESAW_KEYPAD_EDGE_RISING,
152 };
153 
156 enum {
157  SEESAW_ENCODER_STATUS = 0x00,
158  SEESAW_ENCODER_INTENSET = 0x02,
159  SEESAW_ENCODER_INTENCLR = 0x03,
160  SEESAW_ENCODER_POSITION = 0x04,
161  SEESAW_ENCODER_DELTA = 0x05,
162 };
163 
164 #define ADC_INPUT_0_PIN 2
165 #define ADC_INPUT_1_PIN 3
166 #define ADC_INPUT_2_PIN 4
167 #define ADC_INPUT_3_PIN 5
168 
169 #define PWM_0_PIN 4
170 #define PWM_1_PIN 5
171 #define PWM_2_PIN 6
172 #define PWM_3_PIN 7
173 
174 #ifndef INPUT_PULLDOWN
175 #define INPUT_PULLDOWN \
176  0x03
177 #endif
179 
180 /*=========================================================================*/
181 
182 #define SEESAW_HW_ID_CODE 0x55
183 #define SEESAW_EEPROM_I2C_ADDR \
184  0x3F
185 
187 
188 union keyEventRaw {
189  struct {
190  uint8_t EDGE : 2;
191  uint8_t NUM : 6;
192  } bit;
193  uint8_t reg;
194 };
195 
197 union keyEvent {
198  struct {
199  uint8_t EDGE : 2;
200  uint16_t NUM : 14;
201  } bit;
202  uint16_t reg;
203 };
204 
206 union keyState {
207  struct {
208  uint8_t STATE : 1;
209  uint8_t ACTIVE : 4;
210  } bit;
211  uint8_t reg;
212 };
213 
214 /**************************************************************************/
219 /**************************************************************************/
220 class Adafruit_seesaw : public Print {
221 public:
222  // constructors
223  Adafruit_seesaw(TwoWire *Wi = NULL);
224  ~Adafruit_seesaw(void){};
225 
226  bool begin(uint8_t addr = SEESAW_ADDRESS, int8_t flow = -1,
227  bool reset = true);
228  uint32_t getOptions();
229  uint32_t getVersion();
230  void SWReset();
231 
232  void pinMode(uint8_t pin, uint8_t mode);
233  void pinModeBulk(uint32_t pins, uint8_t mode);
234  void pinModeBulk(uint32_t pinsa, uint32_t pinsb, uint8_t mode);
235  virtual void analogWrite(uint8_t pin, uint16_t value, uint8_t width = 8);
236  void digitalWrite(uint8_t pin, uint8_t value);
237  void digitalWriteBulk(uint32_t pins, uint8_t value);
238  void digitalWriteBulk(uint32_t pinsa, uint32_t pinsb, uint8_t value);
239 
240  bool digitalRead(uint8_t pin);
241  uint32_t digitalReadBulk(uint32_t pins);
242  uint32_t digitalReadBulkB(uint32_t pins);
243 
244  void setGPIOInterrupts(uint32_t pins, bool enabled);
245 
246  virtual uint16_t analogRead(uint8_t pin);
247 
248  uint16_t touchRead(uint8_t pin);
249 
250  virtual void setPWMFreq(uint8_t pin, uint16_t freq);
251 
252  void enableSercomDataRdyInterrupt(uint8_t sercom = 0);
253  void disableSercomDataRdyInterrupt(uint8_t sercom = 0);
254 
255  char readSercomData(uint8_t sercom = 0);
256 
257  void EEPROMWrite8(uint8_t addr, uint8_t val);
258  void EEPROMWrite(uint8_t addr, uint8_t *buf, uint8_t size);
259  uint8_t EEPROMRead8(uint8_t addr);
260 
261  void setI2CAddr(uint8_t addr);
262  uint8_t getI2CAddr();
263 
264  void UARTSetBaud(uint32_t baud);
265 
266  void setKeypadEvent(uint8_t key, uint8_t edge, bool enable = true);
267  void enableKeypadInterrupt();
268  void disableKeypadInterrupt();
269  uint8_t getKeypadCount();
270  void readKeypad(keyEventRaw *buf, uint8_t count);
271 
272  float getTemp();
273 
274  int32_t getEncoderPosition();
275  int32_t getEncoderDelta();
276  void enableEncoderInterrupt();
277  void disableEncoderInterrupt();
278  void setEncoderPosition(int32_t pos);
279 
280  virtual size_t write(uint8_t);
281  virtual size_t write(const char *str);
282 
283 protected:
284  uint8_t _i2caddr;
285  TwoWire *_i2cbus;
286  int8_t _flow;
288  void write8(byte regHigh, byte regLow, byte value);
289  uint8_t read8(byte regHigh, byte regLow, uint16_t delay = 125);
290 
291  void read(uint8_t regHigh, uint8_t regLow, uint8_t *buf, uint8_t num,
292  uint16_t delay = 125);
293  void write(uint8_t regHigh, uint8_t regLow, uint8_t *buf, uint8_t num);
294  void writeEmpty(uint8_t regHigh, uint8_t regLow);
295  void _i2c_init();
296 
297  /*=========================================================================
298  REGISTER BITFIELDS
299  -----------------------------------------------------------------------*/
300 
303  union sercom_inten {
304  struct {
305  uint8_t DATA_RDY : 1;
306  } bit;
307  uint8_t reg;
308  };
310 
311  /*=========================================================================*/
312 };
313 
314 #endif
Definition: Adafruit_seesaw.h:206
uint8_t DATA_RDY
this bit is set when data becomes available
Definition: Adafruit_seesaw.h:305
int8_t _flow
Definition: Adafruit_seesaw.h:286
Definition: Adafruit_seesaw.h:303
uint8_t _i2caddr
Definition: Adafruit_seesaw.h:284
uint8_t NUM
the event number
Definition: Adafruit_seesaw.h:191
uint8_t reg
register format
Definition: Adafruit_seesaw.h:193
uint8_t ACTIVE
the registered events for that key
Definition: Adafruit_seesaw.h:209
uint8_t STATE
the current state of the key
Definition: Adafruit_seesaw.h:208
uint16_t NUM
the event number
Definition: Adafruit_seesaw.h:200
Class that stores state and functions for interacting with seesaw helper IC.
Definition: Adafruit_seesaw.h:220
uint16_t reg
register format
Definition: Adafruit_seesaw.h:202
uint8_t EDGE
the edge that was triggered
Definition: Adafruit_seesaw.h:190
uint8_t reg
full register
Definition: Adafruit_seesaw.h:307
#define SEESAW_ADDRESS
Default Seesaw I2C address.
Definition: Adafruit_seesaw.h:35
TwoWire * _i2cbus
Definition: Adafruit_seesaw.h:285
Definition: Adafruit_seesaw.h:197
uint8_t EDGE
the edge that was triggered
Definition: Adafruit_seesaw.h:199
Definition: Adafruit_seesaw.h:188
uint8_t reg
register format
Definition: Adafruit_seesaw.h:211
struct keyEventRaw::@11 bit
bitfield format
sercom_inten _sercom_inten
sercom interrupt enable register instance
Definition: Adafruit_seesaw.h:309