Add i2c joystick 2 esphome component#59
Conversation
|
Hi @ItsRebaseTime, Thanks for your contribution! We'll run some test and review/merge as soon as we can. |
| void I2CJoystick2Component::setup() { | ||
| ESP_LOGCONFIG(TAG, "Setting up I2C Joystick2..."); | ||
| } |
There was a problem hiding this comment.
logging in setup shouldn't use ESP_LOGCONFIG
| void I2CJoystick2Component::setup() { | |
| ESP_LOGCONFIG(TAG, "Setting up I2C Joystick2..."); | |
| } | |
| void I2CJoystick2Component::setup() { | |
| ESP_LOGD(TAG, "Setting up I2C Joystick2..."); | |
| } |
| ESP_LOGCONFIG(TAG, "I2C Joystick2:"); | ||
| LOG_I2C_DEVICE(this); | ||
|
|
||
| if (this->is_failed()) { | ||
| ESP_LOGE(TAG, "Communication with Joystick2 failed"); | ||
| return; | ||
| } | ||
|
|
||
| ESP_LOGCONFIG(TAG, " Firmware version: %u", this->firmware_version_); |
There was a problem hiding this comment.
when use ESP_LOGCONFIG, it would be nice to merge the information together, following Logging Best Practices.
| ESP_LOGCONFIG(TAG, "I2C Joystick2:"); | |
| LOG_I2C_DEVICE(this); | |
| if (this->is_failed()) { | |
| ESP_LOGE(TAG, "Communication with Joystick2 failed"); | |
| return; | |
| } | |
| ESP_LOGCONFIG(TAG, " Firmware version: %u", this->firmware_version_); | |
| if (this->is_failed()) { | |
| ESP_LOGE(TAG, "Communication with Joystick2 failed"); | |
| return; | |
| } | |
| ESP_LOGCONFIG(TAG, "I2C Joystick2: \n" | |
| " Firmware version: %u", | |
| this->firmware_version_); | |
| LOG_I2C_DEVICE(this); |
| ESP_LOGCONFIG(TAG, " Axis: %s", this->axis_ == AXIS_X ? "X" : "Y"); | ||
| ESP_LOGCONFIG(TAG, " Mode: %u", this->mode_); |
There was a problem hiding this comment.
merge these two together
| ESP_LOGCONFIG(TAG, " Axis: %s", this->axis_ == AXIS_X ? "X" : "Y"); | |
| ESP_LOGCONFIG(TAG, " Mode: %u", this->mode_); | |
| ESP_LOGCONFIG(TAG, " Axis: %s\n" | |
| " Mode: %u", | |
| this->axis_ == AXIS_X ? "X" : "Y" | |
| this->mode_); |
| AXIS_OPTIONS = { | ||
| "x": 0, | ||
| "y": 1, | ||
| } | ||
|
|
||
| MODE_OPTIONS = { | ||
| "adc_16bit": 0, | ||
| "adc_8bit": 1, | ||
| "offset_12bit": 2, | ||
| "offset_8bit": 3, | ||
| } |
There was a problem hiding this comment.
probably shoud use code gen to create enum, as it can map the enum defined in C/C++:
enum Axis : uint8_t {
AXIS_X = 0,
AXIS_Y = 1,
};
enum OutputMode : uint8_t {
MODE_ADC_16BIT = 0,
MODE_ADC_8BIT = 1,
MODE_OFFSET_12BIT = 2,
MODE_OFFSET_8BIT = 3,
};
And to use the enum provided by ESPHome:
JoyStick2Axis = i2c_joystick_2_ns.enum("Axis")
JoyStick2Modes = i2c_joystick_2_ns.enum("OutputMode")| AXIS_OPTIONS = { | |
| "x": 0, | |
| "y": 1, | |
| } | |
| MODE_OPTIONS = { | |
| "adc_16bit": 0, | |
| "adc_8bit": 1, | |
| "offset_12bit": 2, | |
| "offset_8bit": 3, | |
| } | |
| AXIS_OPTIONS = { | |
| "X": JoyStick2Axis.AXIS_X, | |
| "Y": JoyStick2Axis.AXIS_Y, | |
| } | |
| MODE_OPTIONS = { | |
| "ADC_16BIT": JoyStick2Modes.MODE_ADC_16BIT, | |
| "ADC_8BIT": JoyStick2Modes.MODE_ADC_8BIT, | |
| "OFFSET_12BIT": JoyStick2Modes.MODE_OFFSET_12BIT, | |
| "OFFSET_8BIT": JoyStick2Modes.MODE_OFFSET_8BIT, | |
| } |
| cv.Optional(CONF_AXIS, default="x"): cv.one_of(*AXIS_OPTIONS.keys(), lower=True), | ||
| cv.Optional(CONF_MODE, default="offset_12bit"): cv.one_of(*MODE_OPTIONS.keys(), lower=True), |
There was a problem hiding this comment.
instead use cv.one_of(), we can use cv.enum()
| cv.Optional(CONF_AXIS, default="x"): cv.one_of(*AXIS_OPTIONS.keys(), lower=True), | |
| cv.Optional(CONF_MODE, default="offset_12bit"): cv.one_of(*MODE_OPTIONS.keys(), lower=True), | |
| cv.Optional(CONF_AXIS, default="x"): cv.enum(JoyStick2Axis), | |
| cv.Optional(CONF_MODE, default="offset_12bit"): cv.enum(JoyStick2Modes), |
parker-int64
left a comment
There was a problem hiding this comment.
Great work on this PR! The overall structure looks solid and the implementation is clear.
We just have a few minor suggestions that could help improve readability/robustness:
- The logging style, we recommend following the best practice provided by ESPHome
- The enum definations, use internal API to make it more concise.
Nothing major — once these are addressed, this should be good to go 🚀.
No description provided.