Skip to content

Commit 2605df3

Browse files
committed
stmhal, pin: Save 140 bytes ROM by simplifying pin_print function.
1 parent 04019e3 commit 2605df3

1 file changed

Lines changed: 40 additions & 23 deletions

File tree

stmhal/pin.c

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,41 +189,58 @@ const pin_obj_t *pin_find(mp_obj_t user_obj) {
189189
STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
190190
pin_obj_t *self = self_in;
191191

192-
// Need to query mode, pull, af
192+
// pin name
193+
print(env, "Pin(Pin.cpu.%s, mode=Pin.", qstr_str(self->name));
193194

194-
print(env, "Pin(Pin.cpu.%s", qstr_str(self->name));
195195
uint32_t mode = pin_get_mode(self);
196+
196197
if (mode == GPIO_MODE_ANALOG) {
197-
print(env, ", mode=Pin.ANALOG)", qstr_str(self->name));
198+
// analog
199+
print(env, "ANALOG)");
200+
198201
} else {
199-
const char *pull_str = "";
200-
uint32_t pull = pin_get_pull(self);
201-
if (pull == GPIO_PULLUP) {
202-
pull_str = ", pull=Pin.PULL_UP";
203-
} else if (pull == GPIO_PULLDOWN) {
204-
pull_str = ", pull=Pin.PULL_DOWN";
205-
}
202+
// IO mode
203+
bool af = false;
204+
qstr mode_qst;
206205
if (mode == GPIO_MODE_INPUT) {
207-
print(env, ", mode=Pin.IN%s)", pull_str);
208-
} else if (mode == GPIO_MODE_OUTPUT_PP || mode == GPIO_MODE_OUTPUT_OD) {
209-
if (mode == GPIO_MODE_OUTPUT_PP) {
210-
print(env, ", mode=Pin.OUT_PP%s)", pull_str);
211-
} else {
212-
print(env, ", mode=Pin.OUT_OD%s)", pull_str);
213-
}
206+
mode_qst = MP_QSTR_IN;
207+
} else if (mode == GPIO_MODE_OUTPUT_PP) {
208+
mode_qst = MP_QSTR_OUT_PP;
209+
} else if (mode == GPIO_MODE_OUTPUT_OD) {
210+
mode_qst = MP_QSTR_OUT_OD;
214211
} else {
212+
af = true;
215213
if (mode == GPIO_MODE_AF_PP) {
216-
print(env, ", mode=Pin.AF_PP");
214+
mode_qst = MP_QSTR_AF_PP;
217215
} else {
218-
print(env, ", mode=Pin.AF_OD");
216+
mode_qst = MP_QSTR_AF_OD;
219217
}
218+
}
219+
print(env, qstr_str(mode_qst)); // safe because mode_qst has no formating chars
220+
221+
// pull mode
222+
qstr pull_qst = MP_QSTR_NULL;
223+
uint32_t pull = pin_get_pull(self);
224+
if (pull == GPIO_PULLUP) {
225+
pull_qst = MP_QSTR_PULL_UP;
226+
} else if (pull == GPIO_PULLDOWN) {
227+
pull_qst = MP_QSTR_PULL_DOWN;
228+
}
229+
if (pull_qst != MP_QSTR_NULL) {
230+
print(env, ", pull=Pin.%s", qstr_str(pull_qst));
231+
}
232+
233+
// AF mode
234+
if (af) {
220235
mp_uint_t af_idx = pin_get_af(self);
221-
const pin_af_obj_t *af = pin_find_af_by_index(self, af_idx);
222-
if (af == NULL) {
223-
print(env, ", af=%d%s)", af_idx, pull_str);
236+
const pin_af_obj_t *af_obj = pin_find_af_by_index(self, af_idx);
237+
if (af_obj == NULL) {
238+
print(env, ", af=%d)", af_idx);
224239
} else {
225-
print(env, ", af=Pin.%s)", qstr_str(af->name), pull_str);
240+
print(env, ", af=Pin.%s)", qstr_str(af_obj->name));
226241
}
242+
} else {
243+
print(env, ")");
227244
}
228245
}
229246
}

0 commit comments

Comments
 (0)