kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Merge pull request #733 from pimoroni/patch-cpp-init-heap
Refactor out remaining C++ heap usage during init.pull/736/head v1.19.18
commit
c9bee93372
|
@ -8,10 +8,11 @@ namespace pimoroni {
|
|||
static const uint8_t DOT_CHAR_WIDTH = 5;
|
||||
|
||||
struct DotChar {
|
||||
uint16_t code;
|
||||
uint8_t data[DOT_CHAR_WIDTH];
|
||||
};
|
||||
|
||||
static const std::map<uint16_t, DotChar> dotfont = {
|
||||
static const DotChar dotfont[] = {
|
||||
{32, {0x00, 0x00, 0x00, 0x00, 0x00}}, // (space)
|
||||
{33, {0x00, 0x00, 0x5f, 0x00, 0x00}}, // !
|
||||
{34, {0x00, 0x07, 0x00, 0x07, 0x00}}, // "
|
||||
|
|
|
@ -64,13 +64,18 @@ namespace pimoroni {
|
|||
}
|
||||
|
||||
void LTP305::set_character(uint8_t x, uint16_t ch) {
|
||||
std::map<uint16_t, DotChar>::const_iterator it = dotfont.find(ch);
|
||||
uint8_t *data = nullptr;
|
||||
for(auto c : dotfont) {
|
||||
if(c.code == ch) {
|
||||
data = &c.data[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(it != dotfont.end()) {
|
||||
DotChar dchar = it->second;
|
||||
if(data) {
|
||||
for(uint8_t cx = 0; cx < DOT_CHAR_WIDTH; cx++) {
|
||||
for(uint8_t cy = 0; cy < HEIGHT; cy++) {
|
||||
uint8_t c = dchar.data[cx] & (0b1 << cy);
|
||||
uint8_t c = data[cx] & (0b1 << cy);
|
||||
set_pixel(x + cx, cy, c);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,30 +2,6 @@
|
|||
#include <algorithm>
|
||||
|
||||
namespace pimoroni {
|
||||
lookup::lookup(std::initializer_list<uint16_t> values) : lut(values) {
|
||||
}
|
||||
|
||||
uint8_t lookup::index(uint16_t value) {
|
||||
auto it = find(lut.begin(), lut.end(), value);
|
||||
|
||||
if(it == lut.end())
|
||||
return 0;
|
||||
|
||||
return it - lut.begin();
|
||||
}
|
||||
|
||||
uint16_t lookup::value(uint8_t index) {
|
||||
return lut[index];
|
||||
}
|
||||
|
||||
pimoroni::lookup LTR559::lookup_led_current({5, 10, 20, 50, 100});
|
||||
pimoroni::lookup LTR559::lookup_led_duty_cycle({25, 50, 75, 100});
|
||||
pimoroni::lookup LTR559::lookup_led_pulse_freq({30, 40, 50, 60, 70, 80, 90, 100});
|
||||
pimoroni::lookup LTR559::lookup_proximity_meas_rate({10, 50, 70, 100, 200, 500, 1000, 2000});
|
||||
pimoroni::lookup LTR559::lookup_light_integration_time({100, 50, 200, 400, 150, 250, 300, 350});
|
||||
pimoroni::lookup LTR559::lookup_light_repeat_rate({50, 100, 200, 500, 1000, 2000});
|
||||
pimoroni::lookup LTR559::lookup_light_gain({1, 2, 4, 8, 0, 0, 48, 96});
|
||||
|
||||
bool LTR559::init() {
|
||||
if(interrupt != PIN_UNUSED) {
|
||||
gpio_set_function(interrupt, GPIO_FUNC_SIO);
|
||||
|
@ -129,7 +105,7 @@ namespace pimoroni {
|
|||
i2c->read_bytes(address, LTR559_ALS_DATA_CH1, (uint8_t *)&als, 4);
|
||||
data.als0 = als[1];
|
||||
data.als1 = als[0];
|
||||
data.gain = this->lookup_light_gain.value((status >> LTR559_ALS_PS_STATUS_ALS_GAIN_SHIFT) & LTR559_ALS_PS_STATUS_ALS_GAIN_MASK);
|
||||
data.gain = lookup_light_gain[(status >> LTR559_ALS_PS_STATUS_ALS_GAIN_SHIFT) & LTR559_ALS_PS_STATUS_ALS_GAIN_MASK];
|
||||
|
||||
data.ratio = 101.0f;
|
||||
|
||||
|
@ -163,12 +139,12 @@ namespace pimoroni {
|
|||
}
|
||||
|
||||
void LTR559::proximity_led(uint8_t current, uint8_t duty_cycle, uint8_t pulse_freq, uint8_t num_pulses) {
|
||||
current = lookup_led_current.index(current);
|
||||
current = lookup<lookup_led_current>(current);
|
||||
|
||||
duty_cycle = lookup_led_duty_cycle.index(duty_cycle);
|
||||
duty_cycle = lookup<lookup_led_duty_cycle>(duty_cycle);
|
||||
duty_cycle <<= LTR559_PS_LED_DUTY_CYCLE_SHIFT;
|
||||
|
||||
pulse_freq = lookup_led_pulse_freq.index(pulse_freq);
|
||||
pulse_freq = lookup<lookup_led_pulse_freq>(pulse_freq);
|
||||
pulse_freq <<= LTR559_PS_LED_PULSE_FREQ_SHIFT;
|
||||
|
||||
uint8_t buf = current | duty_cycle | pulse_freq;
|
||||
|
@ -180,7 +156,7 @@ namespace pimoroni {
|
|||
|
||||
void LTR559::light_control(bool active, uint8_t gain) {
|
||||
uint8_t buf = 0;
|
||||
gain = lookup_light_gain.index(gain);
|
||||
gain = lookup<lookup_light_gain>(gain);
|
||||
buf |= gain << LTR559_ALS_CONTROL_GAIN_SHIFT;
|
||||
|
||||
if(active)
|
||||
|
@ -223,8 +199,8 @@ namespace pimoroni {
|
|||
|
||||
void LTR559::light_measurement_rate(uint16_t integration_time, uint16_t rate) {
|
||||
data.integration_time = integration_time;
|
||||
integration_time = lookup_light_integration_time.index(integration_time);
|
||||
rate = lookup_light_repeat_rate.index(rate);
|
||||
integration_time = lookup<lookup_light_integration_time>(integration_time);
|
||||
rate = lookup<lookup_light_repeat_rate>(rate);
|
||||
uint8_t buf = 0;
|
||||
buf |= rate;
|
||||
buf |= integration_time << LTR559_ALS_MEAS_RATE_INTEGRATION_TIME_SHIFT;
|
||||
|
@ -232,7 +208,7 @@ namespace pimoroni {
|
|||
}
|
||||
|
||||
void LTR559::proximity_measurement_rate(uint16_t rate) {
|
||||
uint8_t buf = lookup_proximity_meas_rate.index(rate);
|
||||
uint8_t buf = lookup<lookup_proximity_meas_rate>(rate);
|
||||
i2c->write_bytes(address, LTR559_PS_MEAS_RATE, &buf, 1);
|
||||
}
|
||||
|
||||
|
|
|
@ -97,15 +97,6 @@ namespace pimoroni {
|
|||
float lux;
|
||||
} ltr559_reading;
|
||||
|
||||
class lookup {
|
||||
private:
|
||||
std::vector<uint16_t> lut;
|
||||
public:
|
||||
lookup(std::initializer_list<uint16_t> values);
|
||||
uint8_t index(uint16_t value);
|
||||
uint16_t value(uint8_t index);
|
||||
};
|
||||
|
||||
class LTR559 {
|
||||
//--------------------------------------------------
|
||||
// Constants
|
||||
|
@ -131,14 +122,13 @@ namespace pimoroni {
|
|||
const uint8_t address = DEFAULT_I2C_ADDRESS;
|
||||
uint interrupt = PIN_UNUSED;
|
||||
|
||||
static pimoroni::lookup lookup_led_current;
|
||||
static pimoroni::lookup lookup_led_duty_cycle;
|
||||
static pimoroni::lookup lookup_led_pulse_freq;
|
||||
static pimoroni::lookup lookup_proximity_meas_rate;
|
||||
static pimoroni::lookup lookup_light_integration_time;
|
||||
static pimoroni::lookup lookup_light_repeat_rate;
|
||||
static pimoroni::lookup lookup_light_gain;
|
||||
|
||||
static constexpr uint16_t lookup_led_current[5] = {5, 10, 20, 50, 100};
|
||||
static constexpr uint16_t lookup_led_duty_cycle[4] = {25, 50, 75, 100};
|
||||
static constexpr uint16_t lookup_led_pulse_freq[8] = {30, 40, 50, 60, 70, 80, 90, 100};
|
||||
static constexpr uint16_t lookup_proximity_meas_rate[8] = {10, 50, 70, 100, 200, 500, 1000, 2000};
|
||||
static constexpr uint16_t lookup_light_integration_time[8] = {100, 50, 200, 400, 150, 250, 300, 350};
|
||||
static constexpr uint16_t lookup_light_repeat_rate[6] = {50, 100, 200, 500, 1000, 2000};
|
||||
static constexpr uint16_t lookup_light_gain[8] = {1, 2, 4, 8, 0, 0, 48, 96};
|
||||
|
||||
//--------------------------------------------------
|
||||
// Constructors/Destructor
|
||||
|
@ -177,6 +167,15 @@ namespace pimoroni {
|
|||
void proximity_measurement_rate(uint16_t rate);
|
||||
void proximity_offset(uint16_t offset);
|
||||
|
||||
template<auto T>
|
||||
const uint16_t lookup(uint16_t value) {
|
||||
size_t length = sizeof(T) / sizeof(uint16_t);
|
||||
for(auto i = 0u; i < length; i++) {
|
||||
if(T[i] == value) return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
uint16_t bit12_to_uint16(uint16_t value);
|
||||
uint16_t uint16_to_bit12(uint16_t value);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <cmath>
|
||||
|
||||
namespace hershey {
|
||||
#ifndef MICROPY_BUILD_TYPE
|
||||
std::map<std::string, const font_t*> fonts = {
|
||||
{ "sans", &futural },
|
||||
//{ "sans_bold", &futuram },
|
||||
|
@ -13,6 +14,7 @@ namespace hershey {
|
|||
{ "serif", ×r },
|
||||
//{ "serif_bold", ×rb }
|
||||
};
|
||||
#endif
|
||||
|
||||
bool has_font(std::string_view font) {
|
||||
if(font == "sans"
|
||||
|
|
|
@ -124,7 +124,16 @@ mp_obj_t BreakoutDotMatrix_set_character(size_t n_args, const mp_obj_t *pos_args
|
|||
breakout_dotmatrix_BreakoutDotMatrix_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_dotmatrix_BreakoutDotMatrix_obj_t);
|
||||
|
||||
int x = args[ARG_x].u_int;
|
||||
int ch = mp_obj_get_int(args[ARG_ch].u_obj);
|
||||
int ch = 0;
|
||||
|
||||
if(mp_obj_is_str_or_bytes(args[ARG_ch].u_obj)) {
|
||||
GET_STR_DATA_LEN(args[ARG_ch].u_obj, str, str_len);
|
||||
if(str_len == 1) {
|
||||
ch = str[0];
|
||||
}
|
||||
} else {
|
||||
ch = mp_obj_get_int(args[ARG_ch].u_obj);
|
||||
}
|
||||
self->breakout->set_character(x, ch);
|
||||
|
||||
return mp_const_none;
|
||||
|
|
|
@ -9,12 +9,16 @@ enum allocator_mode {
|
|||
MICROPYTHON
|
||||
};
|
||||
|
||||
#ifndef CPP_FIXED_HEAP_SIZE
|
||||
#define CPP_FIXED_HEAP_SIZE 1024u
|
||||
#endif
|
||||
|
||||
static uint32_t alloc_bytes = 0;
|
||||
static uint32_t alloc_count = 0;
|
||||
static uint32_t free_count = 0;
|
||||
|
||||
static allocator_mode mode = FIXED_HEAP;
|
||||
static constexpr size_t cpp_heap_size = 10 * 1024 / 4;
|
||||
static constexpr size_t cpp_heap_size = CPP_FIXED_HEAP_SIZE / 4;
|
||||
static uint32_t cpp_heap[cpp_heap_size];
|
||||
static uint32_t ptr = 0;
|
||||
static char cpp_err_buf[128] = {0};
|
||||
|
|
|
@ -18,7 +18,6 @@ include(micropython-common-breakouts)
|
|||
include(pico_unicorn/micropython)
|
||||
include(pico_scroll/micropython)
|
||||
include(pico_rgb_keypad/micropython)
|
||||
include(pico_wireless/micropython)
|
||||
include(pico_explorer/micropython)
|
||||
|
||||
# LEDs & Matrices
|
||||
|
|
|
@ -8,6 +8,7 @@ set(CMAKE_C_STANDARD 11)
|
|||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
include(pico_wireless/micropython)
|
||||
|
||||
# C++ Magic Memory
|
||||
include(cppmem/micropython)
|
||||
|
|
|
@ -8,6 +8,7 @@ set(CMAKE_C_STANDARD 11)
|
|||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
include(pico_wireless/micropython)
|
||||
|
||||
enable_ulab()
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ set(CMAKE_C_STANDARD 11)
|
|||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
include(pico_wireless/micropython)
|
||||
|
||||
enable_ulab()
|
||||
|
||||
|
|
|
@ -42,6 +42,8 @@ STATIC const mp_rom_map_elem_t picoscroll_locals[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_show_bitmap_1d), MP_ROM_PTR(&picoscroll_show_bitmap_1d_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&picoscroll_clear_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picoscroll_is_pressed_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_WIDTH), MP_ROM_INT(17) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_HEIGHT), MP_ROM_INT(7) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(BUTTON_A) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(BUTTON_B) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_X), MP_ROM_INT(BUTTON_X) },
|
||||
|
|
|
@ -34,6 +34,12 @@ STATIC const mp_rom_map_elem_t picounicorn_locals_dict_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picounicorn_is_pressed_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_width), MP_ROM_PTR(&picounicorn_get_width_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_height), MP_ROM_PTR(&picounicorn_get_height_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_WIDTH), MP_ROM_INT(16) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_HEIGHT), MP_ROM_INT(7) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(BUTTON_A) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(BUTTON_B) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_X), MP_ROM_INT(BUTTON_X) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_Y), MP_ROM_INT(BUTTON_Y) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(picounicorn_locals_dict, picounicorn_locals_dict_table);
|
||||
|
@ -59,12 +65,12 @@ const mp_obj_type_t picounicorn_type = {
|
|||
STATIC const mp_rom_map_elem_t picounicorn_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_picounicorn) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_PicoUnicorn), (mp_obj_t)&picounicorn_type },
|
||||
{ MP_ROM_QSTR(MP_QSTR_WIDTH), MP_ROM_INT(16) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_HEIGHT), MP_ROM_INT(7) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(BUTTON_A) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(BUTTON_B) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_X), MP_ROM_INT(BUTTON_X) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_Y), MP_ROM_INT(BUTTON_Y) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_WIDTH), MP_ROM_INT(16) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_HEIGHT), MP_ROM_INT(7) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_picounicorn_globals, picounicorn_globals_table);
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue