module main author unknown version 1 0 description '' variables schalter script 126 50 { comment '## Klingeltöne im RTTTL-Formats (Nokia Ring Tone Text Transfer Language). ## In MicroBlocks Wiki sind die Klingeltöne erklärt. https://wiki.microblocks.fun/en/howto/play_ringtones Außerdem gibt es eine Sammlung mit 10000 Klingeltönen in einer riesigen Textdatei. ' } script 127 202 { whenButtonPressed 'A' 'play ringtone' 'Pacman:d=16,o=6,b=140: b5,b,f#,d#,8b,8d#,c,c7,g,f,8c7,8e,b5,b,f#,d#,8b,8d#,32d#,32e,f,32f,32f#,g,32g,32g#,a,8b' } script 130 336 { whenButtonPressed 'B' 'play ringtone' '007Theme:d=4,o=6,b=125: 8c5,16d5,16d5,8d5,d5,8c5,8c5,8c5,16d_5,16d_5,8d_5,d_5,8d5,8d5,8d5,8c5,16d5, 16d5,8d5,d5,8c5,8c5,8c5,8c5,16d_5,16d_5,8d_5,d_5,8d5,8c_5,8c5,8c,b,8g,8f,g' } script 136 484 { whenCondition (touchPin_logoTouched) stopAll 'play ringtone' 'Starwars:d=16,o=5,b=125: 8c#,8c#,c#,2f#,2c#6,8b,a#,8g#,2f#6,4c#6,8b,a#,8g#,2f#6,4c#6,8b,a#,8b,2g#,8c#, 8c#,c#,2f#,2c#6,8b,a#,8g#,2f#6,4c#6,8b,a#,8g#,2f#6,4c#6,8b,a#,8b,2g#,4c#,c#, 2d#,8c#6,8b,8a#,8g#,8f#,f#,8g#,a#,4g#' } script 145 686 { whenCondition (touchPin_pinTouched 0) stopAll 'play ringtone' 'Beethoven - Fur Elise : d=4,o=5,b=140: 8e6,8d#6,8e6,8d#6,8e6,8b,8d6,8c6,a,8p,8c,8e,8a,b,8p,8e,8g#,8b,c6,p,8e,8e6, 8d#6,8e6,8d#6,8e6,8b,8d6,8c6,a,8p,8c,8e,8a,b,8p,8e,8c6,8b,2a,' } module Ringtone Output author MicroBlocks version 1 3 depends _Tone description 'Plays tunes in RTTTL (Ringtone Text Transfer Language) a language developed by Nokia to store ringtones. Also known as Nokring.' variables '_RTTTL default duration' '_RTTTL default octave' '_RTTTL bpm' '_RTTTL song name' spec 'r' '_RTTTL parse control from' '_RTTTL parse control from _' 'str' '' spec ' ' 'play ringtone' 'play ringtone _' 'str' 'Pacman:d=16,o=6,b=140: b5,b,f#,d#,8b,8d#,c,c7,g,f,8c7,8e,b5,b,f#,d#,8b,8d#,32d#,32e,f,32f,32f#,g,32g,32g#,a,8b' spec 'r' 'current song name' 'current song name' spec ' ' '_RTTTL play note' '_RTTTL play note _' 'str' '8c#' to '_RTTTL parse control from' 'control section' { comment 'Extract control section' local 'd' 4 local 'o' 6 local 'b' 63 repeatUntil ((size (v 'control section')) == 0) { local 'next separator' ('[data:find]' ',' (v 'control section')) if ((v 'next separator') == -1) { 'next separator' = (size (v 'control section')) } local 'to process' ('[data:copyFromTo]' (v 'control section') 1 (v 'next separator')) 'control section' = ('[data:copyFromTo]' (v 'control section') ((v 'next separator') + 1)) 'next separator' = ('[data:find]' ',' (v 'to process')) if ((v 'next separator') == -1) { 'next separator' = ((size (v 'to process')) + 1) } if (('[data:find]' 'd' (v 'to process')) > 0) { d = ('[data:copyFromTo]' (v 'to process') (('[data:find]' '=' (v 'to process')) + 1) ((v 'next separator') - 1)) } (('[data:find]' 'b' (v 'to process')) > 0) { b = ('[data:copyFromTo]' (v 'to process') (('[data:find]' '=' (v 'to process')) + 1) ((v 'next separator') - 1)) } (('[data:find]' 'o' (v 'to process')) > 0) { o = ('[data:copyFromTo]' (v 'to process') (('[data:find]' '=' (v 'to process')) + 1) ((v 'next separator') - 1)) } } return ('[data:makeList]' d o b) } to '_RTTTL play note' note { local 'duration' '' local 'note name' '' local 'octave' (v '_RTTTL default octave') local 'ms / whole note' (240000 / (v '_RTTTL bpm')) local 'isDotted' (('[data:find]' '.' note) > -1) note = ('_trimmedLowercase' note) if (note == '') { comment 'empty note string; skip' return 0 } comment 'parse duration value, if there is one' repeatUntil (or (('[data:unicodeAt]' 1 note) < 48) (('[data:unicodeAt]' 1 note) > 57)) { duration = ('[data:join]' duration (at 1 note)) note = ('[data:copyFromTo]' note 2) } if (duration == '') { duration = (v '_RTTTL default duration') } comment 'parse note name' if (or (('[data:find]' '#' note) > -1) (('[data:find]' '_' note) > -1)) { 'note name' = ('[data:copyFromTo]' note 1 2) } else { 'note name' = (at 1 note) } note = ('[data:copyFromTo]' note ((size (v 'note name')) + 1)) comment 'deal with dotted notes' if isDotted { duration = ((((v 'ms / whole note') / duration) * 15) / 10) } else { duration = ((v 'ms / whole note') / duration) } comment 'parse octave, if there is one' if (note != '') { octave = (at 1 note) } 'play tone' (v 'note name') (octave - 4) duration } to 'current song name' { return (v '_RTTTL song name') } to 'play ringtone' RTTTL { comment 'Initialize frequency map.' ignoreArgs ('_baseFreqForNote' 'c') comment 'Remove song name' '_RTTTL song name' = ('[data:copyFromTo]' RTTTL 1 (('[data:find]' ':' RTTTL) - 1)) RTTTL = ('[data:copyFromTo]' RTTTL ((size (v '_RTTTL song name')) + 2)) local 'control' ('_RTTTL parse control from' ('[data:copyFromTo]' RTTTL 1 (('[data:find]' ':' RTTTL) - 1))) '_RTTTL default duration' = (at 1 control) '_RTTTL default octave' = (at 2 control) '_RTTTL bpm' = (at 3 control) RTTTL = ('[data:copyFromTo]' RTTTL (('[data:find]' ':' RTTTL) + 1)) repeatUntil ((size RTTTL) == 0) { local 'next separator' ('[data:find]' ',' RTTTL) if ((v 'next separator') == -1) { 'next separator' = ((size RTTTL) + 1) } local 'next note' ('[data:copyFromTo]' RTTTL 1 ((v 'next separator') - 1)) RTTTL = ('[data:copyFromTo]' RTTTL ((v 'next separator') + 1)) '_RTTTL play note' (v 'next note') } '_RTTTL song name' = '' } module Tone Output author MicroBlocks version 1 10 tags tone sound music audio note speaker choices tone_NoteName 'nt;c' 'nt;c#' 'nt;d' 'nt;d#' 'nt;e' 'nt;f' 'nt;f#' 'nt;g' 'nt;g#' 'nt;a' 'nt;a#' 'nt;b' description 'Audio tone generation. Make music with MicroBlocks! ' variables _tonePin _toneInitalized _toneLoopOverhead _toneNoteNames _toneArezzoNotes _toneFrequencies spec ' ' 'play tone' 'play note _ octave _ for _ ms' 'str.tone_NoteName num num' 'nt;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 space spec ' ' 'startTone' 'start tone _ Hz' 'num' 440 spec ' ' 'stopTone' 'stop tone' space spec ' ' 'attach buzzer to pin' 'attach buzzer to pin _' 'auto' '' space spec 'r' '_measureLoopOverhead' '_measureLoopOverhead' spec 'r' '_baseFreqForNote' '_baseFreqForNote _' 'auto' 'c' spec 'r' '_baseFreqForSemitone' '_baseFreqForSemitone _' 'num' 0 spec ' ' '_toneLoop' '_toneLoop _ for _ ms' 'num num' 440000 100 spec 'r' '_trimmedLowercase' '_trimmedLowercase _' 'str' 'A. b C...' spec ' ' '_tone init note names' '_tone init note names' 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.' local 'normalized note' ('_trimmedLowercase' note) 'normalized note' = (ifExpression ((at 1 (v 'normalized note')) == 'n') (v 'normalized note') ('[data:join]' 'nt;' (v 'normalized note'))) '_tone init note names' if (('[data:find]' (v 'normalized note') _toneArezzoNotes) > 0) { return ('_baseFreqForSemitone' ('[data:find]' (v 'normalized note') _toneArezzoNotes)) } else { return ('_baseFreqForSemitone' ('[data:find]' (v 'normalized note') _toneNoteNames)) } } to '_baseFreqForSemitone' semitone { if (_toneFrequencies == 0) {_toneFrequencies = ('[data:makeList]' 261626 277183 293665 311127 329628 349228 369994 391995 415305 440000 466164 493883 246942 277183 277183 311127 311127 349228 329628 369994 369994 415305 415305 466164 466164 523252)} if (and (1 <= semitone) (semitone <= (size _toneFrequencies))) { return (at semitone _toneFrequencies) } else { comment 'Bad note name; return 10 Hz' return 10000 } } 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 '_tone init note names' { if (_toneNoteNames == 0) { _toneNoteNames = ('[data:makeList]' 'nt;c' 'nt;c#' 'nt;d' 'nt;d#' 'nt;e' 'nt;f' 'nt;f#' 'nt;g' 'nt;g#' 'nt;a' 'nt;a#' 'nt;b' 'nt;c_' 'nt;db' 'nt;d_' 'nt;eb' 'nt;e_' 'nt;e#' 'nt;f_' 'nt;gb' 'nt;g_' 'nt;ab' 'nt;a_' 'nt;bb' 'nt;b_' 'nt;b#') _toneArezzoNotes = ('[data:makeList]' 'nt;do' 'nt;do#' 'nt;re' 'nt;re#' 'nt;mi' 'nt;fa' 'nt;fa#' 'nt;sol' 'nt;sol#' 'nt;la' 'nt;la#' 'nt;si' 'nt;do_' 'nt;dob' 'nt;re_' 'nt;reb' 'nt;mi_' 'nt;mi#' 'nt;fa_' 'nt;solb' 'nt;sol_' 'nt;lab' 'nt;la_' 'nt;sib' 'nt;si_' 'nt;si#') } } 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 '_trimmedLowercase' s { comment 'Return a copy of the given string without whitespace or periods and all lowercase.' local 'result' (newList (size s)) '[data:delete]' 'all' result for i (size s) { local 'ch' ('[data:unicodeAt]' i s) if (and (ch > 32) (ch != 46)) { if (and (65 <= ch) (ch <= 90)) {ch = (ch + 32)} '[data:addLast]' ch result } } return ('[data:unicodeString]' result) } 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 } ((boardType) == 'CodingBox') { _tonePin = 32 } 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 (freq <= 10000) { waitMillis ms return 0 } 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) + 1)) local 'octave' ((key / 12) - 5) if (octave < 0) { repeat (absoluteValue octave) { freq = (freq / 2) } } repeat octave { freq = (freq * 2) } '_toneLoop' freq ms } to startTone freq { if (_toneInitalized == 0) {'attach buzzer to pin' ''} if ('[io:hasTone]') {'[io:playTone]' _tonePin freq} } to stopTone { startTone 0 } module TouchPin Input author MicroBlocks version 1 0 description 'Capacitive pin touch sensing for micro:bit, Calliope mini, and other boards with touch pins. Works with pins that have 1M ohm external pullup resistors. ' variables _touchPin_isTouched _touchPin_releaseCount spec 'r' 'touchPin_pinTouched' 'pin _ touched' 'num' 0 spec 'r' 'touchPin_logoTouched' 'logo touched' to touchPin_logoTouched { if ('micro:bit v2' == (boardType)) { return (touchPin_pinTouched 26) } ('Calliope v3' == (boardType)) { return (touchPin_pinTouched 26) } return (booleanConstant false) } to touchPin_pinTouched pin { local 'threshold' 20 comment 'Discharge pin' digitalWriteOp pin false waitMillis 5 local 'n' 0 comment 'Wait for pin to become high, incrementing n' repeatUntil (digitalReadOp pin) { n += 1 if (n >= threshold) { comment 'Long charging time means pin is touched' _touchPin_isTouched = (booleanConstant true) _touchPin_releaseCount = 0 digitalWriteOp pin false return _touchPin_isTouched } } digitalWriteOp pin false if (_touchPin_isTouched == 0) { _touchPin_isTouched = (booleanConstant false) } comment 'count is < threshold so pin not touched' if _touchPin_isTouched { comment 'Require several low counts in a row to exit "isTouched" state to avoid jitter.' _touchPin_releaseCount += 1 if (_touchPin_releaseCount > 3) { _touchPin_isTouched = (booleanConstant false) } } return _touchPin_isTouched }