Skip to content

Commit dd1c957

Browse files
committed
mtm_computer: make board.DAC() an actual singleton
1 parent ad21609 commit dd1c957

File tree

1 file changed

+15
-9
lines changed
  • ports/raspberrypi/boards/mtm_computer

1 file changed

+15
-9
lines changed

ports/raspberrypi/boards/mtm_computer/board.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@
1212

1313
// board.DAC() — factory function that constructs an mcp4822.MCP4822 with
1414
// the MTM Workshop Computer's DAC pins (GP18=SCK, GP19=SDI, GP21=CS).
15+
16+
static mp_obj_t board_dac_singleton = MP_OBJ_NULL;
17+
1518
static mp_obj_t board_dac_factory(void) {
16-
mcp4822_mcp4822_obj_t *dac = mp_obj_malloc_with_finaliser(
17-
mcp4822_mcp4822_obj_t, &mcp4822_mcp4822_type);
18-
common_hal_mcp4822_mcp4822_construct(
19-
dac,
20-
&pin_GPIO18, // clock (SCK)
21-
&pin_GPIO19, // mosi (SDI)
22-
&pin_GPIO21, // cs
23-
1); // gain 1x
24-
return MP_OBJ_FROM_PTR(dac);
19+
if (board_dac_singleton == MP_OBJ_NULL) {
20+
mcp4822_mcp4822_obj_t *dac = mp_obj_malloc_with_finaliser(
21+
mcp4822_mcp4822_obj_t, &mcp4822_mcp4822_type);
22+
common_hal_mcp4822_mcp4822_construct(
23+
dac,
24+
&pin_GPIO18, // clock (SCK)
25+
&pin_GPIO19, // mosi (SDI)
26+
&pin_GPIO21, // cs
27+
1); // gain 1x
28+
board_dac_singleton = MP_OBJ_FROM_PTR(dac);
29+
}
30+
return board_dac_singleton;
2531
}
2632
MP_DEFINE_CONST_FUN_OBJ_0(board_dac_obj, board_dac_factory);
2733

0 commit comments

Comments
 (0)