kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Added micropython examples for Pot and Enc
rodzic
1b860840a3
commit
4b850b9290
|
@ -47,6 +47,14 @@ namespace pimoroni {
|
|||
void BreakoutEncoder::set_address(uint8_t address) {
|
||||
ioe.set_address(address);
|
||||
}
|
||||
|
||||
bool BreakoutEncoder::get_interrupt_flag() {
|
||||
return ioe.get_interrupt_flag();
|
||||
}
|
||||
|
||||
void BreakoutEncoder::clear_interrupt_flag() {
|
||||
ioe.clear_interrupt_flag();
|
||||
}
|
||||
|
||||
BreakoutEncoder::Direction BreakoutEncoder::get_direction() {
|
||||
return direction;
|
||||
|
|
|
@ -78,6 +78,8 @@ namespace pimoroni {
|
|||
|
||||
// Calls through to IOExpander class
|
||||
void set_address(uint8_t address);
|
||||
bool get_interrupt_flag();
|
||||
void clear_interrupt_flag();
|
||||
|
||||
// Encoder breakout specific
|
||||
Direction get_direction();
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
from breakout_encoder import BreakoutEncoder
|
||||
|
||||
steps_per_rev = 24
|
||||
|
||||
enc = BreakoutEncoder()
|
||||
|
||||
enc.set_brightness(1.0)
|
||||
# enc.set_direction(BreakoutEncoder.DIRECTION_CCW) # Uncomment this to flip the direction
|
||||
|
||||
|
||||
# From CPython Lib/colorsys.py
|
||||
def hsv_to_rgb(h, s, v):
|
||||
if s == 0.0:
|
||||
return v, v, v
|
||||
i = int(h * 6.0)
|
||||
f = (h * 6.0) - i
|
||||
p = v * (1.0 - s)
|
||||
q = v * (1.0 - s * f)
|
||||
t = v * (1.0 - s * (1.0 - f))
|
||||
i = i % 6
|
||||
if i == 0:
|
||||
return v, t, p
|
||||
if i == 1:
|
||||
return q, v, p
|
||||
if i == 2:
|
||||
return p, v, t
|
||||
if i == 3:
|
||||
return p, q, v
|
||||
if i == 4:
|
||||
return t, p, v
|
||||
if i == 5:
|
||||
return v, p, q
|
||||
|
||||
|
||||
def count_changed(count):
|
||||
print("Count: ", count, sep="")
|
||||
h = ((count % steps_per_rev) * 360.0) / steps_per_rev # Convert the count to a colour hue
|
||||
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
|
||||
enc.set_led(r, g, b)
|
||||
|
||||
|
||||
count = 0
|
||||
|
||||
count_changed(count)
|
||||
enc.clear_interrupt_flag()
|
||||
|
||||
while True:
|
||||
if enc.get_interrupt_flag():
|
||||
count = enc.read()
|
||||
enc.clear_interrupt_flag()
|
||||
|
||||
while count < 0:
|
||||
count += steps_per_rev
|
||||
|
||||
count_changed(count)
|
|
@ -0,0 +1,45 @@
|
|||
import time
|
||||
from breakout_potentiometer import BreakoutPotentiometer
|
||||
|
||||
pot = BreakoutPotentiometer()
|
||||
|
||||
pot.set_brightness(1.0)
|
||||
# pot.set_direction(BreakoutPotentiometer.DIRECTION_CCW) # Uncomment this to flip the direction
|
||||
|
||||
|
||||
# From CPython Lib/colorsys.py
|
||||
def hsv_to_rgb(h, s, v):
|
||||
if s == 0.0:
|
||||
return v, v, v
|
||||
i = int(h * 6.0)
|
||||
f = (h * 6.0) - i
|
||||
p = v * (1.0 - s)
|
||||
q = v * (1.0 - s * f)
|
||||
t = v * (1.0 - s * (1.0 - f))
|
||||
i = i % 6
|
||||
if i == 0:
|
||||
return v, t, p
|
||||
if i == 1:
|
||||
return q, v, p
|
||||
if i == 2:
|
||||
return p, v, t
|
||||
if i == 3:
|
||||
return p, q, v
|
||||
if i == 4:
|
||||
return t, p, v
|
||||
if i == 5:
|
||||
return v, p, q
|
||||
|
||||
|
||||
val = 0
|
||||
|
||||
while True:
|
||||
val = pot.read()
|
||||
h = val * 240.0 # Colour range from red to blue
|
||||
|
||||
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
|
||||
|
||||
print("Percent: ", int(val * 100), "%", sep="")
|
||||
pot.set_led(r, g, b)
|
||||
|
||||
time.sleep(0.02)
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
/***** Methods *****/
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutEncoder_set_address_obj, 2, BreakoutEncoder_set_address);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutEncoder_get_interrupt_flag_obj, BreakoutEncoder_get_interrupt_flag);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutEncoder_clear_interrupt_flag_obj, BreakoutEncoder_clear_interrupt_flag);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutEncoder_get_direction_obj, BreakoutEncoder_get_direction);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutEncoder_set_direction_obj, 2, BreakoutEncoder_set_direction);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutEncoder_set_brightness_obj, 2, BreakoutEncoder_set_brightness);
|
||||
|
@ -16,6 +18,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(BreakoutEncoder_read_obj, BreakoutEncoder_read);
|
|||
/***** Binding of Methods *****/
|
||||
STATIC const mp_rom_map_elem_t BreakoutEncoder_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_address), MP_ROM_PTR(&BreakoutEncoder_set_address_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_interrupt_flag), MP_ROM_PTR(&BreakoutEncoder_get_interrupt_flag_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_clear_interrupt_flag), MP_ROM_PTR(&BreakoutEncoder_clear_interrupt_flag_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_direction), MP_ROM_PTR(&BreakoutEncoder_get_direction_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_direction), MP_ROM_PTR(&BreakoutEncoder_set_direction_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_brightness), MP_ROM_PTR(&BreakoutEncoder_set_brightness_obj) },
|
||||
|
|
|
@ -127,6 +127,18 @@ mp_obj_t BreakoutEncoder_set_address(size_t n_args, const mp_obj_t *pos_args, mp
|
|||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutEncoder_get_interrupt_flag(mp_obj_t self_in) {
|
||||
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
||||
return mp_obj_new_bool(self->breakout->get_interrupt_flag());
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutEncoder_clear_interrupt_flag(mp_obj_t self_in) {
|
||||
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
||||
self->breakout->clear_interrupt_flag();
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutEncoder_get_direction(mp_obj_t self_in) {
|
||||
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
||||
return mp_obj_new_bool(self->breakout->get_direction());
|
||||
|
|
|
@ -8,6 +8,8 @@ extern const mp_obj_type_t breakout_encoder_BreakoutEncoder_type;
|
|||
extern void BreakoutEncoder_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
extern mp_obj_t BreakoutEncoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
|
||||
extern mp_obj_t BreakoutEncoder_set_address(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t BreakoutEncoder_get_interrupt_flag(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutEncoder_clear_interrupt_flag(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutEncoder_get_direction(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutEncoder_set_direction(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t BreakoutEncoder_set_brightness(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
|
|
Ładowanie…
Reference in New Issue