Skip to content

Commit 0c6935e

Browse files
authored
Merge branch 'main' into disable_network
2 parents 610e017 + 12edb57 commit 0c6935e

File tree

13 files changed

+172
-30
lines changed

13 files changed

+172
-30
lines changed

ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ INTERNAL_FLASH_FILESYSTEM = 1
1010
LONGINT_IMPL = NONE
1111

1212
CIRCUITPY_FULL_BUILD = 0
13-
# TODO: Turn off analogio for now for space reasons, but restore it
14-
# when frozen module gets smaller.
15-
CIRCUITPY_ANALOGIO = 0
1613
CIRCUITPY_AUDIOBUSIO = 0
1714
CIRCUITPY_AUDIOPWMIO = 0
1815
CIRCUITPY_AUDIOMP3 = 0
@@ -32,6 +29,7 @@ CIRCUITPY_USB_HID = 0
3229
CIRCUITPY_USB_MIDI = 0
3330
CIRCUITPY_VECTORIO = 0
3431

32+
CIRCUITPY_ANALOGIO = 1
3533
CIRCUITPY_AUDIOMIXER = 1
3634
CIRCUITPY_AUDIOIO = 1
3735
CIRCUITPY_DISPLAYIO = 1

ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ CIRCUITPY_PS2IO = 0
3030
CIRCUITPY_USB_HID = 0
3131
CIRCUITPY_USB_MIDI = 0
3232
CIRCUITPY_RTC = 0
33+
34+
# Enable board-specific modules
35+
USER_C_MODULES = boards/winterbloom_big_honking_button/usermods
36+
CFLAGS += -DMODULE_BHB_ENABLED=1
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "py/obj.h"
2+
#include "py/runtime.h"
3+
#include "shared-bindings/microcontroller/Pin.h"
4+
#include "samd/pins.h"
5+
#include "sam.h"
6+
7+
STATIC mp_obj_t _bhb_read_adc(void);
8+
9+
STATIC mp_obj_t _bhb_init_adc(void) {
10+
claim_pin(&pin_PB08);
11+
common_hal_never_reset_pin(&pin_PB08);
12+
13+
/* Enable the APB clock for the ADC. */
14+
PM->APBCMASK.reg |= PM_APBCMASK_ADC;
15+
16+
/* Enable GCLK0 for the ADC */
17+
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN |
18+
GCLK_CLKCTRL_GEN_GCLK0 |
19+
GCLK_CLKCTRL_ID_ADC;
20+
21+
/* Wait for bus synchronization. */
22+
while (GCLK->STATUS.bit.SYNCBUSY) {};
23+
24+
uint32_t bias = (*((uint32_t *) ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos;
25+
uint32_t linearity = (*((uint32_t *) ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos;
26+
linearity |= ((*((uint32_t *) ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5;
27+
28+
/* Wait for bus synchronization. */
29+
while (ADC->STATUS.bit.SYNCBUSY) {};
30+
31+
/* Write the calibration data. */
32+
ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(linearity);
33+
34+
/* Use the internal VCC reference. This is 1/2 of what's on VCCA.
35+
since VCCA is 3.3v, this is 1.65v.
36+
*/
37+
ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INTVCC1;
38+
39+
/* Capture 64 samples. */
40+
ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_64 | ADC_AVGCTRL_ADJRES(4);
41+
42+
/* Set the clock prescaler to 32, which is the same as the CircuitPython default.
43+
Set the resolution to 16 for averaging
44+
*/
45+
ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV32 |
46+
ADC_CTRLB_RESSEL_16BIT;
47+
48+
/* Configure the input parameters.
49+
50+
- GAIN_DIV2 means that the input voltage is halved. This is important
51+
because the voltage reference is 1/2 of VCCA. So if you want to
52+
measure 0-3.3v, you need to halve the input as well.
53+
54+
- MUXNEG_GND means that the ADC should compare the input value to GND.
55+
56+
- MUXPOS_PIN3 means that the ADC should read from AIN2, or PB08.
57+
*/
58+
ADC->INPUTCTRL.reg = ADC_INPUTCTRL_GAIN_DIV2 |
59+
ADC_INPUTCTRL_MUXNEG_GND |
60+
ADC_INPUTCTRL_MUXPOS_PIN2;
61+
62+
63+
/* Set PB08 as an input pin. */
64+
PORT->Group[1].DIRCLR.reg = PORT_PB08;
65+
66+
/* Enable the peripheral multiplexer for PB08. */
67+
PORT->Group[1].PINCFG[8].reg |= PORT_PINCFG_PMUXEN;
68+
69+
/* Set PB08 to function B which is analog input. */
70+
PORT->Group[1].PMUX[4].reg |= PORT_PMUX_PMUXE_B;
71+
72+
/* Wait for bus synchronization. */
73+
while (ADC->STATUS.bit.SYNCBUSY) {};
74+
75+
/* Enable the ADC. */
76+
ADC->CTRLA.bit.ENABLE = true;
77+
78+
/* Make one read and throw it away, as per the datasheet. */
79+
_bhb_read_adc();
80+
81+
return mp_const_none;
82+
}
83+
84+
STATIC mp_obj_t _bhb_read_adc(void) {
85+
/* Wait for bus synchronization. */
86+
while (ADC->STATUS.bit.SYNCBUSY) {};
87+
88+
/* Start the ADC using a software trigger. */
89+
ADC->SWTRIG.bit.START = true;
90+
91+
/* Wait for the result ready flag to be set. */
92+
while (ADC->INTFLAG.bit.RESRDY == 0);
93+
94+
/* Clear the flag. */
95+
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
96+
97+
/* Read the value. */
98+
uint32_t result = ADC->RESULT.reg;
99+
100+
return MP_OBJ_NEW_SMALL_INT(result);
101+
}
102+
103+
104+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(_bhb_init_adc_obj, _bhb_init_adc);
105+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(_bhb_read_adc_obj, _bhb_read_adc);
106+
107+
STATIC const mp_rom_map_elem_t _bhb_module_globals_table[] = {
108+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bhb) },
109+
{ MP_ROM_QSTR(MP_QSTR_init_adc), MP_ROM_PTR(&_bhb_init_adc_obj) },
110+
{ MP_ROM_QSTR(MP_QSTR_read_adc), MP_ROM_PTR(&_bhb_read_adc_obj) },
111+
};
112+
113+
STATIC MP_DEFINE_CONST_DICT(_bhb_module_globals, _bhb_module_globals_table);
114+
115+
const mp_obj_module_t _bhb_user_cmodule = {
116+
.base = { &mp_type_module },
117+
.globals = (mp_obj_dict_t*)&_bhb_module_globals,
118+
};
119+
120+
MP_REGISTER_MODULE(MP_QSTR__bhb, _bhb_user_cmodule, MODULE_BHB_ENABLED);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
USERMODULES_DIR := $(USERMOD_DIR)
2+
3+
# Add all C files to SRC_USERMOD.
4+
SRC_USERMOD += $(USERMODULES_DIR)/bhb.c
5+
6+
CFLAGS_USERMOD += -I$(USERMODULES_DIR)

shared-bindings/vectorio/Polygon.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,7 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
4949
//|
5050
STATIC mp_obj_t vectorio_polygon_obj_get_points(mp_obj_t self_in) {
5151
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
52-
mp_obj_t list = mp_obj_new_list(0, NULL);
53-
54-
size_t len = 0;
55-
mp_obj_t *items;
56-
mp_obj_list_get(common_hal_vectorio_polygon_get_points(self), &len, &items);
57-
58-
for (size_t i = 0; i < len; i += 2) {
59-
mp_obj_t tuple[] = { items[i], items[i+1] };
60-
mp_obj_list_append(
61-
list,
62-
mp_obj_new_tuple(2, tuple)
63-
);
64-
}
65-
return list;
52+
return common_hal_vectorio_polygon_get_points(self);
6653
}
6754
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_points_obj, vectorio_polygon_obj_get_points);
6855

shared-module/displayio/__init__.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ void reset_displays(void) {
190190
common_hal_displayio_epaperdisplay_show(display, NULL);
191191
#if CIRCUITPY_FRAMEBUFFERIO
192192
} else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
193-
framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
194-
display->auto_refresh = true;
195-
common_hal_framebufferio_framebufferdisplay_show(display, NULL);
193+
framebufferio_framebufferdisplay_reset(&displays[i].framebuffer_display);
196194
#endif
197195
}
198196
}

shared-module/framebufferio/FramebufferDisplay.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,8 @@ void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisp
318318
gc_collect_ptr(self->framebuffer);
319319
displayio_display_core_collect_ptrs(&self->core);
320320
}
321+
322+
void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t* self) {
323+
common_hal_framebufferio_framebufferdisplay_set_auto_refresh(self, true);
324+
common_hal_framebufferio_framebufferdisplay_show(self, NULL);
325+
}

shared-module/framebufferio/FramebufferDisplay.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct {
5555
void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self);
5656
void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self);
5757
void reset_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self);
58+
void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t* self);
5859

5960
void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t* self);
6061

shared-module/vectorio/Polygon.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
2020
size_t len = 0;
2121
mp_obj_t *items;
2222
mp_obj_list_get(points_tuple_list, &len, &items);
23-
VECTORIO_POLYGON_DEBUG("polygon_points_list len: %d\n", len);
23+
VECTORIO_POLYGON_DEBUG(" self.len: %d, len: %d, ", self->len, len);
2424

2525
if ( len < 3 ) {
2626
mp_raise_TypeError_varg(translate("Polygon needs at least 3 points"));
2727
}
2828

2929
if ( self->len < 2*len ) {
3030
if ( self->points_list != NULL ) {
31+
VECTORIO_POLYGON_DEBUG("free(%d), ", sizeof(self->points_list));
3132
gc_free( self->points_list );
3233
}
3334
self->points_list = gc_alloc( 2 * len * sizeof(int), false, false );
35+
VECTORIO_POLYGON_DEBUG("alloc(%p, %d)", self->points_list, 2 * len * sizeof(int));
3436
}
3537
self->len = 2*len;
3638

@@ -56,22 +58,35 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
5658

5759

5860
void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) {
59-
VECTORIO_POLYGON_DEBUG("%p polygon_construct\n", self);
61+
VECTORIO_POLYGON_DEBUG("%p polygon_construct: ", self);
6062
self->points_list = NULL;
6163
self->len = 0;
6264
self->on_dirty.obj = NULL;
6365
_clobber_points_list( self, points_list );
66+
VECTORIO_POLYGON_DEBUG("\n");
6467
}
6568

6669

6770
mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) {
68-
return self->points_list;
71+
VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_get_points {len: %d, points_list: %p}\n", self, self->len, self->points_list);
72+
mp_obj_t list = mp_obj_new_list(self->len/2, NULL);
73+
74+
for (size_t i = 0; i < self->len; i += 2) {
75+
mp_obj_t tuple[] = { mp_obj_new_int(self->points_list[i]), mp_obj_new_int(self->points_list[i+1]) };
76+
mp_obj_list_append(
77+
list,
78+
mp_obj_new_tuple(2, tuple)
79+
);
80+
}
81+
return list;
6982
}
7083
void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list) {
84+
VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_set_points: ", self);
7185
_clobber_points_list( self, points_list );
7286
if (self->on_dirty.obj != NULL) {
7387
self->on_dirty.event(self->on_dirty.obj);
7488
}
89+
VECTORIO_POLYGON_DEBUG("\n");
7590
}
7691

7792
void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification) {

0 commit comments

Comments
 (0)