Skip to content

Commit c9bee93

Browse files
authored
Merge pull request #733 from pimoroni/patch-cpp-init-heap
Refactor out remaining C++ heap usage during init.
2 parents 6494158 + 540bc2f commit c9bee93

File tree

13 files changed

+65
-59
lines changed

13 files changed

+65
-59
lines changed

drivers/ltp305/dotfont.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ namespace pimoroni {
88
static const uint8_t DOT_CHAR_WIDTH = 5;
99

1010
struct DotChar {
11+
uint16_t code;
1112
uint8_t data[DOT_CHAR_WIDTH];
1213
};
1314

14-
static const std::map<uint16_t, DotChar> dotfont = {
15+
static const DotChar dotfont[] = {
1516
{32, {0x00, 0x00, 0x00, 0x00, 0x00}}, // (space)
1617
{33, {0x00, 0x00, 0x5f, 0x00, 0x00}}, // !
1718
{34, {0x00, 0x07, 0x00, 0x07, 0x00}}, // "

drivers/ltp305/ltp305.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ namespace pimoroni {
6464
}
6565

6666
void LTP305::set_character(uint8_t x, uint16_t ch) {
67-
std::map<uint16_t, DotChar>::const_iterator it = dotfont.find(ch);
67+
uint8_t *data = nullptr;
68+
for(auto c : dotfont) {
69+
if(c.code == ch) {
70+
data = &c.data[0];
71+
break;
72+
}
73+
}
6874

69-
if(it != dotfont.end()) {
70-
DotChar dchar = it->second;
75+
if(data) {
7176
for(uint8_t cx = 0; cx < DOT_CHAR_WIDTH; cx++) {
7277
for(uint8_t cy = 0; cy < HEIGHT; cy++) {
73-
uint8_t c = dchar.data[cx] & (0b1 << cy);
78+
uint8_t c = data[cx] & (0b1 << cy);
7479
set_pixel(x + cx, cy, c);
7580
}
7681
}

drivers/ltr559/ltr559.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22
#include <algorithm>
33

44
namespace pimoroni {
5-
lookup::lookup(std::initializer_list<uint16_t> values) : lut(values) {
6-
}
7-
8-
uint8_t lookup::index(uint16_t value) {
9-
auto it = find(lut.begin(), lut.end(), value);
10-
11-
if(it == lut.end())
12-
return 0;
13-
14-
return it - lut.begin();
15-
}
16-
17-
uint16_t lookup::value(uint8_t index) {
18-
return lut[index];
19-
}
20-
21-
pimoroni::lookup LTR559::lookup_led_current({5, 10, 20, 50, 100});
22-
pimoroni::lookup LTR559::lookup_led_duty_cycle({25, 50, 75, 100});
23-
pimoroni::lookup LTR559::lookup_led_pulse_freq({30, 40, 50, 60, 70, 80, 90, 100});
24-
pimoroni::lookup LTR559::lookup_proximity_meas_rate({10, 50, 70, 100, 200, 500, 1000, 2000});
25-
pimoroni::lookup LTR559::lookup_light_integration_time({100, 50, 200, 400, 150, 250, 300, 350});
26-
pimoroni::lookup LTR559::lookup_light_repeat_rate({50, 100, 200, 500, 1000, 2000});
27-
pimoroni::lookup LTR559::lookup_light_gain({1, 2, 4, 8, 0, 0, 48, 96});
28-
295
bool LTR559::init() {
306
if(interrupt != PIN_UNUSED) {
317
gpio_set_function(interrupt, GPIO_FUNC_SIO);
@@ -129,7 +105,7 @@ namespace pimoroni {
129105
i2c->read_bytes(address, LTR559_ALS_DATA_CH1, (uint8_t *)&als, 4);
130106
data.als0 = als[1];
131107
data.als1 = als[0];
132-
data.gain = this->lookup_light_gain.value((status >> LTR559_ALS_PS_STATUS_ALS_GAIN_SHIFT) & LTR559_ALS_PS_STATUS_ALS_GAIN_MASK);
108+
data.gain = lookup_light_gain[(status >> LTR559_ALS_PS_STATUS_ALS_GAIN_SHIFT) & LTR559_ALS_PS_STATUS_ALS_GAIN_MASK];
133109

134110
data.ratio = 101.0f;
135111

@@ -163,12 +139,12 @@ namespace pimoroni {
163139
}
164140

165141
void LTR559::proximity_led(uint8_t current, uint8_t duty_cycle, uint8_t pulse_freq, uint8_t num_pulses) {
166-
current = lookup_led_current.index(current);
142+
current = lookup<lookup_led_current>(current);
167143

168-
duty_cycle = lookup_led_duty_cycle.index(duty_cycle);
144+
duty_cycle = lookup<lookup_led_duty_cycle>(duty_cycle);
169145
duty_cycle <<= LTR559_PS_LED_DUTY_CYCLE_SHIFT;
170146

171-
pulse_freq = lookup_led_pulse_freq.index(pulse_freq);
147+
pulse_freq = lookup<lookup_led_pulse_freq>(pulse_freq);
172148
pulse_freq <<= LTR559_PS_LED_PULSE_FREQ_SHIFT;
173149

174150
uint8_t buf = current | duty_cycle | pulse_freq;
@@ -180,7 +156,7 @@ namespace pimoroni {
180156

181157
void LTR559::light_control(bool active, uint8_t gain) {
182158
uint8_t buf = 0;
183-
gain = lookup_light_gain.index(gain);
159+
gain = lookup<lookup_light_gain>(gain);
184160
buf |= gain << LTR559_ALS_CONTROL_GAIN_SHIFT;
185161

186162
if(active)
@@ -223,16 +199,16 @@ namespace pimoroni {
223199

224200
void LTR559::light_measurement_rate(uint16_t integration_time, uint16_t rate) {
225201
data.integration_time = integration_time;
226-
integration_time = lookup_light_integration_time.index(integration_time);
227-
rate = lookup_light_repeat_rate.index(rate);
202+
integration_time = lookup<lookup_light_integration_time>(integration_time);
203+
rate = lookup<lookup_light_repeat_rate>(rate);
228204
uint8_t buf = 0;
229205
buf |= rate;
230206
buf |= integration_time << LTR559_ALS_MEAS_RATE_INTEGRATION_TIME_SHIFT;
231207
i2c->write_bytes(address, LTR559_ALS_MEAS_RATE, &buf, 1);
232208
}
233209

234210
void LTR559::proximity_measurement_rate(uint16_t rate) {
235-
uint8_t buf = lookup_proximity_meas_rate.index(rate);
211+
uint8_t buf = lookup<lookup_proximity_meas_rate>(rate);
236212
i2c->write_bytes(address, LTR559_PS_MEAS_RATE, &buf, 1);
237213
}
238214

drivers/ltr559/ltr559.hpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,6 @@ namespace pimoroni {
9797
float lux;
9898
} ltr559_reading;
9999

100-
class lookup {
101-
private:
102-
std::vector<uint16_t> lut;
103-
public:
104-
lookup(std::initializer_list<uint16_t> values);
105-
uint8_t index(uint16_t value);
106-
uint16_t value(uint8_t index);
107-
};
108-
109100
class LTR559 {
110101
//--------------------------------------------------
111102
// Constants
@@ -131,14 +122,13 @@ namespace pimoroni {
131122
const uint8_t address = DEFAULT_I2C_ADDRESS;
132123
uint interrupt = PIN_UNUSED;
133124

134-
static pimoroni::lookup lookup_led_current;
135-
static pimoroni::lookup lookup_led_duty_cycle;
136-
static pimoroni::lookup lookup_led_pulse_freq;
137-
static pimoroni::lookup lookup_proximity_meas_rate;
138-
static pimoroni::lookup lookup_light_integration_time;
139-
static pimoroni::lookup lookup_light_repeat_rate;
140-
static pimoroni::lookup lookup_light_gain;
141-
125+
static constexpr uint16_t lookup_led_current[5] = {5, 10, 20, 50, 100};
126+
static constexpr uint16_t lookup_led_duty_cycle[4] = {25, 50, 75, 100};
127+
static constexpr uint16_t lookup_led_pulse_freq[8] = {30, 40, 50, 60, 70, 80, 90, 100};
128+
static constexpr uint16_t lookup_proximity_meas_rate[8] = {10, 50, 70, 100, 200, 500, 1000, 2000};
129+
static constexpr uint16_t lookup_light_integration_time[8] = {100, 50, 200, 400, 150, 250, 300, 350};
130+
static constexpr uint16_t lookup_light_repeat_rate[6] = {50, 100, 200, 500, 1000, 2000};
131+
static constexpr uint16_t lookup_light_gain[8] = {1, 2, 4, 8, 0, 0, 48, 96};
142132

143133
//--------------------------------------------------
144134
// Constructors/Destructor
@@ -177,6 +167,15 @@ namespace pimoroni {
177167
void proximity_measurement_rate(uint16_t rate);
178168
void proximity_offset(uint16_t offset);
179169

170+
template<auto T>
171+
const uint16_t lookup(uint16_t value) {
172+
size_t length = sizeof(T) / sizeof(uint16_t);
173+
for(auto i = 0u; i < length; i++) {
174+
if(T[i] == value) return i;
175+
}
176+
return 0;
177+
}
178+
180179
private:
181180
uint16_t bit12_to_uint16(uint16_t value);
182181
uint16_t uint16_to_bit12(uint16_t value);

libraries/hershey_fonts/hershey_fonts.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cmath>
44

55
namespace hershey {
6+
#ifndef MICROPY_BUILD_TYPE
67
std::map<std::string, const font_t*> fonts = {
78
{ "sans", &futural },
89
//{ "sans_bold", &futuram },
@@ -13,6 +14,7 @@ namespace hershey {
1314
{ "serif", &timesr },
1415
//{ "serif_bold", &timesrb }
1516
};
17+
#endif
1618

1719
bool has_font(std::string_view font) {
1820
if(font == "sans"

micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,16 @@ mp_obj_t BreakoutDotMatrix_set_character(size_t n_args, const mp_obj_t *pos_args
124124
breakout_dotmatrix_BreakoutDotMatrix_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_dotmatrix_BreakoutDotMatrix_obj_t);
125125

126126
int x = args[ARG_x].u_int;
127-
int ch = mp_obj_get_int(args[ARG_ch].u_obj);
127+
int ch = 0;
128+
129+
if(mp_obj_is_str_or_bytes(args[ARG_ch].u_obj)) {
130+
GET_STR_DATA_LEN(args[ARG_ch].u_obj, str, str_len);
131+
if(str_len == 1) {
132+
ch = str[0];
133+
}
134+
} else {
135+
ch = mp_obj_get_int(args[ARG_ch].u_obj);
136+
}
128137
self->breakout->set_character(x, ch);
129138

130139
return mp_const_none;

micropython/modules/cppmem/cppmem.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ enum allocator_mode {
99
MICROPYTHON
1010
};
1111

12+
#ifndef CPP_FIXED_HEAP_SIZE
13+
#define CPP_FIXED_HEAP_SIZE 1024u
14+
#endif
15+
1216
static uint32_t alloc_bytes = 0;
1317
static uint32_t alloc_count = 0;
1418
static uint32_t free_count = 0;
1519

1620
static allocator_mode mode = FIXED_HEAP;
17-
static constexpr size_t cpp_heap_size = 10 * 1024 / 4;
21+
static constexpr size_t cpp_heap_size = CPP_FIXED_HEAP_SIZE / 4;
1822
static uint32_t cpp_heap[cpp_heap_size];
1923
static uint32_t ptr = 0;
2024
static char cpp_err_buf[128] = {0};

micropython/modules/micropython-common.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ include(micropython-common-breakouts)
1818
include(pico_unicorn/micropython)
1919
include(pico_scroll/micropython)
2020
include(pico_rgb_keypad/micropython)
21-
include(pico_wireless/micropython)
2221
include(pico_explorer/micropython)
2322

2423
# LEDs & Matrices

micropython/modules/micropython-pico.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(CMAKE_C_STANDARD 11)
88
set(CMAKE_CXX_STANDARD 17)
99

1010
include(micropython-common)
11+
include(pico_wireless/micropython)
1112

1213
# C++ Magic Memory
1314
include(cppmem/micropython)

micropython/modules/micropython-picolipo_16mb.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(CMAKE_C_STANDARD 11)
88
set(CMAKE_CXX_STANDARD 17)
99

1010
include(micropython-common)
11+
include(pico_wireless/micropython)
1112

1213
enable_ulab()
1314

0 commit comments

Comments
 (0)