Skip to content

atmel-samd: Add modules directory with frozen bytecode support (like ESP8266 port), and NeoPixel python wrapper module. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ BOSSAC := tools/bossac_osx

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../lib/mp-readline
Expand Down Expand Up @@ -187,7 +196,7 @@ SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS))

SRC_AUTOGEN = \
$(BUILD)/_frozen_mpy.c \
$(BUILD)/frozen_mpy.c \

OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
Expand All @@ -199,9 +208,16 @@ SRC_QSTR += $(SRC_C) $(SRC_BINDINGS_EXPANDED) $(STM_SRC_C)

all: $(BUILD)/firmware.bin

$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h
$(ECHO) "MISC freezing bytecode"
$(Q)../tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $< > $@
# to build .mpy files from .py files
$(BUILD)/$(FROZEN_MPY_DIR)/%.mpy: $(FROZEN_MPY_DIR)/%.py
@$(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)
$(ECHO) "LINK $@"
Expand Down
Binary file removed atmel-samd/frozentest.mpy
Binary file not shown.
File renamed without changes.
31 changes: 31 additions & 0 deletions atmel-samd/modules/neopixel.py
Original file line number Diff line number Diff line change
@@ -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)