Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add STEMMA QT Rotary Encoder Driver #524

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,84 @@
/*!
* @file WipperSnapper_I2C_Driver_RotaryEncoder.h
*
* Device driver for the STEMMA QT Rotary Encoder.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2023 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_RotaryEncoder_H
#define WipperSnapper_I2C_Driver_RotaryEncoder_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_Seesaw.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a STEMMA QT Rotary Encoder.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_RotaryEncoder : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a STEMMA QT Rotary Encoder.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_RotaryEncoder(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c; //TODO: Is this necessary if it doesn't need to get passed to seesaw::begin?
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an STEMMA QT Rotary Encoder.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_RotaryEncoder() {
// Called when a RotaryEncoder component is deleted.
delete _RotaryEncoder;
}

/*******************************************************************************/
/*!
@brief Initializes the STEMMA QT Rotary Encoder and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_RotaryEncoder = new Adafruit_seesaw();
return _RotaryEncoder->begin((uint8_t)_sensorAddress); //TODO: Do i need to send constructor params for unit8_t "flow" or bool "reset"?
Copy link
Contributor

@tyeth tyeth Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add reset, not sure about flow I'll have to do some reading.
Also we have to pass the i2c bus (TwoWire object passed into the constructor, and here available as _i2c) into the sensor/seesaw in addition to i2c address. Have a play with the arduino encoder-example from adafruit seesaw library and try passing in a Wire object so you get the feel for it. The reason is the QTPY boards use the second TwoWire bus (a.k.a. Wire1) so we have to use the overload of seesaw constructor/begin that supports defining the bus. See here https://github.com/adafruit/Adafruit_Wippersnapper_Arduino/blob/main/src/components/i2c/drivers/WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor.h#L44

}

/*******************************************************************************/
/*!
@brief Reads the current postion of a rotary encoder.
@param rawEvent
Pointer to an Adafruit_Sensor event.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEncoderPosition(sensors_event_t *rawEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment this should be getEventRaw like the stemma_soil sensor does, and we'll have similar methods for other event types in the future (or maybe refactor to do encoder / buttons / neopixels differently over i2c). See https://github.com/adafruit/Adafruit_Wippersnapper_Arduino/blob/main/src/components/i2c/drivers/WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor.h#L85

rawEvent->data = _RotaryEncoder->getEncoderPosition(); //it doesn't look like Adafruit_Sensor was built for this sort of thing
return true;
}



protected:
Adafruit_seesaw *_RotaryEncoder; ///< Pointer to RotaryEncoder object
};

#endif // WipperSnapper_I2C_Driver_RotaryEncoder