Last active
November 21, 2020 13:23
-
-
Save oschettler/11fdf9f3e0c7b70fed6b24722aee52f5 to your computer and use it in GitHub Desktop.
Ein Katzenspielzeug mit Calliope Mini und Microblocks - https://twitter.com/codekulturbonn/status/1330139599188930561
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
module main | |
author unknown | |
version 1 0 | |
description '' | |
variables x x2 led blip | |
spec ' ' 'blink' 'blink' | |
to blink { | |
led = (random 1 3) | |
if (led == 1) { | |
'Calliope set LED' 0 0 100 | |
} (led == 2) { | |
'Calliope set LED' 0 100 0 | |
} (led == 3) { | |
'Calliope set LED' 100 0 0 | |
} | |
waitMillis 50 | |
'Calliope set LED' 0 0 0 | |
} | |
script 54 54 { | |
whenStarted | |
x = ('[sensors:tiltY]') | |
forever { | |
if (8 < (random 1 10)) { | |
blink | |
'play tone' 'D' 0 200 | |
} | |
x2 = ('[sensors:tiltY]') | |
if (5 < (absoluteValue (x2 - x))) { | |
x = x2 | |
'[display:mbDisplay]' 4540401 | |
blink | |
'play tone' 'C' 0 200 | |
blink | |
'play tone' 'G' 0 200 | |
} | |
waitMillis 500 | |
'[display:mbDisplayOff]' | |
} | |
} | |
script 391 100 { | |
to blink {} | |
} | |
module 'Basic Sensors' Input | |
author MicroBlocks | |
version 1 0 | |
tags tilt acceleration light sensor | |
description 'Provides blocks to read tilt in the three axes, acceleration, temperature and light level. Many boards come with this particular set of sensors, such as the micro:bit, the Circuit Playground Express, the Calliope or the Citilab ED1.' | |
spec 'r' '[sensors:tiltX]' 'tilt x' | |
spec 'r' '[sensors:tiltY]' 'tilt y' | |
spec 'r' '[sensors:tiltZ]' 'tilt z' | |
spec 'r' '[sensors:acceleration]' 'acceleration' | |
spec 'r' '[display:lightLevel]' 'light level' | |
spec 'r' '[sensors:temperature]' 'temperature (°C)' | |
module Calliope | |
author MicroBlocks | |
version 1 0 | |
tags calliope led sound speaker | |
description 'Blocks to control the LED and speaker in Calliope boards.' | |
spec ' ' 'Calliope set LED' 'Calliope set LED red _ green _ blue _' 'auto auto auto' 0 0 100 | |
spec ' ' 'Calliope set speaker' 'Calliope set speaker _' 'bool' true | |
spec 'r' 'Calliope sound' 'Calliope loudness' | |
to 'Calliope set LED' red green blue { | |
'[display:neoPixelSetPin]' -1 false | |
local 'rgb' ((((255 * red) / 100) & 255) << 16) | |
rgb = (rgb | ((((255 * green) / 100) & 255) << 8)) | |
rgb = (rgb | (((255 * blue) / 100) & 255)) | |
'[display:neoPixelSend]' rgb | |
} | |
to 'Calliope set speaker' onOff { | |
digitalWriteOp 23 true | |
digitalWriteOp 24 onOff | |
digitalWriteOp 25 (not onOff) | |
} | |
to 'Calliope sound' { | |
local 'min' 10000 | |
local 'max' 0 | |
local 'end_msecs' ((millisOp) + 50) | |
repeatUntil ((millisOp) > end_msecs) { | |
local 'sample' (analogReadOp 0) | |
if (sample < min) { | |
min = sample | |
} | |
if (sample > max) { | |
max = sample | |
} | |
} | |
return ((100 * (max - min)) / 1020) | |
} | |
module 'LED Display' Output | |
author MicroBlocks | |
version 1 0 | |
tags pixel matrix led tft | |
description 'Display primitives for the 5x5 LED display on the BBC micro:bit, Calliope mini and M5Atom Matrix. Boards with TFT displays (such as the Citilab ED1 or the M5Stack family) also support this primitives in a simulated "fat pixel" display.' | |
spec ' ' '[display:mbDisplay]' 'display _' 'microbitDisplay' 15237440 | |
spec ' ' '[display:mbDisplayOff]' 'clear display' | |
spec ' ' '[display:mbPlot]' 'plot x _ y _' 'num num' 3 3 | |
spec ' ' '[display:mbUnplot]' 'unplot x _ y _' 'num num' 3 3 | |
spec ' ' 'displayCharacter' 'display character _' 'str' 'A' | |
to displayCharacter s { | |
s = ('[data:join]' '' s) | |
'[display:mbDrawShape]' ('[display:mbShapeForLetter]' (at 1 s)) | |
} | |
module Tone Output | |
author MicroBlocks | |
version 1 3 | |
tags tone sound music audio note speaker | |
description 'Audio tone generation. Make music with MicroBlocks! | |
' | |
variables _tonePin _toneInitalized _toneLoopOverhead | |
spec ' ' 'play tone' 'play note _ octave _ for _ ms' 'auto num num' 'C' 0 500 | |
spec ' ' 'playMIDIKey' 'play midi key _ for _ ms' 'num num' 60 500 | |
spec ' ' 'play frequency' 'play frequency _ for _ ms' 'num num' 261 500 | |
spec ' ' 'attach buzzer to pin' 'attach buzzer to pin _' 'auto' '' | |
spec 'r' '_measureLoopOverhead' '_measureLoopOverhead' | |
spec 'r' '_baseFreqForNote' '_baseFreqForNote _' 'auto' 'c' | |
spec 'r' '_baseFreqForSharpOrFlat' '_baseFreqForSharpOrFlat _' 'auto' 'c#' | |
spec 'r' '_baseFreqForSemitone' '_baseFreqForSemitone _' 'num' 0 | |
spec ' ' '_toneLoop' '_toneLoop _ for _ ms' 'num num' 440000 100 | |
to '_baseFreqForNote' note { | |
comment 'Return the frequency for the given note in the middle-C octave | |
scaled by 1000. For example, return 440000 (440Hz) for A. | |
Note names may be upper or lower case. Note names | |
may be followed by # for a sharp or b for a flat.' | |
if (or (note == 'c') (note == 'C')) { | |
return 261626 | |
} (or (note == 'd') (note == 'D')) { | |
return 293665 | |
} (or (note == 'e') (note == 'E')) { | |
return 329628 | |
} (or (note == 'f') (note == 'F')) { | |
return 349228 | |
} (or (note == 'g') (note == 'G')) { | |
return 391995 | |
} (or (note == 'a') (note == 'A')) { | |
return 440000 | |
} (or (note == 'b') (note == 'B')) { | |
return 493883 | |
} | |
return ('_baseFreqForSharpOrFlat' note) | |
} | |
to '_baseFreqForSemitone' semitone { | |
if (0 == semitone) { | |
return 261626 | |
} (1 == semitone) { | |
return 277183 | |
} (2 == semitone) { | |
return 293665 | |
} (3 == semitone) { | |
return 311127 | |
} (4 == semitone) { | |
return 329628 | |
} (5 == semitone) { | |
return 349228 | |
} (6 == semitone) { | |
return 369994 | |
} (7 == semitone) { | |
return 391995 | |
} (8 == semitone) { | |
return 415305 | |
} (9 == semitone) { | |
return 440000 | |
} (10 == semitone) { | |
return 466164 | |
} (11 == semitone) { | |
return 493883 | |
} | |
} | |
to '_baseFreqForSharpOrFlat' note { | |
comment 'Return the frequency for the given sharp or flat note in the | |
middle-C octave scaled by 1000. Only handles black keys. | |
Thus, you can''t write E# to mean F.' | |
if (or (or (note == 'c#') (note == 'C#')) (or (note == 'db') (note == 'Db'))) { | |
return 277183 | |
} (or (or (note == 'd#') (note == 'D#')) (or (note == 'eb') (note == 'Eb'))) { | |
return 311127 | |
} (or (or (note == 'f#') (note == 'F#')) (or (note == 'gb') (note == 'Gb'))) { | |
return 369994 | |
} (or (or (note == 'g#') (note == 'G#')) (or (note == 'ab') (note == 'Ab'))) { | |
return 415305 | |
} (or (or (note == 'a#') (note == 'A#')) (or (note == 'bb') (note == 'Bb'))) { | |
return 466164 | |
} | |
comment 'Unrecognized note names map to 0.1 Hz, which is inaudible. | |
This helps users find typos in their tunes.' | |
return 100 | |
} | |
to '_measureLoopOverhead' { | |
comment 'Measure the loop overhead on this device' | |
local 'halfCycle' 100 | |
local 'startT' (microsOp) | |
repeat 100 { | |
digitalWriteOp _tonePin false | |
waitMicros halfCycle | |
digitalWriteOp _tonePin false | |
waitMicros halfCycle | |
} | |
local 'usecs' ((microsOp) - startT) | |
return ((usecs - 20000) / 200) | |
} | |
to '_toneLoop' scaledFreq ms { | |
if (_toneInitalized == 0) {'attach buzzer to pin' ''} | |
if ('[io:hasTone]') { | |
'[io:playTone]' _tonePin (scaledFreq / 1000) | |
waitMillis ms | |
'[io:playTone]' _tonePin 0 | |
} else { | |
local 'halfCycle' ((500000000 / scaledFreq) - _toneLoopOverhead) | |
local 'cycles' ((ms * 500) / halfCycle) | |
repeat cycles { | |
digitalWriteOp _tonePin true | |
waitMicros halfCycle | |
digitalWriteOp _tonePin false | |
waitMicros halfCycle | |
} | |
} | |
} | |
to 'attach buzzer to pin' pinNumber { | |
if (pinNumber == '') { | |
comment 'Pin number not specified; use default pin for this device' | |
if ((boardType) == 'Citilab ED1') { | |
_tonePin = 26 | |
} ((boardType) == 'M5Stack-Core') { | |
_tonePin = 25 | |
} ((boardType) == 'M5StickC') { | |
_tonePin = 26 | |
} ((boardType) == 'Calliope') { | |
digitalWriteOp 23 true | |
digitalWriteOp 24 true | |
_tonePin = 25 | |
} ((boardType) == 'D1-Mini') { | |
_tonePin = 12 | |
} else { | |
_tonePin = -1 | |
} | |
} else { | |
_tonePin = pinNumber | |
} | |
_toneLoopOverhead = ('_measureLoopOverhead') | |
_toneInitalized = (booleanConstant true) | |
} | |
to 'play frequency' freq ms { | |
'_toneLoop' (freq * 1000) ms | |
} | |
to 'play tone' note octave ms { | |
local 'freq' ('_baseFreqForNote' note) | |
if (octave < 0) { | |
repeat (absoluteValue octave) { | |
freq = (freq / 2) | |
} | |
} | |
repeat octave { | |
freq = (freq * 2) | |
} | |
'_toneLoop' freq ms | |
} | |
to playMIDIKey key ms { | |
local 'freq' ('_baseFreqForSemitone' (key % 12)) | |
local 'octave' ((key / 12) - 5) | |
if (octave < 0) { | |
repeat (absoluteValue octave) { | |
freq = (freq / 2) | |
} | |
} | |
repeat octave { | |
freq = (freq * 2) | |
} | |
'_toneLoop' freq ms | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment