This repository was archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Creating the skeleton of the microbit library and adding a button object #185
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
806576b
shim design example
andreamah deceeb7
some effort on image object design
andreamah 8ed9e5b
initial look at image class
andreamah 32060ce
fixes to image
andreamah ef6498c
finished first draft of most image methods
andreamah d2a7672
width and height modifications
andreamah 16beee3
more additions to image
andreamah 52ba07e
display microbit library
91352a0
resolve merge conflicts
cc0ac13
update dusplay
c761e8b
integrated image into display class
andreamah 4cbda2b
fixed LED array reference issue
andreamah 3557047
changes to display
0717e6b
fixed merge conflicts
ecd96e0
added bytearray compatability
andreamah 9652a23
Before merge
ddcf389
reserve merge conflicts
20690b2
display show working, still need to add tests
1f4d0dd
first commit
vandyliu c4a1ca2
updated folder hierarchy
vandyliu d66ce53
resolve merge conflicts
vandyliu 73a826c
formatted with black
vandyliu dfe868b
added comments
vandyliu 154bc58
Updated functions that are actually in microbit lib
c7d39ce
fixed running_time bug
72e0f2f
Added unit tests
2484fda
reverted settings.json
b9b5e72
updated gulp file
113024d
addressed comments
55e011a
Formatted with black
da46cc8
Addressed comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,135 +1,138 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| const gulp = require("gulp"); | ||
|
|
||
| const ts = require("gulp-typescript"); | ||
| const sourcemaps = require("gulp-sourcemaps"); | ||
| const typescript = require("typescript"); | ||
| const del = require("del"); | ||
| const es = require("event-stream"); | ||
| const vsce = require("vsce"); | ||
| const nls = require("vscode-nls-dev"); | ||
|
|
||
| const tsProject = ts.createProject("./tsconfig.json", { typescript }); | ||
|
|
||
| const inlineMap = true; | ||
| const inlineSource = false; | ||
| const outDest = "out"; | ||
|
|
||
| // A list of all locales supported by VSCode can be found here: https://code.visualstudio.com/docs/getstarted/locales | ||
| const languages = [{ folderName: "en", id: "en" }]; | ||
|
|
||
| gulp.task("clean", () => { | ||
| return del( | ||
| [ | ||
| "out/*", | ||
| "package.nls.*.json", | ||
| "../../dist/*0.0.0-UNTRACKEDVERSION.vsix" | ||
| ], | ||
| { force: true } | ||
| ); | ||
| }); | ||
|
|
||
| const pythonToMove = [ | ||
| "./src/adafruit_circuitplayground/*.*", | ||
| "./src/*.py", | ||
| "./src/requirements.txt", | ||
| ]; | ||
|
|
||
| gulp.task("python-compile", () => { | ||
| // the base option sets the relative root for the set of files, | ||
| // preserving the folder structure | ||
| return gulp.src(pythonToMove, { base: "./src/" }).pipe(gulp.dest("out")); | ||
| }); | ||
|
|
||
| gulp.task("internal-compile", () => { | ||
| return compile(false); | ||
| }); | ||
|
|
||
| gulp.task("internal-nls-compile", () => { | ||
| return compile(true); | ||
| }); | ||
|
|
||
| gulp.task("add-locales", () => { | ||
| return gulp | ||
| .src(["package.nls.json"]) | ||
| .pipe(nls.createAdditionalLanguageFiles(languages, "locales")) | ||
| .pipe(gulp.dest(".")); | ||
| }); | ||
|
|
||
| gulp.task("vsce:publish", () => { | ||
| return vsce.publish(); | ||
| }); | ||
|
|
||
| gulp.task("vsce:package", () => { | ||
| return vsce.createVSIX({ | ||
| packagePath: "../../dist/deviceSimulatorExpress-0.0.0-UNTRACKEDVERSION.vsix" | ||
| }); | ||
| }); | ||
|
|
||
| gulp.task( | ||
| "compile", | ||
| gulp.series("clean", "internal-compile", "python-compile", callback => { | ||
| callback(); | ||
| }) | ||
| ); | ||
|
|
||
| gulp.task( | ||
| "build", | ||
| gulp.series( | ||
| "clean", | ||
| "internal-nls-compile", | ||
| "python-compile", | ||
| "add-locales", | ||
| callback => { | ||
| callback(); | ||
| } | ||
| ) | ||
| ); | ||
|
|
||
| gulp.task( | ||
| "publish", | ||
| gulp.series("compile", "vsce:publish", callback => { | ||
| callback(); | ||
| }) | ||
| ); | ||
|
|
||
| gulp.task( | ||
| "package", | ||
| gulp.series("compile", "vsce:package", callback => { | ||
| callback(); | ||
| }) | ||
| ); | ||
|
|
||
| //---- internal | ||
|
|
||
| function compile(buildNls) { | ||
| var r = tsProject | ||
| .src() | ||
| .pipe(sourcemaps.init()) | ||
| .pipe(tsProject()) | ||
| .js.pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through()) | ||
| .pipe( | ||
| buildNls | ||
| ? nls.createAdditionalLanguageFiles(languages, "locales", "out") | ||
| : es.through() | ||
| ); | ||
|
|
||
| if (inlineMap && inlineSource) { | ||
| r = r.pipe(sourcemaps.write()); | ||
| } else { | ||
| r = r.pipe( | ||
| sourcemaps.write("../out", { | ||
| // no inlined source | ||
| includeContent: inlineSource, | ||
| // Return relative source map root directories per file. | ||
| sourceRoot: "../src" | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| return r.pipe(gulp.dest(outDest)); | ||
| } | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| const gulp = require("gulp"); | ||
|
|
||
| const ts = require("gulp-typescript"); | ||
| const sourcemaps = require("gulp-sourcemaps"); | ||
| const typescript = require("typescript"); | ||
| const del = require("del"); | ||
| const es = require("event-stream"); | ||
| const vsce = require("vsce"); | ||
| const nls = require("vscode-nls-dev"); | ||
|
|
||
| const tsProject = ts.createProject("./tsconfig.json", { typescript }); | ||
|
|
||
| const inlineMap = true; | ||
| const inlineSource = false; | ||
| const outDest = "out"; | ||
|
|
||
| // A list of all locales supported by VSCode can be found here: https://code.visualstudio.com/docs/getstarted/locales | ||
| const languages = [{ folderName: "en", id: "en" }]; | ||
|
|
||
| gulp.task("clean", () => { | ||
| return del( | ||
| [ | ||
| "out/*", | ||
| "package.nls.*.json", | ||
| "../../dist/*0.0.0-UNTRACKEDVERSION.vsix", | ||
| ], | ||
| { force: true } | ||
| ); | ||
| }); | ||
|
|
||
| const pythonToMove = [ | ||
| "./src/adafruit_circuitplayground/*.*", | ||
| "./src/microbit/*.*", | ||
| "./src/microbit/!(test)/**/*", | ||
| "./src/*.py", | ||
| "./src/requirements.txt", | ||
| ]; | ||
|
|
||
| gulp.task("python-compile", () => { | ||
| // the base option sets the relative root for the set of files, | ||
| // preserving the folder structure | ||
| return gulp.src(pythonToMove, { base: "./src/" }).pipe(gulp.dest("out")); | ||
| }); | ||
|
|
||
| gulp.task("internal-compile", () => { | ||
| return compile(false); | ||
| }); | ||
|
|
||
| gulp.task("internal-nls-compile", () => { | ||
| return compile(true); | ||
| }); | ||
|
|
||
| gulp.task("add-locales", () => { | ||
| return gulp | ||
| .src(["package.nls.json"]) | ||
| .pipe(nls.createAdditionalLanguageFiles(languages, "locales")) | ||
| .pipe(gulp.dest(".")); | ||
| }); | ||
|
|
||
| gulp.task("vsce:publish", () => { | ||
| return vsce.publish(); | ||
| }); | ||
|
|
||
| gulp.task("vsce:package", () => { | ||
| return vsce.createVSIX({ | ||
| packagePath: | ||
| "../../dist/deviceSimulatorExpress-0.0.0-UNTRACKEDVERSION.vsix", | ||
| }); | ||
| }); | ||
|
|
||
| gulp.task( | ||
| "compile", | ||
| gulp.series("clean", "internal-compile", "python-compile", callback => { | ||
| callback(); | ||
| }) | ||
| ); | ||
|
|
||
| gulp.task( | ||
| "build", | ||
| gulp.series( | ||
| "clean", | ||
| "internal-nls-compile", | ||
| "python-compile", | ||
| "add-locales", | ||
| callback => { | ||
| callback(); | ||
| } | ||
| ) | ||
| ); | ||
|
|
||
| gulp.task( | ||
| "publish", | ||
| gulp.series("compile", "vsce:publish", callback => { | ||
| callback(); | ||
| }) | ||
| ); | ||
|
|
||
| gulp.task( | ||
| "package", | ||
| gulp.series("compile", "vsce:package", callback => { | ||
| callback(); | ||
| }) | ||
| ); | ||
|
|
||
| //---- internal | ||
|
|
||
| function compile(buildNls) { | ||
| var r = tsProject | ||
| .src() | ||
| .pipe(sourcemaps.init()) | ||
| .pipe(tsProject()) | ||
| .js.pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through()) | ||
| .pipe( | ||
| buildNls | ||
| ? nls.createAdditionalLanguageFiles(languages, "locales", "out") | ||
| : es.through() | ||
| ); | ||
|
|
||
| if (inlineMap && inlineSource) { | ||
| r = r.pipe(sourcemaps.write()); | ||
| } else { | ||
| r = r.pipe( | ||
| sourcemaps.write("../out", { | ||
| // no inlined source | ||
| includeContent: inlineSource, | ||
| // Return relative source map root directories per file. | ||
| sourceRoot: "../src", | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| return r.pipe(gulp.dest(outDest)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .shim import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| class Button: | ||
| # The implementation is based off of https://github.com/bbcmicrobit/micropython/blob/master/docs/button.rst. | ||
| def __init__(self): | ||
| self.__pressed = False | ||
| self.__presses = 0 | ||
| self.__prev_pressed = False | ||
|
|
||
| def is_pressed(self): | ||
| return self.__pressed | ||
|
|
||
| def was_pressed(self): | ||
| res = self.__prev_pressed | ||
| self.__prev_pressed = False | ||
| return res | ||
|
|
||
| def get_presses(self): | ||
| res = self.__presses | ||
| self.__presses = 0 | ||
| return res | ||
|
|
||
| def __press_down(self): | ||
nasadigital marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.__pressed = True | ||
| self.__prev_pressed = True | ||
| self.__presses += 1 | ||
|
|
||
| def __release(self): | ||
| self.__pressed = False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import time | ||
|
|
||
| from .button import Button | ||
|
|
||
|
|
||
| class MicrobitModel: | ||
| def __init__(self): | ||
| # State in the Python process | ||
| self.button_a = Button() | ||
| self.button_b = Button() | ||
| self.__start_time = time.time() | ||
|
|
||
| def sleep(self, n): | ||
| time.sleep(n / 1000) | ||
|
|
||
| def running_time(self): | ||
| print(f"time. time: {time.time()}") | ||
| return time.time() - self.__start_time | ||
|
|
||
|
|
||
| mb = MicrobitModel() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from .model import microbit_model | ||
|
|
||
|
|
||
| def sleep(n): | ||
| microbit_model.mb.sleep(n) | ||
|
|
||
|
|
||
| def running_time(): | ||
| microbit_model.mb.running_time() | ||
nasadigital marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import pytest | ||
| from ..model.button import Button | ||
|
|
||
|
|
||
| class TestButton(object): | ||
| def setup_method(self): | ||
| self.button = Button() | ||
|
|
||
| def test_press_down(self): | ||
| self.button._Button__press_down() | ||
| assert self.button._Button__presses == 1 | ||
| assert self.button._Button__pressed | ||
| assert self.button._Button__prev_pressed | ||
| self.button._Button__press_down() | ||
| assert self.button._Button__presses == 2 | ||
| assert self.button._Button__pressed | ||
| assert self.button._Button__prev_pressed | ||
|
|
||
| def test_release(self): | ||
| self.button._Button__pressed = True | ||
| self.button._Button__prev_pressed = False | ||
| self.button._Button__release() | ||
| assert not self.button._Button__pressed | ||
|
|
||
| def test_is_pressed(self): | ||
| assert not self.button.is_pressed() | ||
| self.button._Button__press_down() | ||
| assert self.button.is_pressed() | ||
|
|
||
| def test_was_pressed(self): | ||
| assert not self.button.was_pressed() | ||
| self.button._Button__press_down() | ||
| self.button._Button__release() | ||
| assert self.button.was_pressed() | ||
| # Button resets __prev_pressed after was_pressed() is called. | ||
| assert not self.button.was_pressed() | ||
|
|
||
| @pytest.mark.parametrize("presses", [1, 2, 4]) | ||
| def test_get_presses(self, presses): | ||
| assert 0 == self.button.get_presses() | ||
| for i in range(presses): | ||
| self.button._Button__press_down() | ||
| self.button._Button__release() | ||
| assert presses == self.button.get_presses() | ||
| # Presses is reset to 0 after get_presses() is called. | ||
| assert 0 == self.button.get_presses() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.