module main
author unknown
version 1 0 
description ''
variables wet scaled values 

	spec ' ' 'measure' 'measure'

to measure {
  '[display:mbDisplay]' 15237440
  values = ('[data:makeList]')
  repeat 10 {
    '[data:addLast]' (analogReadOp 1) values
    waitMillis 200
  }
  wet = 0
  for i 10 {
    wet = (maximum (at i values) wet)
  }
  '[display:mbDisplayOff]'
}

script 429 40 {
whenStarted
forever {
  measure
  printIt wet
  '[radio:sendPair]' 'wet' wet
  if (wet > 50) {
    'play tone' 'C' 0 500
    digitalWriteOp 2 true
    waitMillis 1000
    digitalWriteOp 2 false
  }
  waitMillis 600000
}
}

script 48 68 {
whenButtonPressed 'A'
measure
scroll_text wet
}

script 52 201 {
to measure {}
}

script 425 380 {
whenButtonPressed 'B'
digitalWriteOp 2 true
waitMillis 1000
digitalWriteOp 2 false
}


module 'Basic Sensors' Input
author MicroBlocks
version 1 1 
tags tilt acceleration light sensor 
choices accelerometerRange '1' '2' '4' '8' 
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)'
	spec 'r' '[sensors:magneticField]' 'magnetic field'
	spec ' ' '_setAccelRange' 'set acceleration range _ g = 100' 'menu.accelerometerRange' '1'

to '_setAccelRange' n {
  '[sensors:setAccelerometerRange]' (0 + n)
}


module 'LED Display' Output
author MicroBlocks
version 1 2 
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.'
variables _stop_scrolling_text 

	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'
	spec ' ' 'scroll_text' 'scroll text _ : pausing _ ms' 'str num' 'HELLO ROSA!' 100
	spec ' ' 'stopScrollingText' 'stop scrolling'

to displayCharacter s {
  s = ('[data:join]' '' s)
  if ((size s) == 0) {
    '[display:mbDisplayOff]'
    return 0
  }
  '[display:mbDrawShape]' ('[display:mbShapeForLetter]' (at 1 s))
}

to scroll_text text optionalDelay {
  text = ('[data:join]' text '')
  delay = 100
  if ((pushArgCount) > 1) {
    delay = optionalDelay
  }
  _stop_scrolling_text = (booleanConstant false)
  local 'length' (size text)
  for position ((length * 6) + 6) {
    if _stop_scrolling_text {return 0}
    for i length {
      '[display:mbDrawShape]' ('[display:mbShapeForLetter]' ('[data:unicodeAt]' i text)) (((i * 6) + 2) - position) 1
    }
    waitMillis delay
  }
}

to stopScrollingText {
  _stop_scrolling_text = (booleanConstant true)
  waitMillis 10
  '[display:mbDisplayOff]'
}


module Radio Comm
author MicroBlocks
version 1 1 
tags radio communication messaging network 
description 'Send and receive messages between micro:bit boards via their built-in radio system.'

	spec ' ' '[radio:sendInteger]' 'radio send number _' 'num' 123
	spec ' ' '[radio:sendString]' 'radio send string _' 'str' 'Hello!'
	spec ' ' '[radio:sendPair]' 'radio send pair _ = _' 'str num' 'light' 10
	spec 'r' '[radio:messageReceived]' 'radio message received?'
	spec 'r' '[radio:receivedInteger]' 'radio last number'
	spec 'r' '[radio:receivedString]' 'radio last string'
	spec ' ' '[radio:setGroup]' 'radio set group _ (0-255)' 'num' 0
	spec ' ' '[radio:setPower]' 'radio set power (0-7) _' 'num' 4
	spec 'r' '_receivedMessageType' '_radio last message type'
	spec ' ' '_setChannel' '_radio set channel (0-83) _' 'num' 7
	spec 'r' '_signalStrength' '_radio last signal strength'

to '_receivedMessageType' {
  return ('[radio:receivedMessageType]')
}

to '_setChannel' channel {
  '[radio:setChannel]' channel
}

to '_signalStrength' {
  return ('[radio:signalStrength]')
}


module Tone Output
author MicroBlocks
version 1 6 
tags tone sound music audio note speaker 
description 'Audio tone generation. Make music with MicroBlocks!'
variables _tonePin _toneInitalized _toneLoopOverhead _toneNoteNames _toneArezzoNotes _toneFrequencies 

	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' '_baseFreqForSemitone' '_baseFreqForSemitone _' 'num' 0
	spec ' ' '_toneLoop' '_toneLoop _ for _ ms' 'num num' 440000 100
	spec 'r' '_trimmedLowercase' '_trimmedLowercase _' 'str' 'A. b C...'

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)
  if (_toneNoteNames == 0) {
    _toneNoteNames = ('[data:makeList]' 'c' 'c#' 'd' 'd#' 'e' 'f' 'f#' 'g' 'g#' 'a' 'a#' 'b' 'c_' 'db' 'd_' 'eb' 'e_' 'e#' 'f_' 'gb' 'g_' 'ab' 'a_' 'bb' 'b_' 'b#')
    _toneArezzoNotes = ('[data:makeList]' 'do' 'do#' 're' 're#' 'mi' 'fa' 'fa#' 'sol' 'sol#' 'la' 'la#' 'si' 'do_' 'dob' 're_' 'reb' 'mi_' 'mi#' 'fa_' 'solb' 'sol_' 'lab' 'la_' 'sib' 'si_' 'si#')
  }
  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 '_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
    } 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
}