module main
author unknown
version 1 0 
description ''
variables umbralx umbraly stop 

script 477 64 {
whenButtonPressed 'A'
comment 'Botón A pulsado.
Parada de emergencia.'
bleRadio_sendString 's'
ms_clear_display
ms_display (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 4 4 4 255) (colorSwatch 10 10 10 255) (colorSwatch 35 190 30 255) (colorSwatch 10 10 10 255) (colorSwatch 35 190 30 255) (colorSwatch 10 10 10 255) (colorSwatch 35 190 30 255) (colorSwatch 8 8 8 255) (colorSwatch 0 0 0 255) (colorSwatch 7 7 7 255) (colorSwatch 35 190 30 255) (colorSwatch 4 4 4 255) (colorSwatch 4 4 4 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 4 4 4 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 35 190 30 255) (colorSwatch 12 12 12 255) (colorSwatch 0 0 0 255) (colorSwatch 3 3 3 255) (colorSwatch 35 190 30 255)
}

script 968 69 {
whenCondition (and (and (('[sensors:tiltX]') < stop) (('[sensors:tiltX]') > (stop * -1))) (and (('[sensors:tiltY]') < stop) (('[sensors:tiltY]') > (stop * -1))))
bleRadio_sendString 's'
ms_clear_display
ms_display (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 4 4 4 255) (colorSwatch 10 10 10 255) (colorSwatch 35 190 30 255) (colorSwatch 10 10 10 255) (colorSwatch 35 190 30 255) (colorSwatch 10 10 10 255) (colorSwatch 35 190 30 255) (colorSwatch 8 8 8 255) (colorSwatch 0 0 0 255) (colorSwatch 7 7 7 255) (colorSwatch 35 190 30 255) (colorSwatch 4 4 4 255) (colorSwatch 4 4 4 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 4 4 4 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 35 190 30 255) (colorSwatch 12 12 12 255) (colorSwatch 0 0 0 255) (colorSwatch 3 3 3 255) (colorSwatch 35 190 30 255)
}

script 66 132 {
whenStarted
'[ble:radioSetGroup]' 6
umbralx = 40
umbraly = 40
stop = 39
}

script 80 362 {
whenCondition (('[sensors:tiltX]') > umbralx)
bleRadio_sendString 'r'
ms_clear_display
ms_display (colorSwatch 0 0 0 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 8 8 8 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 4 4 4 255) (colorSwatch 2 2 2 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 3 3 3 255) (colorSwatch 0 0 0 255)
}

script 471 372 {
whenCondition (('[sensors:tiltX]') < (umbralx * -1))
bleRadio_sendString 'l'
ms_clear_display
ms_display (colorSwatch 0 0 0 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 8 8 8 255) (colorSwatch 4 4 4 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 8 8 8 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 3 3 3 255) (colorSwatch 0 0 0 255)
}

script 1011 594 {
whenStarted
comment 'Instrucciones para conocer los valores
de la inclinación del eje X y Eje Y.
Uso interno para pruebas.'
forever {
  sayIt ('[sensors:tiltX]') ('[sensors:tiltY]')
}
}

script 511 648 {
whenCondition (('[sensors:tiltY]') < (umbraly * -1))
bleRadio_sendString 'f'
ms_clear_display
ms_display (colorSwatch 0 0 0 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 3 3 3 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 4 4 4 255) (colorSwatch 35 190 30 255) (colorSwatch 8 8 8 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 3 3 3 255) (colorSwatch 0 0 0 255)
}

script 78 681 {
whenCondition (('[sensors:tiltY]') > umbraly)
bleRadio_sendString 'b'
ms_clear_display
ms_display (colorSwatch 0 0 0 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 6 6 6 255) (colorSwatch 35 190 30 255) (colorSwatch 3 3 3 255) (colorSwatch 35 190 30 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 35 190 30 255) (colorSwatch 2 2 2 255) (colorSwatch 0 0 0 255) (colorSwatch 0 0 0 255) (colorSwatch 35 190 30 255) (colorSwatch 3 3 3 255) (colorSwatch 0 0 0 255)
}


module 'BLE Radio' Comm
author MicroBlocks
version 1 1 
description 'This library supports easy message exchange among boards that support Bluetooth Low Energy (BLE).

A message can contain a string, a number, or both. Messages are received by all boards in the same BLE radio group as the sender.

This library is similar to the Radio library, but messages are sent via BLE advertisements instead of the Nordic radio system.
'
variables _bleRadio_lastNumber _bleRadio_lastString 

  spec ' ' 'bleRadio_sendNumber' 'ble send number _' 'num' 123
  spec ' ' 'bleRadio_sendString' 'ble send string _' 'str' 'Hello!'
  spec ' ' 'bleRadio_sendPair' 'ble send pair _ = _' 'str num' 'light' 10
  space
  spec 'r' 'bleRadio_messageReceived' 'ble message received?'
  spec 'r' 'bleRadio_receivedInteger' 'ble last number'
  spec 'r' 'bleRadio_receivedString' 'ble last string'
  space
  spec ' ' '[ble:radioSetGroup]' 'ble set group _ (0-255)' 'num' 0

to bleRadio_messageReceived {
  local 'msg' ('[ble:radioReceive]')
  if ((size msg) < 4) {
    return (booleanConstant false)
  }
  _bleRadio_lastNumber = ((at 1 msg) | (((at 2 msg) << 8) | (((at 3 msg) << 16) | ((at 4 msg) << 24))))
  local 'highByte' (at 4 msg)
  if (and (64 <= highByte) (highByte <= 127)) {
    _bleRadio_lastNumber = 'out of range number (positve)'
  }
  if (and (128 <= highByte) (highByte <= 191)) {
    _bleRadio_lastNumber = 'out of range number (negative)'
  }
  _bleRadio_lastString = ('[data:join]' '' ('[data:copyFromTo]' msg 5))
  return (booleanConstant true)
}

to bleRadio_receivedInteger {
  return _bleRadio_lastNumber
}

to bleRadio_receivedString {
  if (_bleRadio_lastString == 0) {_bleRadio_lastString = ''}
  return _bleRadio_lastString
}

to bleRadio_sendNumber n {
  bleRadio_sendPair '' n
}

to bleRadio_sendPair s n {
  local 'msg' ('[data:newByteArray]' 4)
  comment 'MicroBlocks BLE Radio message format:
    MicroBlocks integer (4 bytes, LSB first)
    string (UTF8, variable length)'
  atPut 1 msg (n & 255)
  atPut 2 msg ((n >> 8) & 255)
  atPut 3 msg ((n >> 16) & 255)
  atPut 4 msg ((n >> 24) & 255)
  msg = ('[data:join]' msg ('[data:asByteArray]' s))
  '[ble:radioStartBeam]' msg
  waitMillis 500
  '[ble:radioStopBeam]'
}

to bleRadio_sendString s {
  bleRadio_sendPair s 0
}


module microSTEAMakers
author 'Joan Guillén'
version 1 8 
depends Tone Color 
tags keyestudio steam 
choices ms_led_imageMenu heart 'small heart' yes no happy sad confused angry asleep surprised silly fabulous meh 't-shirt' 'roller skate' duck house tortoise butterfly 'stick figure' ghost sword giraffe skull umbrella snake rabbit cow 'quarter note' 'eight note' pitchfork target triangle 'left triangle' 'chess board' diamond 'small diamond' square 'small square' scissors 
choices ms_inclination 'ms;down' x y 
choices ms_accel x y z 
choices ms_energy mA mV mW 
choices ms_touchPin '0' '1' '2' '4' '6' '8' '12' 
choices ms_energyMeter 'ms;start' 'ms;stop' 'ms;reset' 
description 'Provides custom blocks for the micro:STEAMakers, an ESP32 board with WiFi, BLE, 5x5 RGB LED matrix, 2 buttons, touch, light, sound, temperature and humidity sensors, six-axis accelerometer/gyroscope, energy control, and GPIO and I2C pins for additional extensions.
'
variables _ms_neoPixels _ms_neoPixels_scale _ms_scrolling_stopped _ms_energyReading _ms_energySum _ms_aht20_on _ms_aht20_reading _ms_aht20_data _ms_MPU6050_on _ms_MPU6050_add 

  spec ' ' 'ms_display' 'ms display #BR# _ _ _ _ _ #BR# _ _ _ _ _ #BR# _ _ _ _ _ #BR# _ _ _ _ _ #BR# _ _ _ _ _' 'color color color color color color color color color color color color color color color color color color color color color color color color color'
  spec ' ' 'ms_clear_display' 'ms clear display'
  spec ' ' 'ms_setGlobalBrightnessScale' 'ms scale brightness to _ %' 'num' 100
  spec ' ' 'ms_plot' 'ms plot x _ y _ with _' 'num num color' 3 3
  spec ' ' 'ms_unplot' 'ms unplot x _ y _' 'num num' 3 3
  space
  spec 'r' 'ms_touching' 'ms touching ?'
  spec 'r' 'ms_touching_pin' 'ms pin _ touched?' 'menu.ms_touchPin' 0
  spec 'r' 'ms_light' 'ms light'
  spec 'r' 'ms_sound' 'ms sound'
  spec 'r' 'ms_temperature' 'ms temperature ºC'
  spec 'r' 'ms_humidity' 'ms humidity %'
  space
  spec 'r' 'ms_accel' 'ms accel _ m/s2' 'menu.ms_accel' 'x'
  spec 'r' 'ms_gyro' 'ms gyro _ deg/s' 'menu.ms_accel' 'x'
  spec 'r' 'ms_inclination' 'ms inclination towards _ º' 'menu.ms_inclination' 'ms;down'
  space
  spec 'r' 'ms_energy' 'ms energy _' 'menu.ms_energy' 'mW'
  spec ' ' 'ms_energyMeter' 'ms _ energy meter' 'menu.ms_energyMeter' 'ms;start'
  spec 'r' 'ms_energyConsumption' 'ms energy consumption (µWh)'
  space
  spec ' ' '_ms_neoPixels_setup' '_ms_neoPixels_setup'
  spec ' ' '_ms_neoPixels_update' '_ms_neoPixels_update'
  spec 'r' '_ms_imageNames' '_ms_imageNames'
  spec ' ' '_ms_aht20_setup' '_ms_aht20_setup'
  spec ' ' '_ms_aht20_getData' '_ms_aht20_getData'
  spec ' ' '_ms_MPU6050_setup' '_ms_MPU6050_setup'
  spec ' ' '_ms_energy_read_loop' '_ms_energy_read_loop'

to '_ms_MPU6050_setup' {
  _ms_MPU6050_add = (hexToInt '68')
  comment 'setclockSource  0x01'
  local 'value' (i2cGet _ms_MPU6050_add (hexToInt '6b'))
  value = (value & (hexToInt 'F8'))
  value = (value | (hexToInt '01'))
  '[sensors:i2cWrite]' _ms_MPU6050_add ('[data:makeList]' (hexToInt '6B') value)
  comment 'setScale amb MPU6050_SCALE_2000DPS -> 0x03'
  comment 'dpsPerDigit = .060975f'
  local 'value' (i2cGet _ms_MPU6050_add (hexToInt '1B'))
  value = (value & (hexToInt 'E7'))
  value = (value | (3 << 3))
  '[sensors:i2cWrite]' _ms_MPU6050_add ('[data:makeList]' (hexToInt '1B') value)
  comment 'setRange amb MPU6050_RANGE_2G -> 0x00'
  comment 'rangePerDigit = .000061f'
  local 'value' (i2cGet _ms_MPU6050_add (hexToInt '1C'))
  value = (value & (hexToInt 'E7'))
  value = (value | (0 << 3))
  '[sensors:i2cWrite]' _ms_MPU6050_add ('[data:makeList]' (hexToInt '1C') value)
  comment 'setSleepEnabled -> false'
  comment 'registerBit (0x6B,6)'
  local 'value' (i2cGet _ms_MPU6050_add (hexToInt '6B'))
  value = (value & ('~' (1 << 6)))
  '[sensors:i2cWrite]' _ms_MPU6050_add ('[data:makeList]' (hexToInt '6B') value)
}

to '_ms_aht20_getData' {
  if (not _ms_aht20_on) {'_ms_aht20_setup'}
  _ms_aht20_reading = (booleanConstant true)
  local 'aht20_read' (newList 6)
  '[sensors:i2cWrite]' (hexToInt '38') ('[data:makeList]' (hexToInt 'AC') (hexToInt '33') (hexToInt '00'))
  waitMillis 75
  '[sensors:i2cRead]' (hexToInt '38') aht20_read
  _ms_aht20_data = aht20_read
  _ms_aht20_reading = (booleanConstant false)
}

to '_ms_aht20_setup' {
  '[sensors:i2cWrite]' (hexToInt '38') ('[data:makeList]' (hexToInt 'BE') (hexToInt '08') (hexToInt '00'))
  '[sensors:i2cWrite]' (hexToInt '38') ('[data:makeList]' (hexToInt 'AC') (hexToInt '33') (hexToInt '00'))
  _ms_aht20_on = (booleanConstant true)
  waitMillis 75
}

to '_ms_energy_read_loop' {
  repeatUntil (not _ms_energyReading) {
    _ms_energySum = (_ms_energySum + (ms_energy 'mA'))
    waitMillis 1000
  }
}

to '_ms_imageNames' {
  return '{"heart":4685802, "small heart":145728, "yes":2269696, "no":18157905, "happy":15237440, "sad":18284864, "confused":22348096, "angry":23036241, "asleep":459616, "surprised":4526090, "silly":25984017, "fabulous":15008639, "meh":2236443, "t-shirt":15154043, "roller skate":11534104, "duck":489702, "house":10976708, "tortoise":359872, "butterfly":29332475, "stick figure":18158564, "ghost":23068334, "sword":4657284, "giraffe":10946627, "skull":15171246, "umbrella":6460398, "snake":469859, "rabbit":16104613, "cow":4685361, "quarter note":7573636, "eight note":7590276, "pitchfork":4357813, "target":4681156, "triangle":1026176, "left triangle":32805985, "chess board":11184810, "diamond":4539716, "small diamond":141440, "square":33080895, "small square":469440, "scissors":20287859}'
}

to '_ms_neoPixels_setup' {
  '[display:mbDisplayOff]'
  waitMicros 300
  if (_ms_neoPixels == 0) {
    _ms_neoPixels = (newList 25 0)
    _ms_neoPixels_scale = 100
  }
}

to '_ms_neoPixels_update' {
  comment 'Alway set the NeoPixel pin; may have been changed by another library.'
  '[display:neoPixelSetPin]' 3 false
  if (100 == _ms_neoPixels_scale) {
    '[display:neoPixelSend]' _ms_neoPixels
  } else {
    local 'currentPixels' (newList 25)
    local 'adjustedScale' ('[misc:rescale]' _ms_neoPixels_scale 0 100 31 100)
    adjustedScale = (maximum 0 (minimum adjustedScale 100))
    for pix 25 {
      atPut pix currentPixels ('[misc:hsvColor]' ('[misc:hue]' (at pix _ms_neoPixels)) ('[misc:saturation]' (at pix _ms_neoPixels)) ((('[misc:brightness]' (at pix _ms_neoPixels)) * adjustedScale) / 100))
    }
    '[display:neoPixelSend]' currentPixels
  }
  waitMicros 300
}

to ms_accel axis {
  if (not _ms_MPU6050_on) {'_ms_MPU6050_setup'}
  '[sensors:i2cWrite]' _ms_MPU6050_add ('[data:makeList]' (hexToInt '3B'))
  local 'reading' (newList 6)
  '[sensors:i2cRead]' _ms_MPU6050_add reading
  local 'msB' 3
  local 'lsB' 4
  local 'direction' -1
  if (axis == 'y') {
    msB = 1
    lsB = 2
    direction = 1
  }
  if (axis == 'z') {
    msB = 5
    lsB = 6
    direction = 1
  }
  local 'result' (at msB reading)
  if ((result >> 7) == 1) {
    result = (-1 * (('~' result) & 127))
  }
  return (direction * (((result << 8) | (at lsB reading)) / 1672))
}

to ms_clear_display {
  '_ms_neoPixels_setup'
  atPut 'all' _ms_neoPixels 0
  '_ms_neoPixels_update'
  '[display:mbDisplayOff]'
}

