kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Switch C++ to use Direction enum for functions and added Direction contants to MP
rodzic
4a206a9b70
commit
d4ba1d97d9
|
@ -48,12 +48,12 @@ namespace pimoroni {
|
||||||
ioe.set_address(address);
|
ioe.set_address(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BreakoutEncoder::get_direction(void) {
|
BreakoutEncoder::Direction BreakoutEncoder::get_direction() {
|
||||||
return direction_cw;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BreakoutEncoder::set_direction(bool clockwise) {
|
void BreakoutEncoder::set_direction(Direction direction) {
|
||||||
direction_cw = clockwise;
|
this->direction = direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BreakoutEncoder::set_brightness(float brightness) {
|
void BreakoutEncoder::set_brightness(float brightness) {
|
||||||
|
@ -77,7 +77,7 @@ namespace pimoroni {
|
||||||
|
|
||||||
int16_t BreakoutEncoder::read() {
|
int16_t BreakoutEncoder::read() {
|
||||||
int16_t count = ioe.read_rotary_encoder(ENC_CHANNEL);
|
int16_t count = ioe.read_rotary_encoder(ENC_CHANNEL);
|
||||||
if(!direction_cw)
|
if(direction != DIRECTION_CW)
|
||||||
count = 0 - count;
|
count = 0 - count;
|
||||||
|
|
||||||
ioe.clear_interrupt_flag();
|
ioe.clear_interrupt_flag();
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace pimoroni {
|
||||||
public:
|
public:
|
||||||
static const uint8_t DEFAULT_I2C_ADDRESS = 0x0F;
|
static const uint8_t DEFAULT_I2C_ADDRESS = 0x0F;
|
||||||
static constexpr float DEFAULT_BRIGHTNESS = 1.0f; //Effectively the maximum fraction of the period that the LED will be on
|
static constexpr float DEFAULT_BRIGHTNESS = 1.0f; //Effectively the maximum fraction of the period that the LED will be on
|
||||||
static const bool DEFAULT_DIRECTION = DIRECTION_CW;
|
static const Direction DEFAULT_DIRECTION = DIRECTION_CW;
|
||||||
static const uint8_t PIN_UNUSED = UINT8_MAX;
|
static const uint8_t PIN_UNUSED = UINT8_MAX;
|
||||||
static const uint32_t DEFAULT_TIMEOUT = 1;
|
static const uint32_t DEFAULT_TIMEOUT = 1;
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ namespace pimoroni {
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private:
|
private:
|
||||||
IOExpander ioe;
|
IOExpander ioe;
|
||||||
bool direction_cw = DEFAULT_DIRECTION;
|
Direction direction = DEFAULT_DIRECTION;
|
||||||
float brightness = DEFAULT_BRIGHTNESS;
|
float brightness = DEFAULT_BRIGHTNESS;
|
||||||
uint8_t interrupt_pin = PIN_UNUSED; // A local copy of the value passed to the IOExpander, used in initialisation
|
uint8_t interrupt_pin = PIN_UNUSED; // A local copy of the value passed to the IOExpander, used in initialisation
|
||||||
|
|
||||||
|
@ -80,8 +80,8 @@ namespace pimoroni {
|
||||||
void set_address(uint8_t address);
|
void set_address(uint8_t address);
|
||||||
|
|
||||||
// Encoder breakout specific
|
// Encoder breakout specific
|
||||||
bool get_direction();
|
Direction get_direction();
|
||||||
void set_direction(bool clockwise);
|
void set_direction(Direction direction);
|
||||||
|
|
||||||
void set_brightness(float brightness);
|
void set_brightness(float brightness);
|
||||||
void set_led(uint8_t r, uint8_t g, uint8_t b);
|
void set_led(uint8_t r, uint8_t g, uint8_t b);
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace pimoroni {
|
||||||
ioe.set_mode(POT_TERM_B, IOExpander::PIN_OUT);
|
ioe.set_mode(POT_TERM_B, IOExpander::PIN_OUT);
|
||||||
ioe.set_mode(POT_INPUT, IOExpander::PIN_ADC);
|
ioe.set_mode(POT_INPUT, IOExpander::PIN_ADC);
|
||||||
|
|
||||||
if(direction_cw) {
|
if(direction == DIRECTION_CW) {
|
||||||
// Clockwise increasing
|
// Clockwise increasing
|
||||||
ioe.output(POT_TERM_A, IOExpander::LOW);
|
ioe.output(POT_TERM_A, IOExpander::LOW);
|
||||||
ioe.output(POT_TERM_B, IOExpander::HIGH);
|
ioe.output(POT_TERM_B, IOExpander::HIGH);
|
||||||
|
@ -58,7 +58,7 @@ namespace pimoroni {
|
||||||
ioe.set_address(address);
|
ioe.set_address(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
float BreakoutPotentiometer::get_adc_vref(void) {
|
float BreakoutPotentiometer::get_adc_vref() {
|
||||||
return ioe.get_adc_vref();
|
return ioe.get_adc_vref();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,12 +66,12 @@ namespace pimoroni {
|
||||||
ioe.set_adc_vref(vref);
|
ioe.set_adc_vref(vref);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BreakoutPotentiometer::get_direction(void) {
|
BreakoutPotentiometer::Direction BreakoutPotentiometer::get_direction() {
|
||||||
return direction_cw;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BreakoutPotentiometer::set_direction(bool clockwise) {
|
void BreakoutPotentiometer::set_direction(Direction direction) {
|
||||||
if(clockwise) {
|
if(direction == DIRECTION_CW) {
|
||||||
// Clockwise increasing
|
// Clockwise increasing
|
||||||
ioe.output(POT_TERM_A, IOExpander::LOW);
|
ioe.output(POT_TERM_A, IOExpander::LOW);
|
||||||
ioe.output(POT_TERM_B, IOExpander::HIGH);
|
ioe.output(POT_TERM_B, IOExpander::HIGH);
|
||||||
|
@ -81,7 +81,7 @@ namespace pimoroni {
|
||||||
ioe.output(POT_TERM_A, IOExpander::HIGH);
|
ioe.output(POT_TERM_A, IOExpander::HIGH);
|
||||||
ioe.output(POT_TERM_B, IOExpander::LOW);
|
ioe.output(POT_TERM_B, IOExpander::LOW);
|
||||||
}
|
}
|
||||||
direction_cw = clockwise;
|
this->direction = direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BreakoutPotentiometer::set_brightness(float brightness) {
|
void BreakoutPotentiometer::set_brightness(float brightness) {
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace pimoroni {
|
||||||
public:
|
public:
|
||||||
static const uint8_t DEFAULT_I2C_ADDRESS = 0x0E;
|
static const uint8_t DEFAULT_I2C_ADDRESS = 0x0E;
|
||||||
static constexpr float DEFAULT_BRIGHTNESS = 1.0f; //Effectively the maximum fraction of the period that the LED will be on
|
static constexpr float DEFAULT_BRIGHTNESS = 1.0f; //Effectively the maximum fraction of the period that the LED will be on
|
||||||
static const bool DEFAULT_DIRECTION = DIRECTION_CW;
|
static const Direction DEFAULT_DIRECTION = DIRECTION_CW;
|
||||||
static const uint8_t PIN_UNUSED = UINT8_MAX;
|
static const uint8_t PIN_UNUSED = UINT8_MAX;
|
||||||
static const uint32_t DEFAULT_TIMEOUT = 1;
|
static const uint32_t DEFAULT_TIMEOUT = 1;
|
||||||
static const uint32_t DEFAULT_ADC_TIMEOUT = 1;
|
static const uint32_t DEFAULT_ADC_TIMEOUT = 1;
|
||||||
|
@ -43,7 +43,7 @@ namespace pimoroni {
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private:
|
private:
|
||||||
IOExpander ioe;
|
IOExpander ioe;
|
||||||
bool direction_cw = DEFAULT_DIRECTION;
|
Direction direction = DEFAULT_DIRECTION;
|
||||||
float brightness = DEFAULT_BRIGHTNESS;
|
float brightness = DEFAULT_BRIGHTNESS;
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,8 +80,8 @@ namespace pimoroni {
|
||||||
void set_adc_vref(float vref);
|
void set_adc_vref(float vref);
|
||||||
|
|
||||||
// Potentiometer breakout specific
|
// Potentiometer breakout specific
|
||||||
bool get_direction();
|
Direction get_direction();
|
||||||
void set_direction(bool clockwise);
|
void set_direction(Direction direction);
|
||||||
|
|
||||||
void set_brightness(float brightness);
|
void set_brightness(float brightness);
|
||||||
void set_led(uint8_t r, uint8_t g, uint8_t b);
|
void set_led(uint8_t r, uint8_t g, uint8_t b);
|
||||||
|
|
|
@ -22,6 +22,8 @@ STATIC const mp_rom_map_elem_t BreakoutEncoder_locals_dict_table[] = {
|
||||||
{ MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&BreakoutEncoder_set_led_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&BreakoutEncoder_set_led_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_available), MP_ROM_PTR(&BreakoutEncoder_available_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_available), MP_ROM_PTR(&BreakoutEncoder_available_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&BreakoutEncoder_read_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&BreakoutEncoder_read_obj) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_DIRECTION_CW), MP_ROM_INT(1) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_DIRECTION_CCW), MP_ROM_INT(0) },
|
||||||
};
|
};
|
||||||
STATIC MP_DEFINE_CONST_DICT(BreakoutEncoder_locals_dict, BreakoutEncoder_locals_dict_table);
|
STATIC MP_DEFINE_CONST_DICT(BreakoutEncoder_locals_dict, BreakoutEncoder_locals_dict_table);
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ mp_obj_t BreakoutEncoder_set_direction(size_t n_args, const mp_obj_t *pos_args,
|
||||||
|
|
||||||
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_encoder_BreakoutEncoder_obj_t);
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_encoder_BreakoutEncoder_obj_t);
|
||||||
|
|
||||||
self->breakout->set_direction(args[ARG_clockwise].u_bool);
|
self->breakout->set_direction(args[ARG_clockwise].u_bool ? BreakoutEncoder::DIRECTION_CW : BreakoutEncoder::DIRECTION_CCW);
|
||||||
|
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,8 @@ STATIC const mp_rom_map_elem_t BreakoutPotentiometer_locals_dict_table[] = {
|
||||||
{ MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&BreakoutPotentiometer_set_led_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&BreakoutPotentiometer_set_led_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&BreakoutPotentiometer_read_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&BreakoutPotentiometer_read_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_read_raw), MP_ROM_PTR(&BreakoutPotentiometer_read_raw_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_read_raw), MP_ROM_PTR(&BreakoutPotentiometer_read_raw_obj) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_DIRECTION_CW), MP_ROM_INT(1) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_DIRECTION_CCW), MP_ROM_INT(0) },
|
||||||
};
|
};
|
||||||
STATIC MP_DEFINE_CONST_DICT(BreakoutPotentiometer_locals_dict, BreakoutPotentiometer_locals_dict_table);
|
STATIC MP_DEFINE_CONST_DICT(BreakoutPotentiometer_locals_dict, BreakoutPotentiometer_locals_dict_table);
|
||||||
|
|
||||||
|
|
|
@ -167,7 +167,7 @@ mp_obj_t BreakoutPotentiometer_set_direction(size_t n_args, const mp_obj_t *pos_
|
||||||
|
|
||||||
breakout_potentiometer_BreakoutPotentiometer_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_potentiometer_BreakoutPotentiometer_obj_t);
|
breakout_potentiometer_BreakoutPotentiometer_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_potentiometer_BreakoutPotentiometer_obj_t);
|
||||||
|
|
||||||
self->breakout->set_direction(args[ARG_clockwise].u_bool);
|
self->breakout->set_direction(args[ARG_clockwise].u_bool ? BreakoutPotentiometer::DIRECTION_CW : BreakoutPotentiometer::DIRECTION_CCW);
|
||||||
|
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue