Progress on encoder wheel C++ and MP

pull/774/head
ZodiusInfuser 2023-05-02 17:31:11 +01:00
rodzic d4d6cd1936
commit 387df3bd12
7 zmienionych plików z 60 dodań i 41 usunięć

Wyświetl plik

@ -29,6 +29,12 @@ namespace pimoroni {
success = true;
}
ioe.set_mode(SW_UP, IOExpander::PIN_IN_PU);
ioe.set_mode(SW_DOWN, IOExpander::PIN_IN_PU);
ioe.set_mode(SW_LEFT, IOExpander::PIN_IN_PU);
ioe.set_mode(SW_RIGHT, IOExpander::PIN_IN_PU);
ioe.set_mode(SW_CENTRE, IOExpander::PIN_IN_PU);
}
return success;
@ -71,7 +77,20 @@ namespace pimoroni {
}
bool BreakoutEncoderWheel::pressed(uint button) {
return 0; // TODO
switch(button) {
case 0:
return ioe.input(SW_UP) == 0;
case 1:
return ioe.input(SW_DOWN) == 0;
case 2:
return ioe.input(SW_LEFT) == 0;
case 3:
return ioe.input(SW_RIGHT) == 0;
case 4:
return ioe.input(SW_CENTRE) == 0;
default:
return false;
}
}
int BreakoutEncoderWheel::count() {

Wyświetl plik

@ -17,12 +17,11 @@ PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
HOUR = 4
MINUTE = 5
SECOND = 6
MICROSECOND = 7
# Constants
BRIGHTNESS = 1.0 # The brightness of the LEDs
UPDATES = 50 # How many times the LEDs will be updated per second
UPDATE_RATE = 1 / UPDATES
UPDATE_RATE_US = 1000000 // UPDATES
# Handy values for the number of milliseconds
MILLIS_PER_SECOND = 1000
@ -33,15 +32,15 @@ MILLIS_PER_HALF_DAY = MILLIS_PER_HOUR * 12
# Create a new BreakoutEncoderWheel
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
wheel = BreakoutEncoderWheel(i2c)
rtc = machine.RTC()
rtc = RTC()
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - (time.ticks_ms() / 1000)
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
time_to_sleep = time.ticks_diff(end_time, time.ticks_us())
if time_to_sleep > 0:
time.sleep_us(time_to_sleep)
# Simple function to clamp a value between a minimum and maximum
@ -72,13 +71,13 @@ def led_brightness_at(led, position, half_width=1, span=1):
while True:
# Record the start time of this loop
start_time = time.ticks_ms() / 1000
start_time = time.ticks_us()
# Get the current system time
now = rtc.datetime()
# Convert the seconds, minutes, and hours into milliseconds (this is done to give a smoother animation, particularly for the seconds hand)
sec_as_millis = (now[SECOND] * MILLIS_PER_SECOND) + (now[MICROSECOND] // MILLIS_PER_SECOND)
sec_as_millis = (now[SECOND] * MILLIS_PER_SECOND)
min_as_millis = (now[MINUTE] * MILLIS_PER_MINUTE) + sec_as_millis
hour_as_millis = ((now[HOUR] % 12) * MILLIS_PER_HOUR) + min_as_millis
@ -96,4 +95,4 @@ while True:
wheel.show()
# Sleep until the next update, accounting for how long the above operations took to perform
sleep_until(start_time + UPDATE_RATE)
sleep_until(time.ticks_add(start_time, UPDATE_RATE_US))

Wyświetl plik

@ -23,7 +23,7 @@ PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
BRIGHTNESS_STEP = 0.02 # How much to increase or decrease the brightness each update
SATURATION_STEP = 0.02 # How much to increase or decrease the saturation each update
UPDATES = 50 # How many times to update the LEDs per second
UPDATE_RATE = 1 / UPDATES
UPDATE_RATE_US = 1000000 // UPDATES
# Create a new BreakoutEncoderWheel
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
@ -69,14 +69,14 @@ def clamp01(value):
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - (time.ticks_ms() / 1000)
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
time_to_sleep = time.ticks_diff(end_time, time.ticks_us())
if time_to_sleep > 0:
time.sleep_us(time_to_sleep)
while True:
# Record the start time of this loop
start_time = time.ticks_ms() / 1000
start_time = time.ticks_us()
# If up is pressed, increase the brightness
if wheel.pressed(UP):
@ -138,4 +138,4 @@ while True:
changed = False
# Sleep until the next update, accounting for how long the above operations took to perform
sleep_until(start_time + UPDATE_RATE)
sleep_until(time.ticks_add(start_time, UPDATE_RATE_US))

Wyświetl plik

@ -16,7 +16,7 @@ PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
SPEED = 5 # The speed that the PWM will cycle at
UPDATES = 50 # How many times to update LEDs and Servos per second
UPDATE_RATE = 1 / UPDATES
UPDATE_RATE_US = 1000000 // UPDATES
FREQUENCY = 1000 # The frequency to run the PWM at
# Create a new BreakoutEncoderWheel
@ -36,16 +36,16 @@ offset = 0.0
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - (time.ticks_ms() / 1000)
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
time_to_sleep = time.ticks_diff(end_time, time.ticks_us())
if time_to_sleep > 0:
time.sleep_us(time_to_sleep)
# Make PWM waves until the centre button is pressed
while not wheel.pressed(CENTRE):
# Record the start time of this loop
start_time = time.ticks_ms() / 1000
start_time = time.ticks_us()
offset += SPEED / 1000.0
@ -61,7 +61,7 @@ while not wheel.pressed(CENTRE):
wheel.gpio_pwm_load()
# Sleep until the next update, accounting for how long the above operations took to perform
sleep_until(start_time + UPDATE_RATE)
sleep_until(time.ticks_add(start_time, UPDATE_RATE_US))
# Turn off the PWM outputs
for g in GPIOS:

Wyświetl plik

@ -16,7 +16,7 @@ PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
SPEED = 5 # The speed that the LEDs will cycle at
BRIGHTNESS = 1.0 # The brightness of the LEDs
UPDATES = 50 # How many times the LEDs will be updated per second
UPDATE_RATE = 1 / UPDATES
UPDATE_RATE_US = 1000000 // UPDATES
# Create a new BreakoutEncoderWheel
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
@ -29,16 +29,16 @@ offset = 0.0
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - (time.ticks_ms() / 1000)
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
time_to_sleep = time.ticks_diff(end_time, time.ticks_us())
if time_to_sleep > 0:
time.sleep_us(time_to_sleep)
# Make rainbows
while True:
# Record the start time of this loop
start_time = time.ticks_ms() / 1000
start_time = time.ticks_us()
offset += SPEED / 1000.0
@ -49,4 +49,4 @@ while True:
wheel.show()
# Sleep until the next update, accounting for how long the above operations took to perform
sleep_until(start_time + UPDATE_RATE)
sleep_until(time.ticks_add(start_time, UPDATE_RATE_US))

Wyświetl plik

@ -20,7 +20,7 @@ BRIGHTNESS = 1.0 # The brightness of the LEDs when the stopwa
UPDATES = 50 # How many times the LEDs will be updated per second
MINUTE_UPDATES = UPDATES * 60 # How many times the LEDs will be updated per minute
HOUR_UPDATES = MINUTE_UPDATES * 60 # How many times the LEDs will be updated per hour
UPDATE_RATE = 1 / UPDATES
UPDATE_RATE_US = 1000000 // UPDATES
IDLE_PULSE_MIN = 0.2 # The brightness (between 0.0 and 1.0) that the idle pulse will go down to
IDLE_PULSE_MAX = 0.5 # The brightness (between 0.0 and 1.0) that the idle pulse will go up to
@ -49,13 +49,13 @@ def clamp(n, smallest, largest):
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - (time.ticks_ms() / 1000)
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
time_to_sleep = time.ticks_diff(end_time, time.ticks_us())
if time_to_sleep > 0:
time.sleep_us(time_to_sleep)
# Record the current time
current_time = (time.ticks_ms() / 1000)
current_time = time.ticks_us()
# Run the update loop forever
while True:
@ -100,9 +100,9 @@ while True:
# Set each LED, such that ones below the current time are fully lit, ones after
# are off, and the one at the transition is at a percentage of the brightness
for i in range(NUM_LEDS):
r = clamp(r_to_light - i, 0.0, 1.0) * BRIGHTNESS * 255
g = clamp(g_to_light - i, 0.0, 1.0) * BRIGHTNESS * 255
b = clamp(b_to_light - i, 0.0, 1.0) * BRIGHTNESS * 255
r = int(clamp(r_to_light - i, 0.0, 1.0) * BRIGHTNESS * 255)
g = int(clamp(g_to_light - i, 0.0, 1.0) * BRIGHTNESS * 255)
b = int(clamp(b_to_light - i, 0.0, 1.0) * BRIGHTNESS * 255)
wheel.set_rgb(i, r, g, b)
wheel.show()
@ -122,5 +122,5 @@ while True:
hour_update = 0
# Sleep until the next update, accounting for how long the above operations took to perform
current_time += UPDATE_RATE
current_time = time.ticks_add(current_time, UPDATE_RATE_US)
sleep_until(current_time)

Wyświetl plik

@ -21,10 +21,11 @@ typedef struct _breakout_encoder_wheel_BreakoutEncoderWheel_obj_t {
mp_obj_t BreakoutEncoderWheel_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_encoder_wheel_BreakoutEncoderWheel_obj_t *self = nullptr;
enum { ARG_i2c, ARG_address, ARG_interrupt };
enum { ARG_i2c, ARG_ioe_address, ARG_led_address, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutEncoderWheel::DEFAULT_IOE_I2C_ADDRESS} },
{ MP_QSTR_ioe_address, MP_ARG_INT, {.u_int = BreakoutEncoderWheel::DEFAULT_IOE_I2C_ADDRESS} },
{ MP_QSTR_led_address, MP_ARG_INT, {.u_int = BreakoutEncoderWheel::DEFAULT_LED_I2C_ADDRESS} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} },
};
@ -37,7 +38,7 @@ mp_obj_t BreakoutEncoderWheel_make_new(const mp_obj_type_t *type, size_t n_args,
self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj);
self->breakout = m_new_class(BreakoutEncoderWheel, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int);
self->breakout = m_new_class(BreakoutEncoderWheel, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_ioe_address].u_int, args[ARG_led_address].u_int, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "BreakoutEncoderWheel: breakout not found when initialising");