Skip to content

Add i2c joystick 2 esphome component#59

Open
ItsRebaseTime wants to merge 1 commit intom5stack:mainfrom
ItsRebaseTime:i2c_joystick_2
Open

Add i2c joystick 2 esphome component#59
ItsRebaseTime wants to merge 1 commit intom5stack:mainfrom
ItsRebaseTime:i2c_joystick_2

Conversation

@ItsRebaseTime
Copy link
Copy Markdown

No description provided.

@parker-int64
Copy link
Copy Markdown
Contributor

Hi @ItsRebaseTime,

Thanks for your contribution! We'll run some test and review/merge as soon as we can.

Comment on lines +17 to +19
void I2CJoystick2Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up I2C Joystick2...");
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

logging in setup shouldn't use ESP_LOGCONFIG

Suggested change
void I2CJoystick2Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up I2C Joystick2...");
}
void I2CJoystick2Component::setup() {
ESP_LOGD(TAG, "Setting up I2C Joystick2...");
}

Comment on lines +118 to +126
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_);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

when use ESP_LOGCONFIG, it would be nice to merge the information together, following Logging Best Practices.

Suggested change
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);

Comment on lines +147 to +148
ESP_LOGCONFIG(TAG, " Axis: %s", this->axis_ == AXIS_X ? "X" : "Y");
ESP_LOGCONFIG(TAG, " Mode: %u", this->mode_);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

merge these two together

Suggested change
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_);

Comment on lines +12 to +22
AXIS_OPTIONS = {
"x": 0,
"y": 1,
}

MODE_OPTIONS = {
"adc_16bit": 0,
"adc_8bit": 1,
"offset_12bit": 2,
"offset_8bit": 3,
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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")
Suggested change
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,
}

Comment on lines +33 to +34
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),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

instead use cv.one_of(), we can use cv.enum()

Suggested change
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),

Copy link
Copy Markdown
Contributor

@parker-int64 parker-int64 left a comment

Choose a reason for hiding this comment

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

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 🚀.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants