Merge pull request #10 from adafruit/frozen_modules

atmel-samd: Add modules directory with frozen bytecode support (like ESP8266 port), and NeoPixel python wrapper module.
This commit is contained in:
Scott Shawcroft 2016-10-14 10:16:16 -07:00 committed by GitHub
commit 0584ba1218
4 changed files with 51 additions and 4 deletions

View File

@ -27,6 +27,15 @@ BOSSAC := tools/bossac_osx
HAL_DIR=hal/$(MCU_SERIES) HAL_DIR=hal/$(MCU_SERIES)
# Frozen bytecode configuration. Any .py files placed in the modules subdirectory
# will be frozen into .mpy files and embedded in the firmware (like the ESP8266
# port).
MPY_CROSS = ../mpy-cross/mpy-cross
MPY_TOOL = ../tools/mpy-tool.py
FROZEN_MPY_DIR = modules
FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py')
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
INC += -I. INC += -I.
INC += -I.. INC += -I..
INC += -I../lib/mp-readline INC += -I../lib/mp-readline
@ -187,7 +196,7 @@ SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS)) $(addprefix common-hal/, $(SRC_BINDINGS))
SRC_AUTOGEN = \ SRC_AUTOGEN = \
$(BUILD)/_frozen_mpy.c \ $(BUILD)/frozen_mpy.c \
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
@ -199,9 +208,16 @@ SRC_QSTR += $(SRC_C) $(SRC_BINDINGS_EXPANDED) $(STM_SRC_C)
all: $(BUILD)/firmware.bin all: $(BUILD)/firmware.bin
$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h # to build .mpy files from .py files
$(ECHO) "MISC freezing bytecode" $(BUILD)/$(FROZEN_MPY_DIR)/%.mpy: $(FROZEN_MPY_DIR)/%.py
$(Q)../tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $< > $@ @$(ECHO) "MPY $<"
$(Q)$(MKDIR) -p $(dir $@)
$(Q)$(MPY_CROSS) -o $@ -s $(^:$(FROZEN_MPY_DIR)/%=%) $^
# to build frozen_mpy.c from all .mpy files
$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
@$(ECHO) "Creating $@"
$(Q)$(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $(FROZEN_MPY_MPY_FILES) > $@
$(BUILD)/firmware.elf: $(OBJ) $(BUILD)/firmware.elf: $(OBJ)
$(ECHO) "LINK $@" $(ECHO) "LINK $@"

Binary file not shown.

View File

@ -0,0 +1,31 @@
# NeoPixel driver for MicroPython on ESP8266
# MIT license; Copyright (c) 2016 Damien P. George
from neopixel_write import neopixel_write
class NeoPixel:
def __init__(self, pin, n):
self.pin = pin
self.n = n
self.buf = bytearray(n * 3)
self.pin.init(pin.OUT)
def __setitem__(self, index, val):
r, g, b = val
self.buf[index * 3] = g
self.buf[index * 3 + 1] = r
self.buf[index * 3 + 2] = b
def __getitem__(self, index):
i = index * 3
return self.buf[i + 1], self.buf[i], self.buf[i + 2]
def fill(self, color):
r, g, b = color
for i in range(len(self.buf) / 3):
self.buf[i * 3] = g
self.buf[i * 3 + 1] = r
self.buf[i * 3 + 2] = b
def write(self):
neopixel_write(self.pin, self.buf, True)