to ms_display c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 c25 {
  '_ms_neoPixels_setup'
  for i 25 {
    atPut i _ms_neoPixels (argOrDefault i 0)
  }
  '_ms_neoPixels_update'
}

to ms_energy op {
  if (op == 'mV') {return 3300}
  local 'ampCounter' 0
  for i 500 {
    ampCounter = (ampCounter + (analogReadOp 23))
  }
  ampCounter = (ampCounter * 2)
  ampCounter = ((ampCounter * 428) + 43560611)
  ampCounter = (ampCounter / 1000000)
  if (op == 'mA') {
    return ampCounter
  } (op == 'mW') {
    return ((ampCounter * 3300) / 1000)
  } else {
    return
  }
}

to ms_energyConsumption {
  return ((_ms_energySum * 33) / 36)
}

to ms_energyMeter op {
  if (op == 'ms;start') {
    _ms_energyReading = (booleanConstant true)
    sendBroadcast '_ms_energy_read_loop'
  } (op == 'ms;stop') {
    _ms_energyReading = (booleanConstant false)
  } (op == 'ms;reset') {
    _ms_energySum = 0
  }
}

to ms_gyro axis {
  if (not _ms_MPU6050_on) {'_ms_MPU6050_setup'}
  '[sensors:i2cWrite]' _ms_MPU6050_add ('[data:makeList]' (hexToInt '43'))
  local 'reading' (newList 6)
  '[sensors:i2cRead]' _ms_MPU6050_add reading
  local 'msB' 3
  local 'lsB' 4
  local 'direction' 1
  if (axis == 'y') {
    msB = 1
    lsB = 2
    direction = -1
  }
  if (axis == 'z') {
    msB = 5
    lsB = 6
    direction = -1
  }
  local 'result' (at msB reading)
  if ((result >> 7) == 1) {
    result = (-1 * (('~' result) & 127))
  }
  return ((10 * direction) * (((result << 8) | (at lsB reading)) / 164))
}

to ms_humidity {
  if (not _ms_aht20_reading) {'_ms_aht20_getData'}
  local 'rawData' _ms_aht20_data
  local 'calc' 0
  calc = (at 2 rawData)
  calc = (calc << 8)
  calc = (calc + (at 3 rawData))
  calc = (calc << 4)
  calc = (calc + ((at 4 rawData) >> 4))
  return ((calc * 100) / 1048576)
}

to ms_inclination op {
  if (not _ms_MPU6050_on) {'_ms_MPU6050_setup'}
  if (op == 'x') {return ((ms_accel 'x') * 9)}
  if (op == 'y') {return ((ms_accel 'y') * 9)}
  return (((ms_accel 'z') + 10) * 9)
}

to ms_light {
  return (analogReadOp 24)
}

to ms_plot x y color {
  '_ms_neoPixels_setup'
  if (and (0 < x) (and (0 < y) (and (6 > x) (6 > y)))) {
    atPut (x + ((y - 1) * 5)) _ms_neoPixels color
    '_ms_neoPixels_update'
  }
}

to ms_setGlobalBrightnessScale scale {
  '_ms_neoPixels_setup'
  _ms_neoPixels_scale = scale
  '_ms_neoPixels_update'
}

to ms_sound {
  return (analogReadOp 22)
}

to ms_temperature {
  if (not _ms_aht20_reading) {'_ms_aht20_getData'}
  local 'rawData' _ms_aht20_data
  local 'calc' 0
  calc = ((at 4 rawData) & (hexToInt '0f'))
  calc = (calc << 8)
  calc = (calc + (at 5 rawData))
  calc = (calc << 8)
  calc = (calc + (at 6 rawData))
  return (((calc * 200) / 1048576) - 50)
}

to ms_touching {
  return (('[sensors:touchRead]' 27) < 35)
}

to ms_touching_pin edgePin {
  return (('[sensors:touchRead]' (0 + edgePin)) < 35)
}

to ms_unplot x y {
  '_ms_neoPixels_setup'
  if (and (0 < x) (and (0 < y) (and (6 > x) (6 > y)))) {
    atPut (x + ((y - 1) * 5)) _ms_neoPixels 0
    '_ms_neoPixels_update'
  }
}

