├── tests ├── __init__.py ├── libs │ ├── __init__.py │ ├── embeds │ │ ├── __init__.py │ │ ├── ble │ │ │ └── __init__.py │ │ └── test_switch.py │ ├── io_peripherals │ │ └── __init__.py │ └── measurements │ │ ├── __init__.py │ │ └── test_measure.py ├── conftest.py └── obniz_json_validator.py ├── obniz ├── parts │ ├── __init__.py │ ├── Camera │ │ ├── __init__.py │ │ └── ArduCAMMini │ │ │ ├── image.jpg │ │ │ └── wire.jpg │ ├── Light │ │ ├── __init__.py │ │ └── LED │ │ │ ├── led.jpg │ │ │ ├── led_on.jpg │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── __init__.py │ │ │ ├── README-ja.md │ │ │ └── README.md │ ├── Moving │ │ ├── __init__.py │ │ ├── DCMotor │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── README-ja.md │ │ │ ├── README.md │ │ │ └── __init__.py │ │ ├── ServoMotor │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── servocable.jpg │ │ │ ├── servomotor.gif │ │ │ ├── servomotor.jpg │ │ │ ├── __init__.py │ │ │ └── README-ja.md │ │ └── StepperMotor │ │ │ ├── wire.jpg │ │ │ ├── wire.png │ │ │ └── image.jpg │ ├── DistanceSensor │ │ ├── __init__.py │ │ ├── HC-SR04 │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── README-ja.md │ │ │ └── README.md │ │ └── GP2Y0A21YK0F │ │ │ ├── image.gif │ │ │ ├── image.jpg │ │ │ ├── wired.png │ │ │ ├── gp2y0a21yk0f.fzz │ │ │ ├── README-ja.md │ │ │ ├── README.md │ │ │ └── __init__.py │ ├── MovementSensor │ │ ├── __init__.py │ │ ├── Button │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── README-ja.md │ │ │ ├── README.md │ │ │ └── __init__.py │ │ ├── HC-SR505 │ │ │ ├── image.jpg │ │ │ ├── __init__.py │ │ │ ├── README-ja.md │ │ │ └── README.md │ │ ├── JoyStick │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── joystick.jpg │ │ │ ├── joystick_pins.jpg │ │ │ ├── __init__.py │ │ │ ├── README-ja.md │ │ │ └── README.md │ │ ├── KXR94-2050 │ │ │ └── image.jpg │ │ └── Potentiometer │ │ │ ├── pm.jpg │ │ │ ├── c_pm.jpg │ │ │ ├── wired.png │ │ │ ├── potentiometer.fzz │ │ │ ├── __init__.py │ │ │ ├── README-ja.md │ │ │ └── README.md │ ├── PressureSensor │ │ ├── __init__.py │ │ └── FSR-40X │ │ │ ├── wired.fzz │ │ │ ├── wired.png │ │ │ ├── README-ja.md │ │ │ ├── README.md │ │ │ └── __init__.py │ └── TemperatureSensor │ │ ├── __init__.py │ │ ├── i2c │ │ ├── __init__.py │ │ ├── BME280 │ │ │ ├── image.jpg │ │ │ ├── image2.jpg │ │ │ └── image3.jpg │ │ └── SHT31 │ │ │ ├── sht31.jpg │ │ │ ├── README.md │ │ │ ├── README-ja.md │ │ │ └── __init__.py │ │ └── analog │ │ ├── LM60 │ │ ├── wired.fzz │ │ ├── wired.png │ │ ├── __init__.py │ │ ├── README.md │ │ └── README-ja.md │ │ └── __init__.py ├── obniz │ ├── libs │ │ ├── __init__.py │ │ ├── hw │ │ │ ├── __init__.py │ │ │ ├── index.py │ │ │ ├── obnizb1.json │ │ │ ├── esp32w.json │ │ │ └── obnizb2.json │ │ ├── embeds │ │ │ ├── __init__.py │ │ │ ├── ble │ │ │ │ ├── __init__.py │ │ │ │ ├── ble_helper.py │ │ │ │ ├── ble_advertisement.py │ │ │ │ ├── ble_service.py │ │ │ │ ├── ble_remote_service.py │ │ │ │ ├── ble_remote_attribute_abstract.py │ │ │ │ ├── ble_remote_descriptor.py │ │ │ │ ├── ble_peripheral.py │ │ │ │ └── ble_descriptor.py │ │ │ ├── switch.py │ │ │ └── display.py │ │ ├── utils │ │ │ ├── __init__.py │ │ │ └── util.py │ │ ├── io_peripherals │ │ │ ├── __init__.py │ │ │ ├── ad.py │ │ │ └── io_.py │ │ └── measurements │ │ │ ├── __init__.py │ │ │ ├── logicanalyzer.py │ │ │ └── measure.py │ ├── __version__.py │ ├── obniz_uis.py │ └── obniz_parts.py └── __init__.py ├── .dockerignore ├── MANIFEST.in ├── .flake8 ├── .gitignore ├── json_schema ├── response │ ├── io │ │ ├── get.yml │ │ └── index.yml │ ├── ad │ │ ├── index.yml │ │ └── get.yml │ ├── spi │ │ ├── index.yml │ │ └── read.yml │ ├── uart │ │ ├── index.yml │ │ └── receive.yml │ ├── measure │ │ ├── index.yml │ │ └── echo.yml │ ├── switch │ │ ├── index.yml │ │ └── change.yml │ ├── system │ │ ├── index.yml │ │ └── pong.yml │ ├── message │ │ ├── index.yml │ │ └── receive.yml │ ├── logicanalyzer │ │ ├── index.yml │ │ └── data.yml │ ├── ws │ │ ├── index.yml │ │ ├── ready.yml │ │ └── redirect.yml │ ├── i2c │ │ ├── index.yml │ │ ├── master.yml │ │ └── slave.yml │ ├── debug │ │ ├── index.yml │ │ ├── error.yml │ │ └── warning.yml │ ├── ble │ │ ├── index.yml │ │ ├── central │ │ │ ├── scan_finish.yml │ │ │ ├── service_get_finish.yml │ │ │ ├── service_get.yml │ │ │ ├── characteristic_get_finish.yml │ │ │ ├── status_update.yml │ │ │ ├── descriptor_get_finish.yml │ │ │ ├── descriptor_get.yml │ │ │ ├── characteristic_notify.yml │ │ │ ├── characteristic_write.yml │ │ │ ├── characteristic_register_notify.yml │ │ │ ├── characteristic_unregister_notify.yml │ │ │ ├── characteristic_read.yml │ │ │ ├── descriptor_write.yml │ │ │ ├── descriptor_read.yml │ │ │ ├── error.yml │ │ │ ├── characteristic_get.yml │ │ │ ├── index.yml │ │ │ └── scan.yml │ │ └── peripheral │ │ │ ├── index.yml │ │ │ ├── status.yml │ │ │ ├── characteristic_notify_read.yml │ │ │ ├── characteristic_write.yml │ │ │ ├── characteristic_notify_write.yml │ │ │ ├── descriptor_notify_read.yml │ │ │ ├── characteristic_read.yml │ │ │ ├── descriptor_write.yml │ │ │ ├── descriptor_notify_write.yml │ │ │ └── descriptor_read.yml │ └── index.yml └── request │ ├── spi │ ├── deinit.yml │ ├── index.yml │ ├── write.yml │ └── init_master.yml │ ├── ad │ ├── deinit.yml │ ├── index.yml │ └── get.yml │ ├── i2c │ ├── deinit.yml │ ├── index.yml │ ├── write.yml │ ├── init_master.yml │ ├── read.yml │ └── init_slave.yml │ ├── io │ ├── deinit.yml │ ├── output.yml │ ├── input.yml │ ├── output_detail.yml │ ├── pull_type.yml │ ├── output_type.yml │ ├── index.yml │ └── input_detail.yml │ ├── pwm │ ├── deinit.yml │ ├── init.yml │ ├── pulse.yml │ ├── freq.yml │ ├── index.yml │ └── modulate.yml │ ├── uart │ ├── deinit.yml │ ├── send.yml │ ├── index.yml │ └── init.yml │ ├── logicanalyzer │ ├── deinit.yml │ ├── index.yml │ └── init.yml │ ├── switch │ ├── get.yml │ └── index.yml │ ├── measure │ ├── index.yml │ └── echo.yml │ ├── ws │ ├── index.yml │ └── reset_obniz_on_ws_disconnection.yml │ ├── ble │ ├── index.yml │ ├── central │ │ ├── scan_stop.yml │ │ ├── disconnect.yml │ │ ├── connect.yml │ │ ├── service_get.yml │ │ ├── scan_start.yml │ │ ├── characteristic_get.yml │ │ ├── descriptor_get.yml │ │ ├── characteristic_read.yml │ │ ├── characteristic_register_notify.yml │ │ ├── descriptor_read.yml │ │ ├── characteristic_unregister_notify.yml │ │ ├── characteristic_write.yml │ │ ├── descriptor_write.yml │ │ └── index.yml │ └── peripheral │ │ ├── servie_stop_all.yml │ │ ├── advertisement_stop.yml │ │ ├── advertisement_start.yml │ │ ├── servie_stop.yml │ │ ├── characteristic_notify.yml │ │ ├── index.yml │ │ ├── characteristic_read.yml │ │ ├── descriptor_read.yml │ │ ├── characteristic_write.yml │ │ └── descriptor_write.yml │ ├── system │ ├── reset.yml │ ├── reboot.yml │ ├── wait.yml │ ├── self_check.yml │ ├── ping.yml │ ├── keep_working_at_offline.yml │ └── index.yml │ ├── message │ ├── index.yml │ └── send.yml │ ├── display │ ├── clear.yml │ ├── text.yml │ ├── index.yml │ ├── raw.yml │ ├── qr.yml │ └── pin_assign.yml │ ├── ioanimation │ ├── index.yml │ ├── changeState.yml │ └── init.yml │ └── index.yml ├── .travis.yml ├── prototypetest ├── display.py ├── led.py ├── servo.py └── main.py ├── pytest.ini ├── _tools ├── Dockerfile ├── docker-compose.yml └── DOCKER-README.md ├── tox.ini ├── Pipfile ├── LICENSE └── setup.py /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/libs/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/libs/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/libs/hw/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/Camera/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/Light/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/Moving/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/libs/embeds/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/libs/embeds/ble/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/libs/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/PressureSensor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/libs/io_peripherals/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/libs/measurements/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | **/__pycache__ 2 | **/*.pyc -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/libs/io_peripherals/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/libs/measurements/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obniz/obniz/__version__.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.6.0" 2 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | graft obniz/obniz 2 | graft obniz/parts 3 | -------------------------------------------------------------------------------- /obniz/parts/Light/LED/led.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Light/LED/led.jpg -------------------------------------------------------------------------------- /obniz/parts/Light/LED/led_on.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Light/LED/led_on.jpg -------------------------------------------------------------------------------- /obniz/parts/Light/LED/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Light/LED/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/Light/LED/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Light/LED/wired.png -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 100 3 | exclude =.git 4 | import-order-style = google 5 | application-import-names = obniz 6 | -------------------------------------------------------------------------------- /obniz/parts/Moving/DCMotor/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/DCMotor/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/Moving/DCMotor/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/DCMotor/wired.png -------------------------------------------------------------------------------- /obniz/parts/Camera/ArduCAMMini/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Camera/ArduCAMMini/image.jpg -------------------------------------------------------------------------------- /obniz/parts/Camera/ArduCAMMini/wire.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Camera/ArduCAMMini/wire.jpg -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/ServoMotor/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/ServoMotor/wired.png -------------------------------------------------------------------------------- /obniz/parts/Moving/StepperMotor/wire.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/StepperMotor/wire.jpg -------------------------------------------------------------------------------- /obniz/parts/Moving/StepperMotor/wire.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/StepperMotor/wire.png -------------------------------------------------------------------------------- /obniz/parts/Moving/StepperMotor/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/StepperMotor/image.jpg -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/HC-SR04/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/DistanceSensor/HC-SR04/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/HC-SR04/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/DistanceSensor/HC-SR04/wired.png -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Button/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/Button/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Button/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/Button/wired.png -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/HC-SR505/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/HC-SR505/image.jpg -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/JoyStick/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/JoyStick/wired.png -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/servocable.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/ServoMotor/servocable.jpg -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/servomotor.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/ServoMotor/servomotor.gif -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/servomotor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/Moving/ServoMotor/servomotor.jpg -------------------------------------------------------------------------------- /obniz/parts/PressureSensor/FSR-40X/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/PressureSensor/FSR-40X/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/PressureSensor/FSR-40X/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/PressureSensor/FSR-40X/wired.png -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/KXR94-2050/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/KXR94-2050/image.jpg -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/pm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/Potentiometer/pm.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /dist/ 3 | /obniz.egg-info/ 4 | /.tox/ 5 | __pycache__/ 6 | .DS_Store 7 | .pytest_cache/ 8 | .vscode/ 9 | .venv/ 10 | test-*.py -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/image.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/DistanceSensor/GP2Y0A21YK0F/image.gif -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/DistanceSensor/GP2Y0A21YK0F/image.jpg -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/DistanceSensor/GP2Y0A21YK0F/wired.png -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/joystick.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/JoyStick/joystick.jpg -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/c_pm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/Potentiometer/c_pm.jpg -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/Potentiometer/wired.png -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/BME280/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/TemperatureSensor/i2c/BME280/image.jpg -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/SHT31/sht31.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/TemperatureSensor/i2c/SHT31/sht31.jpg -------------------------------------------------------------------------------- /json_schema/response/io/get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/io/get" 5 | 6 | type: boolean -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/analog/LM60/wired.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/TemperatureSensor/analog/LM60/wired.fzz -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/analog/LM60/wired.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/TemperatureSensor/analog/LM60/wired.png -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/BME280/image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/TemperatureSensor/i2c/BME280/image2.jpg -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/BME280/image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/TemperatureSensor/i2c/BME280/image3.jpg -------------------------------------------------------------------------------- /json_schema/request/spi/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/spi/deinit" 5 | 6 | type: "null" -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/joystick_pins.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/JoyStick/joystick_pins.jpg -------------------------------------------------------------------------------- /json_schema/request/ad/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ad/deinit" 5 | 6 | type: "null" 7 | -------------------------------------------------------------------------------- /json_schema/request/i2c/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/i2c/deinit" 5 | 6 | type: "null" 7 | -------------------------------------------------------------------------------- /json_schema/request/io/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/deinit" 5 | 6 | type: "null" 7 | -------------------------------------------------------------------------------- /json_schema/request/pwm/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/pwm/deinit" 5 | 6 | type: "null" 7 | -------------------------------------------------------------------------------- /json_schema/request/uart/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/uart/deinit" 5 | 6 | type: "null" 7 | -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/gp2y0a21yk0f.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/DistanceSensor/GP2Y0A21YK0F/gp2y0a21yk0f.fzz -------------------------------------------------------------------------------- /json_schema/request/io/output.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/output" 5 | 6 | type: boolean 7 | 8 | -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/potentiometer.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obniz/obniz-python-sdk/HEAD/obniz/parts/MovementSensor/Potentiometer/potentiometer.fzz -------------------------------------------------------------------------------- /json_schema/request/logicanalyzer/deinit.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/logicAnalyzer/deinit" 5 | 6 | type: "null" 7 | -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_helper.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | class BleHelper: 5 | @classmethod 6 | def uuid_filter(cls, uuid): 7 | return re.sub(r"[^0-9abcdef]", "", uuid.lower()) 8 | -------------------------------------------------------------------------------- /json_schema/response/ad/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ad" 5 | basePath : "ad0" 6 | 7 | "anyOf": 8 | - {$ref : "/response/ad/get"} -------------------------------------------------------------------------------- /json_schema/response/io/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/io" 5 | basePath : "io0" 6 | 7 | "anyOf": 8 | - {$ref : "/response/io/get"} -------------------------------------------------------------------------------- /json_schema/response/spi/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/spi" 5 | basePath : "spi0" 6 | 7 | "anyOf": 8 | - {$ref : "/response/spi/read"} -------------------------------------------------------------------------------- /json_schema/request/switch/get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/switch/get" 5 | related : "/response/switch/change" 6 | 7 | type: string 8 | enum : [get] -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: python 3 | python: 4 | - "3.6" 5 | - "3.7" 6 | 7 | install: 8 | - pip install pipenv 9 | - pipenv install --deploy --dev 10 | 11 | script: 12 | - pipenv run pytest 13 | -------------------------------------------------------------------------------- /json_schema/request/io/input.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/input" 5 | related : "/response/io/get" 6 | 7 | type : string 8 | enum: 9 | - get -------------------------------------------------------------------------------- /json_schema/response/uart/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/uart" 5 | basePath : "uart0" 6 | 7 | "anyOf": 8 | - {$ref : "/response/uart/receive"} -------------------------------------------------------------------------------- /json_schema/request/measure/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/measure" 5 | basePath : "measure" 6 | 7 | "anyOf": 8 | - {$ref : "/request/measure/echo"} -------------------------------------------------------------------------------- /json_schema/response/measure/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/measure" 5 | basePath : "measure" 6 | 7 | "anyOf": 8 | - {$ref : "/response/measure/echo"} -------------------------------------------------------------------------------- /json_schema/response/switch/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/switch" 5 | basePath : "switch" 6 | 7 | "anyOf": 8 | - {$ref : "/response/switch/change"} -------------------------------------------------------------------------------- /json_schema/response/system/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/system" 5 | basePath : "system" 6 | 7 | "anyOf": 8 | - {$ref : "/response/system/pong"} -------------------------------------------------------------------------------- /json_schema/response/uart/receive.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/uart/receive" 5 | 6 | type: object 7 | properties: 8 | data : {$ref: "/dataArray"} 9 | -------------------------------------------------------------------------------- /json_schema/request/ws/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ws" 5 | basePath : "ws" 6 | 7 | "anyOf": 8 | - {$ref : "/request/ws/reset_obniz_on_ws_disconnection"} -------------------------------------------------------------------------------- /json_schema/response/message/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/message" 5 | basePath : "message" 6 | 7 | "anyOf": 8 | - {$ref : "/response/message/receive"} -------------------------------------------------------------------------------- /json_schema/request/uart/send.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/uart/send" 5 | 6 | type: object 7 | required : [data] 8 | properties: 9 | data : {$ref : "/dataArray"} -------------------------------------------------------------------------------- /json_schema/request/pwm/init.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/pwm/init" 5 | 6 | type: object 7 | required : [io] 8 | properties: 9 | io : {$ref: "/pinSetting"} 10 | -------------------------------------------------------------------------------- /json_schema/response/logicanalyzer/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/logicAnalyzer" 5 | basePath : "logic_analyzer" 6 | 7 | "anyOf": 8 | - {$ref : "/response/logicAnalyzer/data"} -------------------------------------------------------------------------------- /json_schema/response/ws/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ws" 5 | basePath : "ws" 6 | 7 | "anyOf": 8 | - {$ref : "/response/ws/ready"} 9 | - {$ref : "/response/ws/redirect"} -------------------------------------------------------------------------------- /json_schema/request/ble/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble" 5 | basePath : "ble" 6 | 7 | "anyOf": 8 | - {$ref : "/request/ble/peripheral" } 9 | - {$ref : "/request/ble/central" } -------------------------------------------------------------------------------- /json_schema/response/ad/get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ad/get" 5 | 6 | type: number 7 | example : 3.300 8 | minimum : 0 9 | maximum : 5 10 | description : "current value (volt)" -------------------------------------------------------------------------------- /json_schema/response/i2c/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/i2c" 5 | basePath : "i2c0" 6 | 7 | "anyOf": 8 | - {$ref : "/response/i2c/master"} 9 | - {$ref : "/response/i2c/slave"} -------------------------------------------------------------------------------- /json_schema/response/logicanalyzer/data.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/logicAnalyzer/data" 5 | 6 | type: object 7 | required : [data] 8 | properties: 9 | data : {$ref : "/bitArray" } -------------------------------------------------------------------------------- /json_schema/response/spi/read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/spi/read" 5 | 6 | 7 | type: object 8 | required : [data] 9 | properties: 10 | data : {$ref : "/dataArray"} 11 | -------------------------------------------------------------------------------- /json_schema/response/debug/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/debug" 5 | basePath : "debug" 6 | 7 | "anyOf": 8 | - {$ref : "/response/debug/warning"} 9 | - {$ref : "/response/debug/error"} -------------------------------------------------------------------------------- /json_schema/request/ble/central/scan_stop.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/scan_stop" 5 | 6 | 7 | type: object 8 | required : [scan] 9 | properties: 10 | scan : 11 | type : "null" -------------------------------------------------------------------------------- /json_schema/response/ble/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble" 5 | basePath : "ble" 6 | 7 | 8 | "anyOf": 9 | - {$ref : "/response/ble/central"} 10 | - {$ref : "/response/ble/peripheral"} -------------------------------------------------------------------------------- /json_schema/request/system/reset.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system/reset" 5 | 6 | type: object 7 | required : [reset] 8 | properties: 9 | reset : 10 | type: boolean 11 | enum : [true] -------------------------------------------------------------------------------- /json_schema/request/message/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/message" 5 | basePath : "message" 6 | description : "send/receive with other obniz or WebHook" 7 | 8 | "anyOf": 9 | - {$ref : "/request/message/send"} -------------------------------------------------------------------------------- /json_schema/request/system/reboot.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system/reboot" 5 | 6 | type: object 7 | required : [reboot] 8 | properties: 9 | reboot : 10 | type: boolean 11 | enum : [true] -------------------------------------------------------------------------------- /json_schema/request/ad/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ad" 5 | basePath : "ad0" 6 | description : "available ad0~ad11" 7 | 8 | "anyOf": 9 | - {$ref : "/request/ad/get"} 10 | - {$ref : "/request/ad/deinit"} -------------------------------------------------------------------------------- /json_schema/request/display/clear.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/display/clear" 5 | 6 | 7 | type: object 8 | required : [clear] 9 | properties: 10 | clear: 11 | type: boolean 12 | enum: [true] -------------------------------------------------------------------------------- /json_schema/request/display/text.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/display/text" 5 | 6 | 7 | type: object 8 | required : [text] 9 | properties: 10 | text: 11 | type : string 12 | example : "Hello, obniz!" -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/servie_stop_all.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/service_stop_all" 5 | 6 | type: object 7 | required : [peripheral] 8 | properties: 9 | peripheral : 10 | type : "null" -------------------------------------------------------------------------------- /json_schema/request/system/wait.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system/wait" 5 | 6 | type: object 7 | required : [wait] 8 | properties: 9 | wait : 10 | type: integer 11 | description : "wait time (ms)" 12 | -------------------------------------------------------------------------------- /json_schema/request/uart/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/uart" 5 | basePath : "uart0" 6 | 7 | "anyOf": 8 | - {$ref : "/request/uart/init"} 9 | - {$ref : "/request/uart/send"} 10 | - {$ref : "/request/uart/deinit"} -------------------------------------------------------------------------------- /prototypetest/display.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | from obniz import Obniz 3 | 4 | async def onconnect(obniz): 5 | obniz.display.clear() 6 | obniz.display.print("Hello, World!") 7 | 8 | obniz = Obniz('15696177') 9 | obniz.onconnect = onconnect 10 | 11 | asyncio.get_event_loop().run_forever() -------------------------------------------------------------------------------- /obniz/__init__.py: -------------------------------------------------------------------------------- 1 | from .obniz import Obniz 2 | from .obniz.__version__ import __version__ as version 3 | 4 | # __copyright__ = "" 5 | __version__ = version 6 | # __license__ = "" 7 | __author__ = "yukisato" 8 | __author_email__ = "yuki@yuki-sato.com" 9 | __url__ = "https://obniz.io/" -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | filterwarnings = 3 | ; workaround for https://github.com/yaml/pyyaml/issues/202 4 | ignore:Using or importing the ABCs::yaml.constructor 5 | ; workaround for https://github.com/aaugustin/websockets/issues/514 6 | ignore:'with \(yield from lock\)'::websockets.protocol 7 | -------------------------------------------------------------------------------- /json_schema/response/ws/ready.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ws/ready" 5 | description : "all things ready" 6 | type: object 7 | required : [ready] 8 | properties: 9 | ready: 10 | type : boolean 11 | enum : [true] 12 | -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/advertisement_stop.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/advertisement_stop" 5 | 6 | 7 | type: object 8 | required : [advertisement] 9 | properties: 10 | advertisement : 11 | type : "null" -------------------------------------------------------------------------------- /json_schema/request/switch/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/switch" 5 | basePath : "switch" 6 | description : "the switch embed on obniz itself. If it's state is changed, notification will be fired." 7 | 8 | "anyOf": 9 | - {$ref : "/request/switch/get"} -------------------------------------------------------------------------------- /json_schema/request/pwm/pulse.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/pwm/pulse" 5 | 6 | type: object 7 | required : [pulse] 8 | properties: 9 | pulse: 10 | type: number 11 | minimum : 0 12 | description : "pulse width (ms)" 13 | -------------------------------------------------------------------------------- /json_schema/request/system/self_check.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system/selfCheck" 5 | description : "circuit IO check" 6 | 7 | type: object 8 | required : [self_check] 9 | properties: 10 | self_check : 11 | type: boolean 12 | enum : [true] -------------------------------------------------------------------------------- /json_schema/request/spi/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/spi" 5 | basePath : "spi0" 6 | description : "available spi0, spi1" 7 | 8 | "anyOf": 9 | - {$ref : "/request/spi/init_master"} 10 | - {$ref : "/request/spi/deinit"} 11 | - {$ref : "/request/spi/write"} -------------------------------------------------------------------------------- /json_schema/response/ble/central/scan_finish.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/scan_finish" 5 | 6 | 7 | type: object 8 | required : [scan_result_finish] 9 | properties: 10 | scan_result_finish : 11 | type : boolean 12 | enum : [true] -------------------------------------------------------------------------------- /json_schema/request/pwm/freq.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/pwm/freq" 5 | 6 | type: object 7 | required : [freq] 8 | properties: 9 | freq : 10 | type: integer 11 | description : "frequency (Hz)" 12 | minimum : 1 13 | maximum : 80000000 14 | -------------------------------------------------------------------------------- /json_schema/request/logicanalyzer/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/logicAnalyzer" 5 | basePath : "logic_analyzer" 6 | description : "Monitor io logic level changes by sampling io." 7 | 8 | "anyOf": 9 | - {$ref : "/request/logicAnalyzer/init" } 10 | - {$ref : "/request/logicAnalyzer/deinit" } -------------------------------------------------------------------------------- /json_schema/request/system/ping.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system/ping" 5 | response : "/response/system/pong" 6 | 7 | type: object 8 | required : [ping] 9 | properties: 10 | ping : 11 | type: object 12 | required : [key] 13 | properties: 14 | key : {$ref : "/dataArray"} -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/analog/LM60/__init__.py: -------------------------------------------------------------------------------- 1 | from .. import AnalogTemplatureSensor 2 | from attrdict import AttrDefault 3 | 4 | class LM60(AnalogTemplatureSensor): 5 | @staticmethod 6 | def info(): 7 | return AttrDefault(bool, {'name': 'LM60'}) 8 | 9 | def calc(self, voltage): 10 | return round(((voltage - 0.424) / 0.00625) * 10) / 10.0 -------------------------------------------------------------------------------- /json_schema/request/system/keep_working_at_offline.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system/keepWorkingAtOffline" 5 | description : "reset obniz when obniz gone to offline." 6 | 7 | type: object 8 | required : [keep_working_at_offline] 9 | properties: 10 | keep_working_at_offline : 11 | type: boolean 12 | -------------------------------------------------------------------------------- /json_schema/request/ws/reset_obniz_on_ws_disconnection.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ws/reset_obniz_on_ws_disconnection" 5 | 6 | 7 | type: object 8 | required : [reset_obniz_on_ws_disconnection] 9 | properties: 10 | reset_obniz_on_ws_disconnection : 11 | type : boolean 12 | default : false 13 | -------------------------------------------------------------------------------- /json_schema/request/display/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/display" 5 | basePath : "display" 6 | 7 | "anyOf": 8 | - {$ref : "/request/display/text"} 9 | - {$ref : "/request/display/clear"} 10 | - {$ref : "/request/display/qr"} 11 | - {$ref : "/request/display/raw"} 12 | - {$ref : "/request/display/pin_assign"} -------------------------------------------------------------------------------- /json_schema/response/system/pong.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/system/pong" 5 | desccription : "pong response with same key of ping request" 6 | 7 | type: object 8 | required : [pong] 9 | properties: 10 | pong : 11 | type: object 12 | required : [key] 13 | properties: 14 | key : {$ref : "/dataArray"} -------------------------------------------------------------------------------- /json_schema/request/pwm/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/pwm" 5 | basePath : "pwm0" 6 | description : "available 0 to 5" 7 | 8 | "anyOf": 9 | - {$ref : "/request/pwm/init"} 10 | - {$ref : "/request/pwm/freq"} 11 | - {$ref : "/request/pwm/pulse"} 12 | - {$ref : "/request/pwm/modulate"} 13 | - {$ref : "/request/pwm/deinit"} -------------------------------------------------------------------------------- /_tools/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3 2 | USER root 3 | 4 | RUN apt-get update 5 | RUN apt-get -y install locales && \ 6 | localedef -f UTF-8 -i ja_JP ja_JP.UTF-8 7 | ENV LANG ja_JP.UTF-8 8 | ENV LANGUAGE ja_JP:ja 9 | ENV LC_ALL ja_JP.UTF-8 10 | ENV TZ JST-9 11 | ENV TERM xterm 12 | 13 | RUN apt-get install -y vim less 14 | RUN pip install --upgrade pip 15 | RUN pip install --upgrade setuptools 16 | RUN pip install pipenv -------------------------------------------------------------------------------- /json_schema/request/i2c/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/i2c" 5 | basePath : "i2c0" 6 | description : "available only i2c0" 7 | "anyOf": 8 | - {$ref : "/request/i2c/init_master"} 9 | - {$ref : "/request/i2c/init_slave"} 10 | - {$ref : "/request/i2c/write"} 11 | - {$ref : "/request/i2c/read"} 12 | - {$ref : "/request/i2c/deinit"} -------------------------------------------------------------------------------- /json_schema/request/io/output_detail.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/output_detail" 5 | 6 | type: object 7 | required : [direction, value] 8 | properties: 9 | direction : 10 | type: string 11 | enum: 12 | - output 13 | 14 | value : 15 | type: boolean 16 | -------------------------------------------------------------------------------- /json_schema/request/spi/write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/spi/write" 5 | related : "/response/spi/read" 6 | 7 | type: object 8 | required : [data, read] 9 | properties: 10 | data : {$ref : "/dataArray1024"} 11 | read : 12 | type: boolean 13 | default : true 14 | description : "If false, write without receive" 15 | 16 | -------------------------------------------------------------------------------- /json_schema/request/ble/central/disconnect.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/disconnect" 5 | 6 | 7 | type: object 8 | required : [disconnect] 9 | properties: 10 | disconnect : 11 | type : object 12 | required: [address] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress" } -------------------------------------------------------------------------------- /json_schema/request/io/pull_type.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/pull_type" 5 | 6 | type: object 7 | required : [pull_type] 8 | properties: 9 | pull_type: 10 | type: string 11 | enum: 12 | - pull-up5v 13 | - pull-up3v 14 | - pull-down 15 | - float 16 | -------------------------------------------------------------------------------- /json_schema/response/i2c/master.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/i2c/master" 5 | 6 | 7 | type: object 8 | required : [mode, address, data] 9 | properties: 10 | mode : 11 | type : string 12 | enum : [master] 13 | 14 | address : 15 | type: integer 16 | minimum : 0 17 | maximum : 1023 18 | 19 | data : {$ref : "/dataArray" } 20 | -------------------------------------------------------------------------------- /json_schema/request/ad/get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ad/get" 5 | related : "/response/ad/get" 6 | desription : "enable & start ad module at io." 7 | 8 | type: object 9 | required : [stream] 10 | properties: 11 | stream : 12 | type : boolean 13 | default : false 14 | description : "true to continuous notifying on voltage change." -------------------------------------------------------------------------------- /json_schema/request/system/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/system" 5 | basePath : "system" 6 | 7 | "anyOf": 8 | - {$ref : "/request/system/wait"} 9 | - {$ref : "/request/system/reset"} 10 | - {$ref : "/request/system/reboot"} 11 | - {$ref : "/request/system/selfCheck"} 12 | - {$ref : "/request/system/keepWorkingAtOffline"} 13 | - {$ref : "/request/system/ping"} -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # tox (https://tox.readthedocs.io/) is a tool for running tests 2 | # in multiple virtualenvs. This configuration file will run the 3 | # test suite on all supported python versions. To use it, "pip install tox" 4 | # and then run "tox" from this directory. 5 | 6 | [tox] 7 | envlist = py36, py37 8 | 9 | [testenv] 10 | deps = 11 | pytest 12 | pytest-mock 13 | asynctest 14 | pyyaml 15 | jsonschema 16 | commands = 17 | pytest 18 | -------------------------------------------------------------------------------- /json_schema/request/io/output_type.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/output_type" 5 | 6 | type: object 7 | required : [output_type] 8 | properties: 9 | output_type: 10 | type: string 11 | enum: 12 | - push-pull5v 13 | - push-pull3v 14 | - open-drain 15 | description : "drive type" -------------------------------------------------------------------------------- /json_schema/request/ioanimation/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ioAnimation" 5 | basePath : "io.animation" 6 | description : "io animation is hardware acceleration for serial sequence change of io. now 'loop' animation is available. it loop io changes regarding json array." 7 | 8 | 9 | 10 | "anyOf": 11 | - {$ref : "/request/ioAnimation/init"} 12 | - {$ref : "/request/ioAnimation/changeState"} -------------------------------------------------------------------------------- /json_schema/request/ble/central/connect.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/connect" 5 | related : "/response/ble/central/status_update" 6 | 7 | 8 | type: object 9 | required : [connect] 10 | properties: 11 | connect : 12 | type : object 13 | required: [address] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress" } 17 | -------------------------------------------------------------------------------- /json_schema/response/ws/redirect.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ws/redirect" 5 | description : "If the server required you to connect other endpoint to communicate with your obniz. This json will be sent." 6 | type: object 7 | required : [redirect] 8 | properties: 9 | redirect: 10 | type : string 11 | example : "wss://ws1.obniz.io" 12 | description : "The url you should redirect to." -------------------------------------------------------------------------------- /json_schema/request/ble/central/service_get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/service_get" 5 | related : "/response/ble/central/service_get" 6 | 7 | 8 | type: object 9 | required : [get_services] 10 | properties: 11 | get_services : 12 | type : object 13 | required: [address] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress"} 17 | -------------------------------------------------------------------------------- /json_schema/request/message/send.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/message/send" 5 | related : "/response/message/receive" 6 | 7 | type: object 8 | additionalProperties: false 9 | required : [data, to] 10 | properties: 11 | data : 12 | example : "button pressed" 13 | description : "All type of data is pass." 14 | to : 15 | type : array 16 | minItems : 1 17 | items : { $ref : "/obnizId" } -------------------------------------------------------------------------------- /json_schema/response/ble/central/service_get_finish.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/service_get_finish" 5 | 6 | 7 | type: object 8 | required : [get_service_result_finish] 9 | properties: 10 | get_service_result_finish : 11 | type : object 12 | required: [address] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | -------------------------------------------------------------------------------- /json_schema/response/switch/change.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/switch/change" 5 | desccription : "value changes are always notified." 6 | 7 | type: object 8 | required : [state] 9 | properties: 10 | state : 11 | type: string 12 | enum : [none, push, left, right] 13 | 14 | action : 15 | type : string 16 | enum : [get] 17 | description : "this is optional and added when user request" 18 | -------------------------------------------------------------------------------- /json_schema/response/i2c/slave.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/i2c/slave" 5 | 6 | 7 | type: object 8 | required : [mode, address, is_fragmented, data] 9 | properties: 10 | mode : 11 | type : string 12 | enum : [slave] 13 | 14 | address : 15 | type: integer 16 | minimum : 0 17 | maximum : 1023 18 | 19 | is_fragmented: 20 | type: boolean 21 | 22 | data : {$ref : "/dataArray" } 23 | -------------------------------------------------------------------------------- /json_schema/response/debug/error.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/debug/error" 5 | desccription : "global error" 6 | 7 | type: object 8 | required : [error] 9 | properties: 10 | 11 | error : 12 | type : object 13 | 14 | additionalProperties: true 15 | properties: 16 | message : 17 | description : "readable message" 18 | type : string 19 | example : "voltage down" 20 | -------------------------------------------------------------------------------- /json_schema/response/message/receive.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/message/receive" 5 | related : "/request/message/send" 6 | 7 | type: object 8 | required : [data, from] 9 | properties: 10 | data : 11 | example : "button pressed" 12 | description : "All type of data is pass." 13 | from : 14 | type : [string, "null"] 15 | example : "1234-5678" 16 | description : "From obniz id. Null is used when WebHook message." -------------------------------------------------------------------------------- /json_schema/request/ble/central/scan_start.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/scan_start" 5 | related : 6 | - "/response/ble/central/scan" 7 | - "/response/ble/central/scan_finish" 8 | 9 | 10 | type: object 11 | required : [scan] 12 | properties: 13 | scan : 14 | type : object 15 | additionalProperties: false 16 | properties: 17 | duration : 18 | type : integer 19 | default : 30 20 | -------------------------------------------------------------------------------- /json_schema/request/io/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io" 5 | basePath : "io0" 6 | description : "General purpose IO available on each io (io0 to io11)." 7 | 8 | "anyOf": 9 | - {$ref : "/request/io/input"} 10 | - {$ref : "/request/io/input_detail"} 11 | - {$ref : "/request/io/output"} 12 | - {$ref : "/request/io/output_detail"} 13 | - {$ref : "/request/io/output_type"} 14 | - {$ref : "/request/io/pull_type"} 15 | - {$ref : "/request/io/deinit"} -------------------------------------------------------------------------------- /json_schema/response/debug/warning.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/debug/warning" 5 | desccription : "global warnings" 6 | 7 | type: object 8 | required : [warning] 9 | properties: 10 | 11 | warning : 12 | type : object 13 | 14 | additionalProperties: true 15 | properties: 16 | message : 17 | description : "readable message" 18 | type : string 19 | example : "unknown command" 20 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/service_get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/service_get" 5 | 6 | 7 | type: object 8 | required : [get_service_result] 9 | properties: 10 | get_service_result : 11 | type : object 12 | required: [address,service_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | -------------------------------------------------------------------------------- /json_schema/request/io/input_detail.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/io/input_detail" 5 | related : "/response/io/get" 6 | 7 | type: object 8 | required : [direction] 9 | properties: 10 | direction : 11 | type: string 12 | enum: 13 | - input 14 | 15 | stream : 16 | type: boolean 17 | default: false 18 | description : "enable stream callback when value change" -------------------------------------------------------------------------------- /json_schema/request/display/raw.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/display/raw" 5 | description : "1 bit represents 1 dot. 1=white, 0=black. 1 byte is part of one line. Order is same like.
6 | {1byte} {2byte} {3byte}...{16byte}
7 | {17byte} {18byte} {19byte}...
8 | .....
9 | .....................{1024byte}" 10 | 11 | type: object 12 | required : [raw] 13 | properties: 14 | raw : {$ref : "/imageData128x64"} 15 | -------------------------------------------------------------------------------- /_tools/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | obniz_python: 4 | restart: always 5 | build: . 6 | container_name: 'obniz_python' 7 | working_dir: '/root/opt' 8 | tty: true 9 | volumes: 10 | - ../:/root/opt 11 | ports: 12 | - "8080:8080" 13 | - "8081:8081" 14 | obniz_python_prototypetest: 15 | restart: always 16 | build: . 17 | container_name: 'obniz_python_prototypetest' 18 | working_dir: '/root/opt' 19 | tty: true 20 | volumes: 21 | - ../prototypetest:/root/opt/prototypetest 22 | -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/advertisement_start.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/advertisement_start" 5 | related : "/response/ble/peripheral/status" 6 | 7 | 8 | type: object 9 | required : [advertisement] 10 | properties: 11 | advertisement : 12 | type : object 13 | required : [adv_data] 14 | additionalProperties: false 15 | properties: 16 | adv_data : {$ref : "/bleAdvertiseData"} 17 | scan_resp : {$ref : "/bleAdvertiseData"} -------------------------------------------------------------------------------- /json_schema/request/display/qr.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/display/qr" 5 | 6 | 7 | type: object 8 | required : [qr] 9 | properties: 10 | qr : 11 | type: object 12 | required: [text] 13 | additionalProperties: false 14 | properties: 15 | text : 16 | type : string 17 | example : "Hello, obniz!" 18 | correction : 19 | type : string 20 | enum : [L, M, Q, H] 21 | default : M -------------------------------------------------------------------------------- /json_schema/request/ble/central/characteristic_get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/characteristic_get" 5 | related : "/response/ble/central/characteristic_get" 6 | 7 | 8 | type: object 9 | required : [get_characteristics] 10 | properties: 11 | get_characteristics : 12 | type : object 13 | required: [address,service_uuid] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress"} 17 | service_uuid : {$ref : "/uuid"} 18 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_get_finish.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_get_finish" 5 | 6 | 7 | type: object 8 | required : [get_characteristic_result_finish] 9 | properties: 10 | get_characteristic_result_finish : 11 | type : object 12 | required: [address,service_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/status_update.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/status_update" 5 | 6 | 7 | type: object 8 | required : [status_update] 9 | properties: 10 | status_update : 11 | type : object 12 | required: [address, status] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | status : 17 | type: string 18 | enum : [connected, disconnected] 19 | -------------------------------------------------------------------------------- /json_schema/response/measure/echo.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/measure/echo" 5 | 6 | type: object 7 | required : [echo] 8 | properties: 9 | echo : 10 | type : array 11 | minItesm : 1 12 | items : 13 | type: object 14 | required : [edge, timing] 15 | properties: 16 | edge : 17 | type : boolean 18 | description : "rising = true" 19 | timing : 20 | type : number 21 | description : "milliseconds from end of pulse" 22 | 23 | -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/servie_stop.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/service_stop" 5 | 6 | type: object 7 | required : [peripheral] 8 | properties: 9 | peripheral : 10 | type : object 11 | required : [stop_service] 12 | properties: 13 | stop_service : 14 | type : object 15 | required: [service_uuid] 16 | additionalProperties: false 17 | properties: 18 | service_uuid : {$ref : "/uuid"} 19 | 20 | -------------------------------------------------------------------------------- /json_schema/request/i2c/write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/i2c/write" 5 | description : "if address over 0b01111111; then address treated as 10bit address automatically. or specify address_bits: 10 to force 10bit address mode." 6 | 7 | type: object 8 | required : [address, data] 9 | properties: 10 | address : 11 | type: integer 12 | minimum : 0 13 | maximum : 1023 14 | 15 | address_bits: 16 | type: integer 17 | enum: [7] 18 | default: 7 19 | 20 | data : {$ref: "/dataArray1024"} -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from .utils import release_obnize, setup_obniz, receive_json, assert_send 4 | 5 | 6 | @pytest.fixture(scope="function") 7 | def obniz(mocker): 8 | obniz = setup_obniz(mocker) 9 | receive_json(obniz, [{'ws': {'ready': True, 'obniz': {'hw': 'obnizb1', 'firmware': '2.0.2'}}}]) 10 | assert_send(obniz, [{'ws': {'reset_obniz_on_ws_disconnection': True}}]) 11 | 12 | yield obniz 13 | release_obnize(obniz) 14 | 15 | @pytest.fixture(scope="function") 16 | def uninitialized_obniz(mocker): 17 | obniz = setup_obniz(mocker) 18 | yield obniz 19 | release_obnize(obniz) 20 | 21 | -------------------------------------------------------------------------------- /json_schema/request/ble/central/descriptor_get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/descriptor_get" 5 | related : "/response/ble/central/descriptor_get" 6 | 7 | 8 | type: object 9 | required : [get_descriptors] 10 | properties: 11 | get_descriptors : 12 | type : object 13 | required: [address,service_uuid,characteristic_uuid] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress"} 17 | service_uuid : {$ref : "/uuid"} 18 | characteristic_uuid : {$ref : "/uuid"} 19 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/descriptor_get_finish.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/descriptor_get_finish" 5 | 6 | 7 | type: object 8 | required : [get_descriptor_result_finish] 9 | properties: 10 | get_descriptor_result_finish : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress" } 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} -------------------------------------------------------------------------------- /json_schema/request/ble/central/characteristic_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/characteristic_read" 5 | related : "/response/ble/central/characteristic_read" 6 | 7 | type: object 8 | required : [read_characteristic] 9 | properties: 10 | read_characteristic : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | -------------------------------------------------------------------------------- /json_schema/request/i2c/init_master.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/i2c/init_master" 5 | description : "internal pull-up is available. But, We recommend use external pull-up resistor." 6 | 7 | type: object 8 | required : [mode, sda, scl, clock] 9 | uniqueKeys : [sda,scl] 10 | properties: 11 | mode : 12 | type : string 13 | enum : [master] 14 | 15 | sda : {$ref: "/pinSetting"} 16 | scl : {$ref: "/pinSetting"} 17 | 18 | clock: 19 | type: integer 20 | description : "frequency (Hz)" 21 | minimum : 1 22 | maximum : 1000000 #1MHz 23 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/descriptor_get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/descriptor_get" 5 | 6 | 7 | type: object 8 | required : [get_descriptor_result] 9 | properties: 10 | get_descriptor_result : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid, descriptor_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress" } 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | descriptor_uuid : {$ref : "/uuid"} -------------------------------------------------------------------------------- /obniz/obniz/libs/hw/index.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | 4 | class HW: 5 | @staticmethod 6 | def get_definition_for(hw): 7 | if hw == 'obnizb1': 8 | with open(os.path.join(os.path.dirname(__file__), 'obnizb1.json')) as f: 9 | hw_obj = json.load(f) 10 | elif hw == 'obnizb2': 11 | with open(os.path.join(os.path.dirname(__file__), 'obnizb2.json')) as f: 12 | hw_obj = json.load(f) 13 | elif hw == 'esp32w': 14 | with open(os.path.join(os.path.dirname(__file__), 'esp32w.json')) as f: 15 | hw_obj = json.load(f) 16 | else: 17 | hw_obj = None 18 | return hw_obj 19 | -------------------------------------------------------------------------------- /json_schema/request/i2c/read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/i2c/read" 5 | description : "if address over 0b01111111; then address treated as 10bit address automatically. or specify address_bits: 10 to force 10bit address mode." 6 | related : "/response/i2c/master" 7 | 8 | type: object 9 | required : [address, read] 10 | properties: 11 | address : 12 | type: integer 13 | minimum : 0 14 | maximum : 1023 15 | 16 | address_bits: 17 | type: integer 18 | enum: [7] 19 | default: 7 20 | 21 | read : 22 | type: integer 23 | minimum : 0 24 | maximum : 1024 25 | -------------------------------------------------------------------------------- /json_schema/request/spi/init_master.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/spi/init_master" 5 | description : "clk, miso, mosi are optional, but at least one are required" 6 | 7 | type: object 8 | required : [mode, clock] # 9 | uniqueKeys : [mosi,miso,clk] 10 | properties: 11 | mode : 12 | type : string 13 | enum : [master] 14 | clk : {$ref: "/pinSetting"} 15 | mosi : {$ref: "/pinSetting"} 16 | miso : {$ref: "/pinSetting"} 17 | clock : 18 | type: integer 19 | default : 115200 20 | minimum : 1 21 | maximum : 26000000 # 26MHz 22 | desription : "frequency (Hz)" 23 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_notify.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_notify" 5 | 6 | 7 | type: object 8 | required : [nofity_characteristic] 9 | properties: 10 | nofity_characteristic : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid, data] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | data : {$ref : "/dataArray" } 19 | 20 | -------------------------------------------------------------------------------- /json_schema/request/ble/central/characteristic_register_notify.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/characteristic_register_notify" 5 | related : "/response/ble/central/characteristic_register_notify" 6 | 7 | type: object 8 | required : [register_notify_characteristic] 9 | properties: 10 | register_notify_characteristic : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | -------------------------------------------------------------------------------- /json_schema/request/ble/central/descriptor_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/descriptor_read" 5 | related : "/response/ble/central/descriptor_read" 6 | 7 | 8 | type: object 9 | required : [read_descriptor] 10 | properties: 11 | read_descriptor : 12 | type : object 13 | required: [address,service_uuid,characteristic_uuid,descriptor_uuid] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress"} 17 | service_uuid : {$ref : "/uuid"} 18 | characteristic_uuid : {$ref : "/uuid"} 19 | descriptor_uuid : {$ref : "/uuid"} 20 | -------------------------------------------------------------------------------- /json_schema/request/ioanimation/changeState.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ioAnimation/changeState" 5 | 6 | type: object 7 | required : [animation] 8 | properties: 9 | animation : 10 | type: object 11 | required : [name, status] 12 | additionalProperties: false 13 | properties: 14 | name : 15 | type: string 16 | example : "animation-1" 17 | minLength: 1 18 | maxLength: 254 19 | 20 | status : 21 | type: string 22 | enum : 23 | - pause 24 | - resume 25 | 26 | -------------------------------------------------------------------------------- /json_schema/request/pwm/modulate.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/pwm/modulate" 5 | 6 | type: object 7 | required : [modulate] 8 | properties: 9 | modulate: 10 | type: object 11 | required : [type, symbol_length, data] 12 | additionalProperties: false 13 | properties: 14 | type : 15 | type : string 16 | enum : 17 | - am 18 | 19 | symbol_length : 20 | type : number 21 | minimum : 0.05 22 | maximum : 1000 23 | description : "symbol width (ms)" 24 | 25 | data : {$ref : "/bitArray" } -------------------------------------------------------------------------------- /json_schema/request/ble/central/characteristic_unregister_notify.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/characteristic_unregister_notify" 5 | related : "/response/ble/central/characteristic_unregister_notify" 6 | 7 | type: object 8 | required : [unregister_notify_characteristic] 9 | properties: 10 | unregister_notify_characteristic : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | -------------------------------------------------------------------------------- /prototypetest/led.py: -------------------------------------------------------------------------------- 1 | 2 | import asyncio 3 | from obniz import Obniz 4 | 5 | async def onconnect(obniz): 6 | print('connected') 7 | led = obniz.wired('LED',{ "anode":0, "cathode":1}) 8 | led.on() 9 | await obniz.wait(500) 10 | led.off() 11 | await obniz.wait(500) 12 | led.on() 13 | await obniz.wait(500) 14 | led.off() 15 | await obniz.wait(500) 16 | led.on() 17 | await obniz.wait(500) 18 | led.off() 19 | await obniz.wait(500) 20 | 21 | obniz = Obniz('15696177') 22 | obniz.debugprint = True 23 | obniz.onconnect = onconnect 24 | 25 | asyncio.get_event_loop().run_forever() 26 | 27 | 28 | print(Obniz.obniz.version) 29 | 30 | 31 | -------------------------------------------------------------------------------- /obniz/obniz/obniz_uis.py: -------------------------------------------------------------------------------- 1 | from .obniz_system_methods import ObnizSystemMethods 2 | 3 | 4 | class ObnizUIs(ObnizSystemMethods): 5 | def __init__(self, id, options): 6 | super().__init__(id, options) 7 | 8 | def _is_valid_obniz_id(self, string): 9 | if type(string) is not str or len(string) < 8: 10 | return False 11 | 12 | string = string.replace("-", "") 13 | 14 | try: 15 | int(string) 16 | except ValueError: 17 | return False 18 | 19 | return True 20 | 21 | def wsconnect(self, desired_server=None): 22 | if not self._is_valid_obniz_id(self.id): 23 | self.error("invalid obniz id") 24 | return 25 | 26 | super().wsconnect(desired_server) 27 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_write" 5 | 6 | 7 | type: object 8 | required : [write_characteristic_result] 9 | properties: 10 | write_characteristic_result : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid, result] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress" } 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | result : 19 | type : string 20 | enum : [success , failed] -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral" 5 | basePath : "ble" 6 | 7 | "anyOf": 8 | - {$ref : "/response/ble/peripheral/status"} 9 | - {$ref : "/response/ble/peripheral/characteristic_read"} 10 | - {$ref : "/response/ble/peripheral/characteristic_write"} 11 | - {$ref : "/response/ble/peripheral/characteristic_notify_read"} 12 | - {$ref : "/response/ble/peripheral/characteristic_notify_write"} 13 | - {$ref : "/response/ble/peripheral/descriptor_read"} 14 | - {$ref : "/response/ble/peripheral/descriptor_write"} 15 | - {$ref : "/response/ble/peripheral/descriptor_notify_read"} 16 | - {$ref : "/response/ble/peripheral/descriptor_notify_write"} -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/characteristic_notify.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/characteristic_notify" 5 | description : "notify characteristic for connected device" 6 | 7 | type: object 8 | required : [peripheral] 9 | properties: 10 | peripheral : 11 | type : object 12 | required : [notify_characteristic] 13 | properties: 14 | notify_characteristic : 15 | type : object 16 | required: [service_uuid,characteristic_uuid] 17 | additionalProperties: false 18 | properties: 19 | service_uuid : {$ref : "/uuid"} 20 | characteristic_uuid : {$ref : "/uuid"} 21 | 22 | -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral" 5 | basePath : "ble" 6 | description : "use obniz as peripheral" 7 | 8 | "anyOf": 9 | - {$ref : "/request/ble/peripheral/advertisement_start"} 10 | - {$ref : "/request/ble/peripheral/advertisement_stop"} 11 | - {$ref : "/request/ble/peripheral/service_start"} 12 | - {$ref : "/request/ble/peripheral/service_stop"} 13 | - {$ref : "/request/ble/peripheral/service_stop_all"} 14 | - {$ref : "/request/ble/peripheral/characteristic_read"} 15 | - {$ref : "/request/ble/peripheral/characteristic_write"} 16 | - {$ref : "/request/ble/peripheral/descriptor_read"} 17 | - {$ref : "/request/ble/peripheral/descriptor_write"} -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_register_notify.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_register_notify" 5 | related : "/request/ble/central/characteristic_register_notify" 6 | 7 | type: object 8 | required : [characteristic_register_notify_result] 9 | properties: 10 | characteristic_register_notify_result : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid,result] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | result : 19 | type : boolean 20 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_unregister_notify.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_unregister_notify" 5 | related : "/request/ble/central/characteristic_unregister_notify" 6 | 7 | type: object 8 | required : [unregister_notify_characteristic_result] 9 | properties: 10 | unregister_notify_characteristic_result : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid,result] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | result : 19 | type : boolean 20 | 21 | -------------------------------------------------------------------------------- /json_schema/request/ble/central/characteristic_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/characteristic_write" 5 | related : "/response/ble/central/characteristic_write" 6 | 7 | 8 | type: object 9 | required : [write_characteristic] 10 | properties: 11 | write_characteristic : 12 | type : object 13 | required: [address,service_uuid,characteristic_uuid,data] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress"} 17 | service_uuid : {$ref : "/uuid"} 18 | characteristic_uuid : {$ref : "/uuid"} 19 | data : {$ref : "/dataArray"} 20 | needResponse : 21 | type : boolean 22 | default : true -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_read" 5 | 6 | 7 | type: object 8 | required : [read_characteristic_result] 9 | properties: 10 | read_characteristic_result : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid,result, data] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | result : 19 | type : string 20 | enum : [success, failed] 21 | data : {$ref : "/dataArray" } 22 | 23 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/descriptor_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/descriptor_write" 5 | 6 | 7 | type: object 8 | required : [write_descriptor_result] 9 | properties: 10 | write_descriptor_results : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid,descriptor_uuid, result] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress" } 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | descriptor_uuid : {$ref : "/uuid"} 19 | result : 20 | type : string 21 | enum : [success , failed] -------------------------------------------------------------------------------- /obniz/parts/PressureSensor/FSR-40X/README-ja.md: -------------------------------------------------------------------------------- 1 | # Pressure Sensor - FSR40X 2 | 圧力センサFSR40Xです。センサで取得した圧力を知ることができます。 3 | 4 | ## wired(obniz, {pin0, pin1}) 5 | Obnizに圧力センサをつなぎます。 6 | pin0,pin1を圧力センサへ接続してください。 7 | 8 | ![](./wired.png) 9 | ```Python 10 | # Python Example 11 | pressure = obniz.wired("FSR40X", {"pin0" 0, "pin1": 1}) 12 | ``` 13 | 14 | ## onchange = callback(press) 15 | 圧力センサの値に変化があった場合にcallback関数を呼び出します。 16 | 17 | ```Python 18 | # Python Example 19 | pressure = obniz.wired("FSR40X", {"pin0" 0, "pin1": 1}) 20 | def onchange(press): 21 | print(press) 22 | pressure.onchange = onchange 23 | ``` 24 | 25 | ## [await]get_wait() 26 | 圧力センサの値を一度だけ取得します 27 | 28 | ```Python 29 | # Python Example 30 | pressure = obniz.wired("FSR40X", {"pin0" 0, "pin1": 1}) 31 | press = await pressure.get_wait() 32 | print(press) 33 | ``` 34 | -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/characteristic_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/characteristic_read" 5 | description : "read characteristic on own service" 6 | related : "/response/ble/peripheral/characteristic_read" 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral : 12 | type : object 13 | required : [read_characteristic] 14 | properties: 15 | read_characteristic : 16 | type : object 17 | required: [service_uuid,characteristic_uuid] 18 | additionalProperties: false 19 | properties: 20 | service_uuid : {$ref : "/uuid"} 21 | characteristic_uuid : {$ref : "/uuid"} 22 | 23 | -------------------------------------------------------------------------------- /prototypetest/servo.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import time 3 | from obniz import Obniz 4 | 5 | 6 | # min 0.5 max 2.4 middle 1.4 7 | async def onconnect(obniz): 8 | obniz.io0.output(False) 9 | obniz.io1.output(True) 10 | 11 | pwm = obniz.get_free_pwm() 12 | pwm.start({"io": 2}) 13 | pwm.freq(100) 14 | pwm.duty(50) 15 | 16 | def read_state(state): 17 | print(state) 18 | if state == "right": 19 | val = 1.0 20 | pwm.pulse(val) 21 | elif state == "left": 22 | val = 1.8 23 | pwm.pulse(val) 24 | elif state == "none": 25 | val = 1.4 26 | pwm.pulse(val) 27 | 28 | obniz.switch.onchange = read_state 29 | 30 | 31 | obniz = Obniz('15696177') 32 | obniz.onconnect = onconnect 33 | 34 | asyncio.get_event_loop().run_forever() -------------------------------------------------------------------------------- /json_schema/request/ble/central/descriptor_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central/descriptor_write" 5 | related : "/response/ble/central/descriptor_write" 6 | 7 | 8 | type: object 9 | required : [write_descriptor] 10 | properties: 11 | write_descriptor : 12 | type : object 13 | required: [address, service_uuid, characteristic_uuid, descriptor_uuid,data] 14 | additionalProperties: false 15 | properties: 16 | address : {$ref : "/deviceAddress"} 17 | service_uuid : {$ref : "/uuid"} 18 | characteristic_uuid : {$ref : "/uuid"} 19 | descriptor_uuid : {$ref : "/uuid"} 20 | data : {$ref : "/dataArray"} 21 | needResponse : 22 | type : boolean 23 | default : true 24 | -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/status.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/status" 5 | 6 | 7 | type: object 8 | required : [peripheral] 9 | properties: 10 | peripheral: 11 | type : object 12 | additionalProperties: false 13 | required : [connection_status] 14 | properties: 15 | connection_status : 16 | type : object 17 | required: [address, status] 18 | additionalProperties: false 19 | properties: 20 | address : {$ref : "/deviceAddress"} 21 | status : 22 | type : string 23 | enum : [connected , disconnected] -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/HC-SR505/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | import asyncio 3 | 4 | class HCSR505: 5 | def __init__(self): 6 | self.keys = ['vcc', 'gnd', 'signal'] 7 | self.required_keys = ['signal'] 8 | 9 | @staticmethod 10 | def info(): 11 | return AttrDefault(bool, {'name': 'HC-SR505'}) 12 | 13 | def wired(self, obniz): 14 | self.obniz = obniz 15 | self.io_signal = obniz.get_io(*[self.params.signal]) 16 | obniz.set_vcc_gnd(*[self.params.vcc, self.params.gnd, '5v']) 17 | def setval(val): 18 | if self.onchange: 19 | self.onchange(val) 20 | self.io_signal.input(setval) 21 | 22 | def get_wait(self): 23 | async def getval(): 24 | val = await self.io_signal.input_wait() 25 | return val 26 | return getval() -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/descriptor_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/descriptor_read" 5 | related : "/response/ble/peripheral/descriptor_read" 6 | 7 | description : "read descriptor on own service" 8 | 9 | type: object 10 | required : [peripheral] 11 | properties: 12 | peripheral : 13 | type : object 14 | required : [read_descriptor] 15 | properties: 16 | read_descriptor : 17 | type : object 18 | required: [service_uuid,characteristic_uuid] 19 | additionalProperties: false 20 | properties: 21 | service_uuid : {$ref : "/uuid"} 22 | characteristic_uuid : {$ref : "/uuid"} 23 | descriptor_uuid : {$ref : "/uuid"} 24 | 25 | 26 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/descriptor_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/descriptor_read" 5 | 6 | 7 | type: object 8 | required : [read_descriptor_result] 9 | properties: 10 | read_descriptor_results : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid,descriptor_uuid, result, data] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress" } 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | descriptor_uuid : {$ref : "/uuid"} 19 | result : 20 | type : string 21 | enum : [success, failed] 22 | data : {$ref : "/dataArray" } -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/characteristic_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/characteristic_write" 5 | description : "write characteristic on own service" 6 | related : "/response/ble/peripheral/characteristic_write" 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral : 12 | type : object 13 | required : [write_characteristic] 14 | properties: 15 | write_characteristic : 16 | type : object 17 | required: [service_uuid,characteristic_uuid,data] 18 | additionalProperties: false 19 | properties: 20 | service_uuid : {$ref : "/uuid"} 21 | characteristic_uuid : {$ref : "/uuid"} 22 | data : {$ref : "/dataArray"} 23 | -------------------------------------------------------------------------------- /obniz/parts/PressureSensor/FSR-40X/README.md: -------------------------------------------------------------------------------- 1 | # Pressure Sensor - FSR40X 2 | 3 | Pressure sensor. It output pressure as a voltage. 4 | 5 | 6 | ## wired(obniz, {pin0, pin1}) 7 | 8 | Connect two pins to an obniz. pin0 and pin1 is obniz io number. 9 | 10 | ![](./wired.png) 11 | ```Python 12 | # Python Example 13 | pressure = obniz.wired("FSR40X", {"pin0" 0, "pin1": 1}) 14 | ``` 15 | 16 | ## onchange = callback(press) 17 | 18 | callback function will called when pressure changed. 19 | 20 | ```Python 21 | # Python Example 22 | pressure = obniz.wired("FSR40X", {"pin0" 0, "pin1": 1}) 23 | def onchange(press): 24 | print(press) 25 | pressure.onchange = onchange 26 | ``` 27 | 28 | ## [await]get_wait(); 29 | 30 | Get pressure value once. 31 | 32 | ```Python 33 | # Python Example 34 | pressure = obniz.wired("FSR40X", {"pin0" 0, "pin1": 1}) 35 | press = await pressure.get_wait() 36 | print(press) 37 | ``` 38 | -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/analog/__init__.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | class AnalogTemplatureSensor: 4 | def __init__(self): 5 | self.keys = ['vcc', 'gnd', 'output'] 6 | self.required_keys = ['output'] 7 | self.drive = '5v' 8 | self.onchange = None 9 | 10 | def wired(self, obniz): 11 | self.obniz = obniz 12 | obniz.set_vcc_gnd(*[self.params.vcc, self.params.gnd, self.drive]) 13 | self.ad = obniz.get_ad(*[self.params.output]) 14 | def calc_temp(voltage): 15 | self.temp = self.calc(voltage) 16 | if self.onchange: 17 | self.onchange(self.temp) 18 | self.ad.start(calc_temp) 19 | 20 | async def get_wait(self): 21 | voltage = await self.ad.get_wait() 22 | self.temp = self.calc(*[voltage]) 23 | return self.temp 24 | 25 | def calc(self, voltage): 26 | return 0 -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | class Potentiometer: 4 | def __init__(self): 5 | self.keys = ['pin0', 'pin1', 'pin2'] 6 | self.required_keys = ['pin0', 'pin1', 'pin2'] 7 | self.vcc_voltage = 5.0 8 | 9 | @staticmethod 10 | def info(): 11 | return AttrDefault(bool, {'name': 'Potentiometer'}) 12 | 13 | def wired(self, obniz): 14 | self.obniz.set_vcc_gnd(*[self.params.pin0, self.params.pin2, '5v']) 15 | self.ad = obniz.get_ad(*[self.params.pin1]) 16 | 17 | def set_voltage(value): 18 | self.vcc_voltage = value 19 | obniz.get_ad(*[self.params.pin0]).start(set_voltage) 20 | 21 | def set_position(value): 22 | self.position = value / self.vcc_voltage 23 | if self.onchange: 24 | self.onchange(self.position) 25 | self.ad.start(set_position) -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/analog/LM60/README.md: -------------------------------------------------------------------------------- 1 | # Temperature Sensor - LM60 2 | Temperature sensor LM60BIZ/LM60CIM 3 | 4 | 5 | 6 | ![photo of AnalogTempratureSensor](./wired.png) 7 | 8 | 9 | 10 | 11 | ## wired(obniz, {vcc, gnd, output}) 12 | ```Python 13 | # Python Example 14 | tempsens = obniz.wired("LM60", {"gnd": 0 , "output": 1, "vcc" 2}) 15 | ``` 16 | 17 | ## onchange 18 | callback function for temperature change. 19 | Unit of temp is Celsius 20 | 21 | ```Python 22 | # Python Example 23 | tempsens = obniz.wired("LM60", {"gnd": 0 , "output": 1, "vcc" 2}) 24 | 25 | def print_temp(temp): 26 | print(temp) 27 | tempsens.onchange = print_temp 28 | ``` 29 | 30 | ## [await]get_wait 31 | get temperature change. 32 | Unit of temp is Celsius 33 | 34 | ```Python 35 | # Python Example 36 | tempsens = obniz.wired("LM60", {"gnd": 0 , "output": 1, "vcc" 2}) 37 | 38 | temp = await tempsens.get_wait() 39 | print(temp) 40 | ``` 41 | -------------------------------------------------------------------------------- /json_schema/request/ble/peripheral/descriptor_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/peripheral/descriptor_write" 5 | related : "/response/ble/peripheral/descriptor_write" 6 | 7 | description : "write descriptor on own service" 8 | 9 | type: object 10 | required : [peripheral] 11 | properties: 12 | peripheral : 13 | type : object 14 | required : [write_descriptor] 15 | properties: 16 | write_descriptor : 17 | type : object 18 | required: [service_uuid,characteristic_uuid,descriptor_uuid,data] 19 | additionalProperties: false 20 | properties: 21 | service_uuid : {$ref : "/uuid"} 22 | characteristic_uuid : {$ref : "/uuid"} 23 | descriptor_uuid : {$ref : "/uuid"} 24 | data : {$ref : "/dataArray"} -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/README-ja.md: -------------------------------------------------------------------------------- 1 | # Potentiometer 2 | ボリュームのように回すことで抵抗値を変えることが出来る可変抵抗です。 3 | 抵抗値を読み取ることにより今どの位置に回されているのかがわかります。3本の端子があり、端の2つの間はどう回していても一定の抵抗値です。この2つの端子に電圧をかけます。 4 | 回されることで間の1本の電圧が2つの電圧の間を移動します。 5 | 6 | ![](./pm.jpg) 7 | 8 | 9 | このパーツで扱えるポテンションメーターの抵抗値は10Ω〜100kΩの間です。 10 | 11 | ## obniz.wired(obniz, {pin0, pin1, pin2}) 12 | ポテンショメーターの3本のピンをそれぞれpin0, 1, 2として接続します。pin0とpin2は逆でも構いません。(回転が逆になりますが) 13 | 14 | ![](./c_pm.jpg) 15 | 16 | 17 | ![](./wired.png) 18 | 19 | ```Python 20 | # Python Example 21 | meter = obniz.wired("Potentiometer", {"pin0": 0, "pin1": 1, "pin2": 2}) 22 | ``` 23 | ## onchange = callback(position) 24 | 回転を監視し、回転された時にcallback関数を呼び出します。回転に合わせて0.0~1.0の値が返ります。例えばちょうど真ん中であれば0.5です。 25 | ```Python 26 | # Python Example 27 | meter = obniz.wired("Potentiometer", {"pin0": 0, "pin1": 1, "pin2": 2}) 28 | def onchange(position): 29 | print("position:", position) 30 | meter.onchange = onchange 31 | ``` -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/analog/LM60/README-ja.md: -------------------------------------------------------------------------------- 1 | # Temperature Sensor - LM60 2 | 温度センサLM60BIZ/LM60CIMです。センサで取得した温度を知ることができます。 3 | 4 | 5 | 6 | 7 | 8 | 9 | ![photo of AnalogTempratureSensor](./wired.png) 10 | 11 | 12 | 13 | ## wired(obniz, {vcc, output, gnd}) 14 | Obnizに温度センサをつなぎます。 15 | ```Python 16 | # Python Example 17 | tempsens = obniz.wired("LM60", {"gnd": 0 , "output": 1, "vcc" 2}) 18 | ``` 19 | 20 | ## onchange 21 | 温度センサの値に変化があった場合にcallback関数を呼び出します。 22 | 温度は摂氏で返されます。 23 | ```Python 24 | # Python Example 25 | tempsens = obniz.wired("LM60", {"gnd": 0 , "output": 1, "vcc" 2}) 26 | 27 | def print_temp(temp): 28 | print(temp) 29 | tempsens.onchange = print_temp 30 | ``` 31 | 32 | 33 | ## [await]get_wait 34 | 35 | 温度センサの値を一度だけ取得します 36 | 温度は摂氏で返されます。 37 | 38 | ```Python 39 | # Python Example 40 | tempsens = obniz.wired("LM60", {"gnd": 0 , "output": 1, "vcc" 2}) 41 | 42 | temp = await tempsens.get_wait() 43 | print(temp) 44 | ``` 45 | -------------------------------------------------------------------------------- /json_schema/request/ble/central/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ble/central" 5 | basePath : "ble" 6 | description : "use obniz as central" 7 | 8 | "anyOf": 9 | - {$ref : "/request/ble/central/scan_start"} 10 | - {$ref : "/request/ble/central/scan_stop"} 11 | - {$ref : "/request/ble/central/connect"} 12 | - {$ref : "/request/ble/central/disconnect"} 13 | - {$ref : "/request/ble/central/service_get"} 14 | - {$ref : "/request/ble/central/characteristic_get"} 15 | - {$ref : "/request/ble/central/characteristic_read"} 16 | - {$ref : "/request/ble/central/characteristic_write"} 17 | - {$ref : "/request/ble/central/characteristic_register_notify"} 18 | - {$ref : "/request/ble/central/characteristic_unregister_notify"} 19 | - {$ref : "/request/ble/central/descriptor_get"} 20 | - {$ref : "/request/ble/central/descriptor_read"} 21 | - {$ref : "/request/ble/central/descriptor_write"} -------------------------------------------------------------------------------- /json_schema/request/i2c/init_slave.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/i2c/init_slave" 5 | related : "/response/i2c/slave" 6 | 7 | type: object 8 | required : [mode, sda, scl, slave_address] 9 | uniqueKeys : [sda,scl] 10 | properties: 11 | mode : 12 | type : string 13 | enum : [master, slave] 14 | sda : {$ref: "/pinSetting"} 15 | scl : {$ref: "/pinSetting"} 16 | 17 | slave_address: 18 | type : integer 19 | minimum : 0 20 | maximum : 1023 21 | 22 | slave_address_length : 23 | type : integer 24 | enum : [7] 25 | default : 7 26 | 27 | 28 | 29 | address : 30 | type: integer 31 | minimum : 0 32 | maximum : 1023 33 | 34 | address_bits: 35 | type: integer 36 | enum: [7] 37 | default: 7 38 | 39 | data : {$ref: "/dataArray"} 40 | read : 41 | type: integer 42 | minimum : 0 43 | -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/characteristic_notify_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/characteristic_notify_read" 5 | description : "callback of external device read characteristic" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [notify_read_characteristic] 15 | properties: 16 | notify_read_characteristic : 17 | type : object 18 | required: [address,service_uuid, characteristic_uuid] 19 | additionalProperties: false 20 | properties: 21 | address : {$ref : "/deviceAddress"} 22 | service_uuid : {$ref : "/uuid"} 23 | characteristic_uuid : {$ref : "/uuid"} -------------------------------------------------------------------------------- /prototypetest/main.py: -------------------------------------------------------------------------------- 1 | 2 | import asyncio 3 | from obniz import Obniz 4 | 5 | async def onconnect(obniz): 6 | print('connected') 7 | obniz.io1.output(False) 8 | obniz.io0.output(False) 9 | await obniz.wait(500) 10 | obniz.io0.output(True) 11 | await obniz.wait(500) 12 | obniz.io0.output(False) 13 | await obniz.wait(500) 14 | obniz.io0.output(True) 15 | await obniz.wait(500) 16 | obniz.io0.output(False) 17 | await obniz.wait(500) 18 | obniz.io0.output(True) 19 | await obniz.wait(500) 20 | obniz.io0.output(False) 21 | await obniz.wait(500) 22 | obniz.io0.output(True) 23 | await obniz.wait(500) 24 | obniz.io0.output(False) 25 | await obniz.wait(500) 26 | obniz.io0.output(True) 27 | 28 | obniz = Obniz('15696177') 29 | obniz.onconnect = onconnect 30 | 31 | asyncio.get_event_loop().run_forever() 32 | 33 | 34 | print(Obniz.obniz.version) 35 | 36 | 37 | -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/SHT31/README.md: -------------------------------------------------------------------------------- 1 | # Temperature Sensor - SHT31 2 | Temperature and Humidity sensor SHT31 3 | 4 | ![](./sht31.jpg) 5 | 6 | ## wired(obniz, {vcc , sda, scl, adr, gnd, addressmode} ) 7 | the address of SHT31 can be choosed from 0x44,0x45. 8 | Please provide 4 for 0x44. 5 for 0x45 to addressmode. 9 | (SHT31 read ADDR value to define it's address. 0 is 0x45.) 10 | ```Python 11 | # Python Example 12 | sensor = obniz.wired( 13 | "SHT31", 14 | {"vcc": 0, "sda": 1, "scl": 2, "adr": 3, "gnd" :4, "addressmode": 5} 15 | ) 16 | ``` 17 | ## [await] get_temp_wait() 18 | Get a temperature. Unit is Celsius. 19 | 20 | ## [await] get_humd_wait() 21 | Get a Humidity. Unit is Ratio(%). 22 | ```Python 23 | # Python Example 24 | sensor = obniz.wired( 25 | "SHT31", 26 | {"vcc": 0, "sda": 1, "scl": 2, "adr": 3, "gnd" :4, "addressmode": 5} 27 | ) 28 | temp = await sensor.get_temp_wait() 29 | humd = await sensor.get_humd_wait() 30 | print('temperature:', temp) 31 | print('humidity:', humd) 32 | ``` 33 | -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/characteristic_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/characteristic_write" 5 | description : "callback of write characteristic" 6 | 7 | type: object 8 | required : [peripheral] 9 | properties: 10 | peripheral: 11 | type : object 12 | additionalProperties: false 13 | required : [write_characteristic_result] 14 | properties: 15 | write_characteristic_result : 16 | type : object 17 | required: [service_uuid, characteristic_uuid, result] 18 | additionalProperties: false 19 | properties: 20 | service_uuid : {$ref : "/uuid"} 21 | characteristic_uuid : {$ref : "/uuid"} 22 | result : 23 | type : string 24 | enum : [success , failed] -------------------------------------------------------------------------------- /json_schema/request/measure/echo.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/measure/echo" 5 | description : "It measures pulse response." 6 | related : "/response/measure/echo" 7 | 8 | type: object 9 | required : [echo] 10 | properties: 11 | echo : 12 | type: object 13 | required : [io_pulse, io_echo, pulse_width] 14 | properties: 15 | io_pulse : {$ref : "/pinSetting"} 16 | io_echo : {$ref : "/pinSetting"} 17 | pulse : 18 | type : string 19 | default : positive 20 | enum : 21 | - positive 22 | - negative 23 | 24 | pulse_width : 25 | type : number 26 | minimum : 0.001 27 | maximum : 1000 28 | 29 | 30 | measure_edges : 31 | type : integer 32 | minimum : 1 33 | maximum : 4 34 | 35 | timeout : 36 | type : number 37 | default : 1000 38 | minimum : 0.001 39 | maximum : 1000 40 | 41 | -------------------------------------------------------------------------------- /json_schema/response/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | --- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response" 5 | type : array 6 | minItems: 1 7 | items : 8 | type: object 9 | additionalProperties: false 10 | patternProperties: 11 | "^io[0-9]$" : {$ref : "/response/io"} 12 | "^io1[0-1]$" : {$ref : "/response/io"} 13 | "^ad[0-9]$" : {$ref : "/response/ad"} 14 | "^ad1[0-1]$" : {$ref : "/response/ad"} 15 | "^uart[0-1]$" : {$ref : "/response/uart"} 16 | "^spi[0-1]$" : {$ref : "/response/spi"} 17 | "^i2c0$" : {$ref : "/response/i2c"} 18 | 19 | properties: 20 | switch : {$ref : "/response/switch"} 21 | ble : {$ref : "/response/ble"} 22 | measure : {$ref : "/response/measure"} 23 | message : {$ref : "/response/message"} 24 | logic_analyzer : {$ref : "/response/logicAnalyzer"} 25 | system : {$ref : "/response/system"} 26 | debug : {$ref : "/response/debug"} 27 | ws : {$ref : "/response/ws"} 28 | -------------------------------------------------------------------------------- /obniz/parts/PressureSensor/FSR-40X/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | import asyncio 4 | 5 | class FSR40X: 6 | def __init__(self): 7 | self.keys = ['pin0', 'pin1'] 8 | self.required_keys = ['pin0', 'pin1'] 9 | self.onchange = None 10 | 11 | @staticmethod 12 | def info(): 13 | return AttrDefault(bool, {'name': 'FSR40X'}) 14 | 15 | def wired(self, obniz): 16 | self.obniz = obniz 17 | self.io_pwr = obniz.get_io(*[self.params.pin0]) 18 | self.ad = obniz.get_ad(*[self.params.pin1]) 19 | self.io_pwr.drive(*['5v']) 20 | self.io_pwr.output(*[True]) 21 | def get_pressure(value): 22 | pressure = value * 100 23 | self.press = pressure 24 | if self.onchange: 25 | self.onchange(self.press) 26 | self.ad.start(get_pressure) 27 | 28 | async def get_wait(self): 29 | value = await self.ad.get_wait() 30 | pressure = value * 100 31 | self.press = pressure 32 | return self.press -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/SHT31/README-ja.md: -------------------------------------------------------------------------------- 1 | # Temperature Sensor - SHT31 2 | 温度センサSHT31です。センサで取得した温度を知ることができます。 3 | 4 | ![](./sht31.jpg) 5 | 6 | ## wired(obniz, {vcc , sda, scl, adr, gnd, addressmode} ) 7 | Obnizに温度センサをつなぎます。 8 | 0,1,2,3,4はそれぞれ温度センサの電源,SDA,SCL,GND,ADDRピンへ接続してください。 9 | 5はI2Cアドレスです。アドレスを0x44にする場合は4,0x45にする場合は5を入力してください。 10 | ADDRピンをプルアップしている場合は0x45,プルダウンしている場合は0x44です。 11 | 秋月電子のモジュールキット(K-12125)を使用している場合のデフォルトは0x45です。 12 | ```Python 13 | # Python Example 14 | sensor = obniz.wired( 15 | "SHT31", 16 | {"vcc": 0, "sda": 1, "scl": 2, "adr": 3, "gnd" :4, "addressmode": 5} 17 | ) 18 | ``` 19 | ## [await] get_temp_wait() 20 | 現在の温度を計測して返します。単位は摂氏(°C)です。 21 | 22 | ## [await] get_humd_wait() 23 | 現在の湿度を計測して返します。単位は%です。 24 | ```Python 25 | # Python Example 26 | sensor = obniz.wired( 27 | "SHT31", 28 | {"vcc": 0, "sda": 1, "scl": 2, "adr": 3, "gnd" :4, "addressmode": 5} 29 | ) 30 | temp = await sensor.get_temp_wait() 31 | humd = await sensor.get_humd_wait() 32 | print('temperature:', temp) 33 | print('humidity:', humd) 34 | ``` 35 | -------------------------------------------------------------------------------- /json_schema/request/uart/init.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/uart/init" 5 | description : "available 0 to 1" 6 | 7 | type: object 8 | required : [rx,tx] 9 | uniqueKeys : [rx,tx,rts,cts] 10 | properties: 11 | rx : {$ref: "/pinSetting"} 12 | tx : {$ref: "/pinSetting"} 13 | baud : 14 | type: integer 15 | default : 115200 16 | minimum : 1 17 | maximum : 5000000 18 | description : "baud rate (bps)" 19 | 20 | stop: 21 | type: number 22 | enum: [1, 1.5, 2] 23 | default : 1 24 | description : "stop bit width" 25 | 26 | bits: 27 | type: integer 28 | enum: [5, 6, 7, 8] 29 | default : 8 30 | 31 | parity: 32 | type: string 33 | enum: [off, odd, even] 34 | default : off 35 | 36 | flowcontrol: 37 | type: string 38 | enum: [off, rts, cts, rts-cts] 39 | default : off 40 | 41 | rts: {$ref: "/pinSetting"} 42 | cts: {$ref: "/pinSetting"} -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/characteristic_notify_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/characteristic_notify_write" 5 | description : "callback of external device write characteristic" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [notify_write_characteristic] 15 | properties: 16 | notify_write_characteristic : 17 | type : object 18 | required: [address,service_uuid, characteristic_uuid, data] 19 | additionalProperties: false 20 | properties: 21 | address : {$ref : "/deviceAddress"} 22 | service_uuid : {$ref : "/uuid"} 23 | characteristic_uuid : {$ref : "/uuid"} 24 | data : {$ref : "/dataArray"} -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/descriptor_notify_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/descriptor_notify_read" 5 | description : "callback of external device read descriptor" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [notify_read_descriptor] 15 | properties: 16 | notify_read_descriptor : 17 | type : object 18 | required: [address,service_uuid, characteristic_uuid,descriptor_uuid] 19 | additionalProperties: false 20 | properties: 21 | address : {$ref : "/deviceAddress"} 22 | service_uuid : {$ref : "/uuid"} 23 | characteristic_uuid : {$ref : "/uuid"} 24 | descriptor_uuid : {$ref : "/uuid"} 25 | -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Potentiometer/README.md: -------------------------------------------------------------------------------- 1 | # Potentiometer 2 | 3 | Potention Meter change it's resistor value regarding angle or position. 4 | obniz can read resistor value by using AD. 5 | It has three pins. between sides resistor values is constant. voltage of center pin moves within left and right side's voltage. 6 | 7 | ![](./pm.jpg) 8 | 9 | This parts can treat only 10 ohm to 100 k ohm. 10 | 11 | 12 | ## obniz.wired(obniz, {pin0, pin1, pin2}) 13 | 14 | pin0 and pin2 is side pins. It can be swapped. But position will be reversed. 15 | 16 | 17 | ![](./c_pm.jpg) 18 | 19 | 20 | ![](./wired.png) 21 | 22 | ```Python 23 | # Python Example 24 | meter = obniz.wired("Potentiometer", {"pin0": 0, "pin1": 1, "pin2": 2}) 25 | ``` 26 | ## onchange = callback(position) 27 | called when rotated. 28 | position value is within 0.0 to 1.0. so, center is 0.5. 29 | ```Python 30 | # Python Example 31 | meter = obniz.wired("Potentiometer", {"pin0": 0, "pin1": 1, "pin2": 2}) 32 | def onchange(position): 33 | print("position:", position) 34 | meter.onchange = onchange 35 | ``` -------------------------------------------------------------------------------- /json_schema/response/ble/central/error.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/error" 5 | 6 | 7 | type: object 8 | required : [error] 9 | properties: 10 | error : 11 | type : object 12 | required: [error_code, message] 13 | additionalProperties: false 14 | properties: 15 | error_code : 16 | type: integer 17 | example : 0 18 | module_error_code : 19 | type: integer 20 | example : 0 21 | function_code : 22 | type: integer 23 | example : 0 24 | message : 25 | type: string 26 | example : "ERROR MESSAGE" 27 | address : {$ref : "/deviceAddress"} 28 | service_uuid : {$ref : "/uuidOrNull"} 29 | characteristic_uuid : {$ref : "/uuidOrNull"} 30 | descriptor_uuid : {$ref : "/uuidOrNull"} -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | url = "https://pypi.org/simple" 3 | verify_ssl = true 4 | name = "pypi" 5 | 6 | [scripts] 7 | test = "pytest -vv" 8 | lint = "sh -c 'flake8 && pipenv run format --check'" 9 | format = "black obniz/ tests/ " 10 | build = "sh -c 'rm -rf build/ dist/ obniz.egg-info/ && python setup.py sdist bdist_wheel'" 11 | testsubmit = "twine upload --repository testpypi dist/*" 12 | # no shortcut for production submit for safe 13 | 14 | [packages] 15 | pyee = "*" 16 | websockets = "*" 17 | attrdict = "*" 18 | semver = "*" 19 | 20 | [dev-packages] 21 | asynctest = "*" 22 | # "black" requires python 3.6+ 23 | # so if it exists in Pipfile, `pipenv insttall` must fail on Travis CI python 3.5 24 | # please install by your self via `pipenv run pip install black` 25 | # (avoid `pipenv install black` in order to bypass adding "black" to Pipfile) 26 | # black = "*" 27 | jsonschema = "*" 28 | "flake8" = "*" 29 | flake8-import-order = "*" 30 | pep8-naming = "*" 31 | pytest = "*" 32 | pytest-mock = "*" 33 | pyyaml = "*" 34 | setuptools = "*" 35 | wheel = "*" 36 | twine = "*" 37 | tox = "*" 38 | -------------------------------------------------------------------------------- /json_schema/request/display/pin_assign.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/display/pin_assign" 5 | 6 | type : object 7 | required : [pin_assign] 8 | properties : 9 | pin_assign : 10 | type : object 11 | minProperties : 1 12 | patternExample : [0,1,2,3] 13 | patternProperties : 14 | "^[0-9]$" : 15 | type : object 16 | properties : 17 | module_name : 18 | type : string 19 | example : "io" 20 | pin_name : 21 | type : string 22 | example : "output" 23 | 24 | "^1[0-1]$" : 25 | type : object 26 | properties : 27 | module_name : 28 | type : string 29 | example : "io" 30 | pin_name : 31 | type : string 32 | example : "output" 33 | 34 | 35 | -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/characteristic_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/characteristic_read" 5 | description : "callback of read characteristic" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [read_characteristic_result] 15 | properties: 16 | read_characteristic_result : 17 | type : object 18 | required: [service_uuid, characteristic_uuid, data, result] 19 | additionalProperties: false 20 | properties: 21 | service_uuid : {$ref : "/uuid"} 22 | characteristic_uuid : {$ref : "/uuid"} 23 | data : {$ref : "/dataArray"} 24 | result : 25 | type : string 26 | enum : [success, failed] -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/descriptor_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/descriptor_write" 5 | description : "callback of write descriptor" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [write_descriptor_result] 15 | properties: 16 | write_descriptor_result : 17 | type : object 18 | required: [service_uuid, characteristic_uuid,descriptor_uuid, result] 19 | additionalProperties: false 20 | properties: 21 | service_uuid : {$ref : "/uuid"} 22 | characteristic_uuid : {$ref : "/uuid"} 23 | descriptor_uuid : {$ref : "/uuid"} 24 | result : 25 | type : string 26 | enum : [success , failed] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 CambrianRobotics inc, 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | This software should be used for control obniz hardware. 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/descriptor_notify_write.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/descriptor_notify_write" 5 | description : "callback of external device write descriptor" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [notify_write_descriptor] 15 | properties: 16 | notify_write_descriptor : 17 | type : object 18 | required: [address,service_uuid, characteristic_uuid,descriptor_uuid, data] 19 | additionalProperties: false 20 | properties: 21 | address : {$ref : "/deviceAddress"} 22 | service_uuid : {$ref : "/uuid"} 23 | characteristic_uuid : {$ref : "/uuid"} 24 | descriptor_uuid : {$ref : "/uuid"} 25 | data : {$ref : "/dataArray"} -------------------------------------------------------------------------------- /json_schema/response/ble/central/characteristic_get.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/characteristic_get" 5 | 6 | 7 | type: object 8 | required : [get_characteristic_result] 9 | properties: 10 | get_characteristic_result : 11 | type : object 12 | required: [address,service_uuid,characteristic_uuid,properties] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | service_uuid : {$ref : "/uuid"} 17 | characteristic_uuid : {$ref : "/uuid"} 18 | properties : 19 | type : array 20 | items : 21 | type : string 22 | enum : 23 | - broadcast 24 | - read 25 | - write_without_response 26 | - write 27 | - notify 28 | - indicate 29 | - auth 30 | - extended_properties 31 | 32 | 33 | -------------------------------------------------------------------------------- /json_schema/response/ble/peripheral/descriptor_read.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/peripheral/descriptor_read" 5 | description : "callback of read descriptor" 6 | 7 | 8 | type: object 9 | required : [peripheral] 10 | properties: 11 | peripheral: 12 | type : object 13 | additionalProperties: false 14 | required : [read_descriptor_result] 15 | properties: 16 | 17 | read_descriptor_result : 18 | type : object 19 | required: [service_uuid, characteristic_uuid,descriptor_uuid, data, result] 20 | additionalProperties: false 21 | properties: 22 | service_uuid : {$ref : "/uuid"} 23 | characteristic_uuid : {$ref : "/uuid"} 24 | descriptor_uuid : {$ref : "/uuid"} 25 | data : {$ref : "/dataArray"} 26 | result : 27 | type : string 28 | enum : [success , failed] -------------------------------------------------------------------------------- /json_schema/response/ble/central/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central" 5 | basePath : "ble" 6 | 7 | "anyOf": 8 | - {$ref : "/response/ble/central/scan"} 9 | - {$ref : "/response/ble/central/scan_finish"} 10 | - {$ref : "/response/ble/central/status_update"} 11 | - {$ref : "/response/ble/central/service_get"} 12 | - {$ref : "/response/ble/central/service_get_finish"} 13 | - {$ref : "/response/ble/central/characteristic_get"} 14 | - {$ref : "/response/ble/central/characteristic_get_finish"} 15 | - {$ref : "/response/ble/central/characteristic_write"} 16 | - {$ref : "/response/ble/central/characteristic_read"} 17 | - {$ref : "/response/ble/central/characteristic_register_notify"} 18 | - {$ref : "/response/ble/central/characteristic_notify"} 19 | - {$ref : "/response/ble/central/characteristic_notify"} 20 | - {$ref : "/response/ble/central/descriptor_get"} 21 | - {$ref : "/response/ble/central/descriptor_get_finish"} 22 | - {$ref : "/response/ble/central/descriptor_write"} 23 | - {$ref : "/response/ble/central/descriptor_read"} 24 | - {$ref : "/response/ble/central/error"} -------------------------------------------------------------------------------- /json_schema/request/index.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | --- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request" 5 | type : array 6 | minItems: 1 7 | items : 8 | type: object 9 | additionalProperties: false 10 | patternProperties: 11 | "^io[0-9]$" : {$ref : "/request/io" } 12 | "^io1[0-1]$" : {$ref : "/request/io" } 13 | "^ad[0-9]$" : {$ref : "/request/ad" } 14 | "^ad1[0-1]$" : {$ref : "/request/ad" } 15 | "^pwm[0-5]$" : {$ref : "/request/pwm" } 16 | "^uart[0-1]$" : {$ref : "/request/uart" } 17 | "^spi[0-1]$" : {$ref : "/request/spi" } 18 | "^i2c0$" : {$ref : "/request/i2c" } 19 | 20 | properties: 21 | io: {$ref : "/request/ioAnimation" } 22 | ble: {$ref : "/request/ble" } 23 | switch: {$ref : "/request/switch" } 24 | display: {$ref : "/request/display" } 25 | measure : {$ref : "/request/measure" } 26 | message : {$ref : "/request/message" } 27 | logic_analyzer : {$ref : "/request/logicAnalyzer" } 28 | system : {$ref : "/request/system" } 29 | ws : {$ref : "/request/ws" } 30 | 31 | -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/HC-SR505/README-ja.md: -------------------------------------------------------------------------------- 1 | # HC-SR505 2 | 3 | PIR センサーです。人や動物が近くにいるかを検出できます。 4 | 5 | ![](image.jpg) 6 | 7 | 8 | ## wired(obniz, {signal [,vcc, gnd]}) 9 | 10 | 3つのピンがあります。直接obnizに接続して下さい。 11 | 12 | ```Python 13 | # Python Example 14 | sensor = obniz.wired("HC-SR505", {"vcc": 0, "signal": 1, "gnd": 2}) 15 | def onchange(val): 16 | if val: 17 | print('Moving Something!') 18 | else: 19 | print('Nothing moving') 20 | sensor.onchange = onchange 21 | ``` 22 | 23 | ## onchange = function(value) 24 | 25 | 何かが変化した時に呼ばれる関数を設定します。 26 | 人が近づいてきたときに関数が呼ばれ、値`True`が引数に入っています。 27 | もし、人がいなくなったりすると`False`が引数に入ります。 28 | 29 | ```Python 30 | # Python Example 31 | sensor = obniz.wired("HC-SR505", {"vcc": 0, "signal": 1, "gnd": 2}) 32 | def onchange(val): 33 | if val: 34 | print('Moving Something!') 35 | else: 36 | print('Nothing moving') 37 | sensor.onchange = onchange 38 | ``` 39 | 40 | ## [await] get_wait() 41 | 42 | 近くに人がいるかどうかを一度だけ取得します. 43 | `True`なら近くに人がいて,`False`なら近くに人がいない状態です 44 | 45 | 46 | ```Python 47 | # Python Example 48 | sensor = obniz.wired("HC-SR505", {"vcc": 0, "signal": 1, "gnd": 2}) 49 | val = await sensor.get_wait() 50 | if val: 51 | print('Moving Something!') 52 | else: 53 | print('Nothing moving') 54 | ``` 55 | -------------------------------------------------------------------------------- /obniz/obniz/libs/utils/util.py: -------------------------------------------------------------------------------- 1 | import json 2 | import attrdict 3 | 4 | class ObnizUtil: 5 | @classmethod 6 | def _key_filter(cls, params, keys): 7 | filterd_params = {} 8 | if type(params) not in [dict, attrdict.default.AttrDefault]: 9 | return filterd_params 10 | 11 | for key in [key for key in params.keys() if key in keys]: 12 | filterd_params[key] = params[key] 13 | 14 | return filterd_params 15 | 16 | @classmethod 17 | def _required_keys(cls, params, keys): 18 | if type(params) not in [dict, attrdict.default.AttrDefault]: 19 | return keys[0] 20 | 21 | for key in keys: 22 | if key not in params: 23 | return key 24 | 25 | return None 26 | 27 | @classmethod 28 | def data_array2string(cls, data): 29 | return "".join([chr(d) for d in data]) 30 | 31 | @classmethod 32 | def string2data_array(cls, string): 33 | return [ord(c) for c in list(string)] 34 | 35 | @classmethod 36 | def json_dumps(cls, obj): 37 | return json.dumps(obj, default=_default) 38 | 39 | 40 | def _default(obj): 41 | if hasattr(obj, "to_json"): 42 | return obj.to_json() 43 | raise TypeError(repr(obj) + " is not JSON serializable") 44 | -------------------------------------------------------------------------------- /obniz/obniz/libs/hw/obnizb1.json: -------------------------------------------------------------------------------- 1 | { 2 | "rev": "1", 3 | "hw": "obnizb1", 4 | "peripherals": { 5 | "io": { 6 | "units": { 7 | "0": {}, 8 | "1": {}, 9 | "2": {}, 10 | "3": {}, 11 | "4": {}, 12 | "5": {}, 13 | "6": {}, 14 | "7": {}, 15 | "8": {}, 16 | "9": {}, 17 | "10": {}, 18 | "11": {} 19 | } 20 | }, 21 | "ad": { 22 | "units": { 23 | "0": {}, 24 | "1": {}, 25 | "2": {}, 26 | "3": {}, 27 | "4": {}, 28 | "5": {}, 29 | "6": {}, 30 | "7": {}, 31 | "8": {}, 32 | "9": {}, 33 | "10": {}, 34 | "11": {} 35 | } 36 | }, 37 | "pwm": { 38 | "units": { 39 | "0": {}, 40 | "1": {}, 41 | "2": {}, 42 | "3": {}, 43 | "4": {}, 44 | "5": {} 45 | } 46 | }, 47 | "uart": { 48 | "units": { 49 | "0": {}, 50 | "1": {} 51 | } 52 | }, 53 | "spi": { 54 | "units": { 55 | "0": {}, 56 | "1": {} 57 | } 58 | }, 59 | "i2c": { 60 | "units": { 61 | "0": {} 62 | } 63 | } 64 | }, 65 | "embeds": { 66 | "ble": {}, 67 | "display": {}, 68 | "switch": {} 69 | } 70 | } -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | class ServoMotor: 4 | def __init__(self): 5 | self.keys = ['gnd', 'vcc', 'signal', 'pwm'] 6 | self.required_keys = [] 7 | self.range = AttrDefault(bool, {'min': 0.5, 'max': 2.4}) 8 | 9 | @staticmethod 10 | def info(): 11 | return AttrDefault(bool, {'name': 'ServoMotor'}) 12 | 13 | def wired(self, obniz): 14 | self.obniz = obniz 15 | self.obniz.set_vcc_gnd(*[self.params.vcc, self.params.gnd, '5v']) 16 | if obniz.is_valid_io(*[self.params.vcc]): 17 | self.io_vcc = obniz.get_io(*[self.params.vcc]) 18 | if self.params.pwm: 19 | self.pwm = self.params.pwm 20 | else: 21 | self.pwm = obniz.get_free_pwm() 22 | self.pwm_io_num = self.params.signal 23 | # self.pwm.start(*[AttrDefault(bool, {'io': self.pwm_io_num})]) 24 | self.pwm.start({'io': self.pwm_io_num}) 25 | self.pwm.freq(*[50]) 26 | 27 | def angle(self, ratio): 28 | max = self.range.max 29 | min = self.range.min 30 | val = ((max - min) * ratio / 180.0 + min) 31 | self.pwm.pulse(*[val]) 32 | 33 | def on(self): 34 | if self.io_vcc: 35 | self.io_vcc.output(*[True]) 36 | 37 | def off(self): 38 | if self.io_vcc: 39 | self.io_vcc.output(*[False]) -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_advertisement.py: -------------------------------------------------------------------------------- 1 | from .ble_advertisement_builder import BleAdvertisementBuilder as Builder 2 | 3 | 4 | class BleAdvertisement: 5 | def __init__(self, obniz): 6 | self.obniz = obniz 7 | self.adv_data = [] 8 | self.scan_resp = [] 9 | 10 | def start(self): 11 | obj = {} 12 | obj["ble"] = {} 13 | obj["ble"]["advertisement"] = {"adv_data": self.adv_data} 14 | 15 | if len(self.scan_resp) > 0: 16 | obj["ble"]["advertisement"]["scan_resp"] = self.scan_resp 17 | 18 | self.obniz.send(obj) 19 | 20 | def end(self): 21 | obj = {} 22 | obj["ble"] = {} 23 | obj["ble"]["advertisement"] = None 24 | self.obniz.send(obj) 25 | 26 | def set_adv_data_raw(self, adv_data): 27 | self.adv_data = adv_data 28 | 29 | def set_adv_data(self, json): 30 | builder = self.adv_data_bulider(json) 31 | self.set_adv_data_raw(builder.build()) 32 | 33 | def adv_data_bulider(self, json_val): 34 | return Builder(self.obniz, json_val) 35 | 36 | def scan_resp_data_builder(self, json): 37 | return Builder(self.obniz, json) 38 | 39 | def set_scan_resp_data_raw(self, scan_resp): 40 | self.scan_resp = scan_resp 41 | 42 | def set_scan_resp_data(self, json): 43 | self.set_scan_resp_data_raw(self.scan_resp_data_builder(json).build()) 44 | -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_service.py: -------------------------------------------------------------------------------- 1 | from .ble_attribute_abstract import BleAttributeAbstract 2 | from .ble_characteristic import BleCharacteristic 3 | 4 | # const BleHelper = require('./bleHelper') 5 | 6 | 7 | class BleService(BleAttributeAbstract): 8 | def __init__(self, obj): 9 | super().__init__(obj) 10 | 11 | self.add_characteristic = self.add_child 12 | self.get_characteristic = self.get_child 13 | 14 | @property 15 | def parent_name(self): 16 | return "peripheral" 17 | 18 | @property 19 | def children_name(self): 20 | return "characteristics" 21 | 22 | @property 23 | def children_class(self): 24 | return BleCharacteristic 25 | 26 | @property 27 | def adv_data(self): 28 | return { 29 | "flags": ["general_discoverable_mode", "br_edr_not_supported"], 30 | "serviceUuids": [self.uuid], 31 | } 32 | 33 | # end() { 34 | # self.peripheral.Obniz.send({ 35 | # ble: { 36 | # peripheral: { 37 | # stop_service: { 38 | # service_uuid: BleHelper.uuidFilter(self.uuid), 39 | # }, 40 | # }, 41 | # }, 42 | # }) 43 | # self.peripheral.removeService(self.uuid) 44 | # } 45 | 46 | # notify(notifyName, params) { 47 | # //nothing 48 | # } 49 | -------------------------------------------------------------------------------- /json_schema/request/logicanalyzer/init.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/logicAnalyzer/init" 5 | exampleDescription : "With below sample code, you will receive only data which start with 'false, false, false' 3bit." 6 | 7 | type: object 8 | required : [io, interval, duration] 9 | properties: 10 | io : 11 | type : array 12 | minItems : 1 13 | maxItems : 1 14 | items : {$ref : "/pinSetting"} 15 | 16 | interval : 17 | type : number 18 | minimum : 0 19 | exclusiveMinimum: true 20 | 21 | duration : 22 | type : integer 23 | minimum : 0 24 | exclusiveMinimum: true 25 | 26 | trigger : 27 | type : object 28 | description : "Without this, logic analyzer will start with any io level changes. trigger specify start position. " 29 | 30 | additionalProperties: false 31 | required : [value, samples] 32 | default : {value: false, samples: 0} 33 | properties: 34 | value : 35 | description : "start value" 36 | type : boolean 37 | default : false 38 | 39 | samples : 40 | type : integer 41 | description : "how that values consists" 42 | minimum : 0 43 | default : 0 44 | example : 3 45 | 46 | -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Button/README-ja.md: -------------------------------------------------------------------------------- 1 | # Button 2 | 押すことで電流を流したり止めたり出来る部品です。これをつなぎ電流が流れているかを見ることでボタンが押されているかをチェックできます。このモジュールではボタンの形によらず、とにかく押せば電流が流れるボタンを扱うことができます。 3 | 4 | ## wired(obniz, {signal [,gnd]}) 5 | 6 | ボタンにある2つのピンをObnizにつなぎます。プラスとマイナスはありません。例えば片方をObnizの0番に。もう片方を1番につないだ場合は以下のようにプログラム上でwireします 7 | 8 | ![photo of wired](./wired.png) 9 | 10 | ```Python 11 | # Python Example 12 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 13 | ``` 14 | 15 | gndはオプショナルです。他のGNDに繋いだ場合は、指定しなくても大丈夫です。 16 | 17 | ```Python 18 | # Python Example 19 | button = obniz.wired("Button", {"signal":0}) 20 | ``` 21 | 22 | ## onchange = function(pressed) 23 | ボタンが押された時、離された時にcallback関数を呼び出します。 24 | 25 | ```Python 26 | # Python Example 27 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 28 | 29 | def onchange(pressed): 30 | print("pressed:", pressed) 31 | 32 | button.onchange = onchange 33 | ``` 34 | 35 | ## [await] is_pressed_wait() 36 | ボタンが押されているかを確認します。 37 | ```Python 38 | # Python Example 39 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 40 | 41 | pressed = await button.is_pressed_wait() 42 | print("pressed:", pressed) 43 | ``` 44 | 45 | 46 | ## [await] state_wait() 47 | ボタンが押される/離されるまで待ちます 48 | ```Python 49 | # Python Example 50 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 51 | 52 | await button.state_wait(True) 53 | print("button pushed!") 54 | await button.state_wait(False) 55 | print("button released") 56 | ``` -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/HC-SR505/README.md: -------------------------------------------------------------------------------- 1 | # HC-SR505 2 | 3 | It is used for detects humans/animals. 4 | 5 | ![](image.jpg) 6 | 7 | 8 | ## wired(obniz, {signal [,vcc, gnd]}) 9 | 10 | It has three pins. Connect them to an obniz directly. 11 | 12 | ```Python 13 | # Python Example 14 | sensor = obniz.wired("HC-SR505", {"vcc": 0, "signal": 1, "gnd": 2}) 15 | def onchange(val): 16 | if val: 17 | print('Moving Something!') 18 | else: 19 | print('Nothing moving') 20 | sensor.onchange = onchange 21 | ``` 22 | 23 | ## onchange = function(value) 24 | 25 | It called when something changed. 26 | It humans is closing to a sensor, this function will called with value `True`. 27 | If a humans leaves or if a human stops moving, this function will called with value `False`. 28 | It's fileter works. So called with False after soon called with True. 29 | 30 | ```Python 31 | # Python Example 32 | sensor = obniz.wired("HC-SR505", {"vcc": 0, "signal": 1, "gnd": 2}) 33 | def onchange(val): 34 | if val: 35 | print('Moving Something!') 36 | else: 37 | print('Nothing moving') 38 | sensor.onchange = onchange 39 | ``` 40 | 41 | ## [await] getWait() 42 | 43 | Get sensor value once. 44 | 45 | ```Python 46 | # Python Example 47 | sensor = obniz.wired("HC-SR505", {"vcc": 0, "signal": 1, "gnd": 2}) 48 | val = await sensor.get_wait() 49 | if val: 50 | print('Moving Something!') 51 | else: 52 | print('Nothing moving') 53 | ``` 54 | -------------------------------------------------------------------------------- /json_schema/response/ble/central/scan.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/response/ble/central/scan" 5 | 6 | 7 | type: object 8 | required : [scan_result] 9 | properties: 10 | scan_result : 11 | type : object 12 | required: [address,ble_event_type,device_type,address_type,flag,rssi] 13 | additionalProperties: false 14 | properties: 15 | address : {$ref : "/deviceAddress"} 16 | ble_event_type : 17 | type : string 18 | enum : 19 | - connectable_advertisemnt 20 | - connectable_directed_advertisemnt 21 | - scannable_advertising 22 | - non_connectable_advertising 23 | - scan_response 24 | device_type : 25 | type : string 26 | enum : [ble, dumo, breder] 27 | address_type: 28 | type : string 29 | enum : [public, random, rpa_public, rpa_random] 30 | flag : 31 | type : integer 32 | minimum : 0 33 | rssi : 34 | type : integer 35 | maximum : 0 36 | adv_data : {$ref : "/bleAdvertiseData"} 37 | scan_resp : {$ref : "/bleAdvertiseData"} 38 | -------------------------------------------------------------------------------- /obniz/parts/Moving/DCMotor/README-ja.md: -------------------------------------------------------------------------------- 1 | # DCMotor 2 | タミヤモーターやマブチモーターのような、
電池を繋いで回すような一般的なモーターです。プラスとマイナスはありませんが逆にすると逆に回転します。 3 | 4 | ![photo of DCMotor](./wired.png) 5 | 6 | ## wire({forward, back}) 7 | モーターから出ている2本の線をObnizにつなぎます。どちらをどこにつないでもOKですが、プログラムで「前」と書いて後ろに動いたらあとで配線を逆にすると良いです。モーターをObnizの0と1番に繋いだ場合は以下のようにします 8 | ```Python 9 | # Python Example 10 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 11 | ``` 12 | ## forward() 13 | モーターを回転させます。 14 | 15 | 16 | ```Python 17 | # Python Example 18 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 19 | 20 | motor.forward() 21 | ``` 22 | ## reverse() 23 | モーターを逆に回転させます。 24 | 25 | ```Python 26 | # Python Example 27 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 28 | 29 | motor.reverse() 30 | ``` 31 | ## stop() 32 | モーターを停止させます。 33 | 34 | ```Python 35 | # Python Example 36 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 37 | 38 | motor.forward() 39 | await obniz.wait(1000) 40 | motor.stop() 41 | ``` 42 | ## move(bool) 43 | directionに合わせて指定した方向にモーターを回転させます。rotateとreverseを引数を変えるだけでこの関数1つで扱えます。trueであれば正転。falseであれば逆に回転します。 44 | 45 | ```Python 46 | # Python Example 47 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 48 | 49 | motor.move(True) # = motor.forward() 50 | ``` 51 | ## power(float) 52 | モーターのパワーを変更します。0~100で指定することが出来ます。 53 | 54 | ```Python 55 | # Python Example 56 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 57 | 58 | motor.move(True) # = motor.forward() 59 | ``` -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Button/README.md: -------------------------------------------------------------------------------- 1 | # Button 2 | Button turn on/off electricity. Just monitor voltage to check button pressed or not. 3 | 4 | ## wired(obniz, {signal [,gnd]}) 5 | 6 | Connect two pins to obniz. Many buttons has no pin direction. you can connect each one to signal,gnd. 7 | 8 | ![photo of wired](./wired.png) 9 | 10 | ```Python 11 | # Python Example 12 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 13 | ``` 14 | 15 | gnd is optional. It can be shared other gnd. 16 | 17 | ```Python 18 | # Python Example 19 | button = obniz.wired("Button", {"signal":0}) 20 | ``` 21 | 22 | 23 | 24 | ## onchange = function(pressed) 25 | called when button pressed/released. 26 | 27 | ```Python 28 | # Python Example 29 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 30 | 31 | def onchange(pressed): 32 | print("pressed:", pressed) 33 | 34 | button.onchange = onchange 35 | ``` 36 | ## [await] is_pressed_wait() 37 | Check current button with waiting result. 38 | ```Python 39 | # Python Example 40 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 41 | 42 | pressed = await button.is_pressed_wait() 43 | print("pressed:", pressed) 44 | ``` 45 | 46 | 47 | 48 | ## [await] state_wait() 49 | Wait until push/release button. 50 | ```Python 51 | # Python Example 52 | button = obniz.wired("Button", {"signal": 0, "gnd": 1}) 53 | 54 | await button.state_wait(True) 55 | print("button pushed!") 56 | await button.state_wait(False) 57 | print("button released") 58 | ``` -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/README-ja.md: -------------------------------------------------------------------------------- 1 | # GP2Y0A21YK0F 2 | 赤外線を利用した距離センサーです。 3 | 距離を電圧として出力するモジュールです。 4 | 5 | ![photo of wired](./image.gif) 6 | 7 | ## wired(obniz, {vcc, gnd, signal}) 8 | 9 | ![photo of wired](./image.jpg) 10 | 11 | ![photo of wired](./wired.png) 12 | ```Python 13 | # Python Example 14 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 15 | def print_distance(distance): 16 | print("distance:", distance, "mm") 17 | sensor.start(print_distance) 18 | ``` 19 | 20 | ## start(callback(distance)) 21 | 距離を継続的に計測します。距離に変化があれば関数が呼ばれます。 22 | 単位は"mm"でunit()関数で他のものに変更できます。 23 | ```Python 24 | # Python Example 25 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 26 | 27 | def print_distance(distance): 28 | print("distance:", distance, "mm") 29 | sensor.start(print_distance) 30 | }) 31 | ``` 32 | 33 | ## [await] get_wait() 34 | 一度だけ距離を測定します 35 | 36 | ```Python 37 | # Python Example 38 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 39 | 40 | while True: 41 | val = await sensor.get_wait() 42 | print("distance", val) 43 | await obniz.wait(1000) 44 | ``` 45 | 46 | 47 | 48 | ## unit(unit) 49 | 単位を変更します。 50 | 51 | 1. "mm"(default) 52 | 2. "inch" 53 | 54 | が利用できます。 55 | 56 | ```Python 57 | # Python Example 58 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 59 | 60 | sensor.unit("inch") 61 | def print_distance(distance): 62 | print("distance:", distance, "mm") 63 | sensor.start(print_distance) 64 | ``` -------------------------------------------------------------------------------- /obniz/obniz/libs/hw/esp32w.json: -------------------------------------------------------------------------------- 1 | { 2 | "rev": "1", 3 | "hw": "esp32w", 4 | "peripherals": { 5 | "io": { 6 | "units": { 7 | "0": {}, 8 | "2": {}, 9 | "4": {}, 10 | "5": {}, 11 | "12": {}, 12 | "13": {}, 13 | "14": {}, 14 | "15": {}, 15 | "16": {}, 16 | "17": {}, 17 | "18": {}, 18 | "19": {}, 19 | "21": {}, 20 | "22": {}, 21 | "23": {}, 22 | "25": {}, 23 | "26": {}, 24 | "27": {}, 25 | "32": {}, 26 | "33": {}, 27 | "34": {}, 28 | "35": {}, 29 | "36": {}, 30 | "37": {}, 31 | "38": {}, 32 | "39": {} 33 | } 34 | }, 35 | "ad": { 36 | "units": { 37 | "32": {}, 38 | "33": {}, 39 | "34": {}, 40 | "35": {}, 41 | "36": {}, 42 | "39": {} 43 | } 44 | }, 45 | "pwm": { 46 | "units": { 47 | "0": {}, 48 | "1": {}, 49 | "2": {}, 50 | "3": {}, 51 | "4": {}, 52 | "5": {} 53 | } 54 | }, 55 | "uart": { 56 | "units": { 57 | "0": {}, 58 | "1": {} 59 | } 60 | }, 61 | "spi": { 62 | "units": { 63 | "0": {}, 64 | "1": {} 65 | } 66 | }, 67 | "i2c": { 68 | "units": { 69 | "0": {} 70 | } 71 | } 72 | }, 73 | "embeds": { 74 | "ble": {}, 75 | "display": {}, 76 | "switch": {} 77 | } 78 | } 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /obniz/obniz/libs/io_peripherals/ad.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | class PeripheralAD: 4 | def __init__(self, obniz, id): 5 | self.obniz = obniz 6 | self.id = id 7 | self.onchange = None 8 | self._reset() 9 | 10 | def _reset(self): 11 | self.value = 0.0 12 | self.observers = [] 13 | 14 | def add_observer(self, callback): 15 | if callback: 16 | self.observers.append(callback) 17 | 18 | def start(self, callback=None): 19 | self.onchange = callback 20 | obj = {} 21 | obj["ad" + str(self.id)] = {"stream": True} 22 | self.obniz.send(obj) 23 | return self.value 24 | 25 | def get_wait(self): 26 | # get_running_loop() function is preferred on Python >= 3.7 27 | future = asyncio.get_event_loop().create_future() 28 | self.add_observer(future) 29 | obj = {} 30 | obj["ad" + str(self.id)] = {"stream": False} 31 | self.obniz.send(obj) 32 | return future 33 | 34 | def end(self): 35 | self.onchange = None 36 | obj = {} 37 | obj["ad" + str(self.id)] = None 38 | self.obniz.send(obj) 39 | 40 | def notified(self, obj): 41 | self.value = obj 42 | if self.onchange: 43 | self.onchange(obj) 44 | 45 | if len(self.observers) > 0: 46 | item = self.observers.pop(0) 47 | if callable(item): # callback 48 | item(obj) 49 | else: # future 50 | item.set_result(obj) 51 | -------------------------------------------------------------------------------- /obniz/parts/Moving/DCMotor/README.md: -------------------------------------------------------------------------------- 1 | # DCMotor 2 | 3 | Common Brushed DC Motor which moves when connected to +/- and reverse when connected to other side. 4 | 5 | ![photo of DCMotor](./wired.png) 6 | 7 | ## wire({forward, back}) 8 | 9 | connect two wire to an obniz and set io number to forward,back. 10 | If you connect to io 0 and 1, then write a program like a below. 11 | 12 | ```Python 13 | # Python Example 14 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 15 | motor.power(50) 16 | motor.forward() 17 | ``` 18 | ## forward() 19 | 20 | start moving forward. 21 | 22 | ```Python 23 | # Python Example 24 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 25 | 26 | motor.forward() 27 | ``` 28 | ## reverse() 29 | 30 | start moving to back. 31 | 32 | ```Python 33 | # Python Example 34 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 35 | 36 | motor.reverse() 37 | ``` 38 | 39 | ## stop() 40 | 41 | stop a motor. 42 | 43 | 44 | ```Python 45 | # Python Example 46 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 47 | 48 | motor.forward() 49 | await obniz.wait(1000) 50 | motor.stop() 51 | ``` 52 | ## move(bool) 53 | 54 | move a motor regard provided value. 55 | 56 | 57 | ```Python 58 | # Python Example 59 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 60 | 61 | motor.move(True) # = motor.forward() 62 | ``` 63 | ## power(float) 64 | set a motor power. 65 | 66 | default is 30. 67 | 68 | power must be within 0~100. 69 | 70 | ```Python 71 | # Python Example 72 | motor = obniz.wired("DCMotor", {"forward": 0, "back": 1}) 73 | 74 | motor.move(True) # = motor.forward() 75 | ``` -------------------------------------------------------------------------------- /obniz/obniz/libs/measurements/logicanalyzer.py: -------------------------------------------------------------------------------- 1 | from ..utils.util import ObnizUtil 2 | 3 | 4 | class LogicAnalyzer: 5 | def __init__(self, obniz): 6 | self.obniz = obniz 7 | self._reset() 8 | 9 | def _reset(self): 10 | self.onmeasured = None 11 | 12 | def start(self, params): 13 | err = ObnizUtil._required_keys(params, ["io", "interval", "duration"]) 14 | if err: 15 | raise Exception( 16 | "LogicAnalyzer start param '" + err + "' required, but not found " 17 | ) 18 | 19 | self.params = ObnizUtil._key_filter( 20 | params, 21 | ["io", "interval", "duration", "triggerValue", "triggerValueSamples"], 22 | ) 23 | 24 | obj = {} 25 | obj["logic_analyzer"] = { 26 | "io": [self.params["io"]], 27 | "interval": self.params["interval"], 28 | "duration": self.params["duration"], 29 | } 30 | if self.params.get("triggerValueSamples", 0) > 0: 31 | obj["logic_analyzer"]["trigger"] = { 32 | "value": bool(self.params["triggerValue"]), 33 | "samples": self.params["triggerValueSamples"], 34 | } 35 | 36 | self.obniz.send(obj) 37 | 38 | def end(self): 39 | obj = {} 40 | obj["logic_analyzer"] = None 41 | self.obniz.send(obj) 42 | 43 | def notified(self, obj): 44 | if self.onmeasured: 45 | self.onmeasured(obj["data"]) 46 | else: 47 | if not self.measured: 48 | self.measured = [] 49 | 50 | self.measured.push(obj["data"]) 51 | -------------------------------------------------------------------------------- /obniz/parts/Light/LED/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | class LED: 4 | def __init__(self): 5 | self.keys = ['anode', 'cathode'] 6 | self.required_keys = ['anode'] 7 | 8 | @staticmethod 9 | def info(): 10 | return AttrDefault(bool, {'name': 'LED'}) 11 | 12 | def wired(self, obniz): 13 | def get_io(io): 14 | if io and type(io) == 'object': 15 | if type(io['output']) == 'function': 16 | return io 17 | return obniz.get_io(*[io]) 18 | 19 | self.obniz = obniz 20 | self.io_anode = get_io(*[self.params.anode]) 21 | self.io_anode.output(*[False]) 22 | if self.params.cathode: 23 | self.io_cathode = get_io(*[self.params.cathode]) 24 | self.io_cathode.output(*[False]) 25 | self.animation_name = 'Led-' + str(self.params.anode) 26 | 27 | def on(self): 28 | self.end_blink() 29 | self.io_anode.output(*[True]) 30 | 31 | def off(self): 32 | self.end_blink() 33 | self.io_anode.output(*[False]) 34 | 35 | def output(self, value): 36 | if value: 37 | self.on() 38 | else: 39 | self.off() 40 | 41 | def end_blink(self): 42 | self.obniz.io.animation(*[self.animation_name, 'pause']) 43 | 44 | def blink(self, interval=100): 45 | frames = [AttrDefault(bool, {'duration': interval, 'state': lambda index: self.io_anode.output(*[True])}), AttrDefault(bool, {'duration': interval, 'state': lambda index: self.io_anode.output(*[False])})] 46 | self.obniz.io.animation(*[self.animation_name, 'loop', frames]) -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/README.md: -------------------------------------------------------------------------------- 1 | # GP2Y0A21YK0F 2 | Infrared Distance Measurement Unit. 3 | This unit output voltage regarding measured distance. 4 | 5 | ![photo of wired](./image.gif) 6 | 7 | ## wired(obniz, {vcc, gnd, signal}) 8 | 9 | ![photo of wired](./image.jpg) 10 | 11 | ![photo of wired](./wired.png) 12 | 13 | ```Python 14 | # Python Example 15 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 16 | def print_distance(distance): 17 | print("distance:", distance, "mm") 18 | sensor.start(print_distance) 19 | ``` 20 | 21 | ## start(callback(distance)) 22 | measure distance continurously. 23 | Callback will be called when distance changed. 24 | default return unit is "mm". change by calling .unit() 25 | ```Python 26 | # Python Example 27 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 28 | 29 | def print_distance(distance): 30 | print("distance:", distance, "mm") 31 | sensor.start(print_distance) 32 | }) 33 | ``` 34 | 35 | ## [await] get_wait() 36 | 37 | Measure distance once. 38 | 39 | ```Python 40 | # Python Example 41 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 42 | 43 | while True: 44 | val = await sensor.get_wait() 45 | print("distance", val) 46 | await obniz.wait(1000) 47 | ``` 48 | 49 | ## unit(unit) 50 | change unit 51 | 52 | 1. "mm"(default) 53 | 2. "inch" 54 | 55 | are available 56 | 57 | ```Python 58 | # Python Example 59 | sensor = obniz.wired("GP2Y0A21YK0F", {"vcc": 0, "gnd": 1, "signal": 2}) 60 | 61 | sensor.unit("inch") 62 | def print_distance(distance): 63 | print("distance:", distance, "mm") 64 | sensor.start(print_distance) 65 | ``` -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/switch.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | class ObnizSwitch: 4 | def __init__(self, obniz): 5 | self.obniz = obniz 6 | self.onchange = None 7 | self._reset() 8 | 9 | def _reset(self): 10 | self.observers = [] 11 | 12 | def noop(state): 13 | pass 14 | 15 | self.on_change_for_state_wait = noop 16 | 17 | def add_observer(self, future): 18 | if future: 19 | self.observers.append(future) 20 | 21 | def get_wait(self): 22 | # get_running_loop() function is preferred on Python >= 3.7 23 | future = asyncio.get_event_loop().create_future() 24 | self.add_observer(future) 25 | obj = {} 26 | obj["switch"] = "get" 27 | self.obniz.send(obj) 28 | return future 29 | 30 | def state_wait(self, is_pressed): 31 | # get_running_loop() function is preferred on Python >= 3.7 32 | future = asyncio.get_event_loop().create_future() 33 | def on_change_for_state_wait(pressed): 34 | def noop(*args): 35 | pass 36 | if is_pressed == pressed: 37 | self.on_change_for_state_wait = noop 38 | future.set_result(None) 39 | self.on_change_for_state_wait = on_change_for_state_wait 40 | return future 41 | 42 | def notified(self, obj): 43 | self.state = obj["state"] 44 | if self.onchange: 45 | self.onchange(self.state) 46 | 47 | self.on_change_for_state_wait(self.state) 48 | 49 | if len(self.observers) > 0: 50 | future = self.observers.pop(0) 51 | future.set_result(self.state) -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/Button/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | import asyncio 4 | 5 | class Button: 6 | def __init__(self): 7 | def nothing(*args): 8 | pass 9 | self.keys = ['signal', 'gnd'] 10 | self.required_keys = ['signal'] 11 | self.onchange = None 12 | self.on_change_for_state_wait = nothing 13 | 14 | @staticmethod 15 | def info(): 16 | return AttrDefault(bool, {'name': 'Button'}) 17 | 18 | def wired(self, obniz): 19 | self.io_signal = obniz.get_io(*[self.params.signal]) 20 | if obniz.is_valid_io(*[self.params.gnd]): 21 | self.io_supply = obniz.get_io(*[self.params.gnd]) 22 | self.io_supply.output(*[False]) 23 | self.io_signal.pull(*['5v']) 24 | def getval(value): 25 | self.is_pressed = (value == False) 26 | if self.onchange: 27 | self.onchange(value == False) 28 | self.on_change_for_state_wait(value == False) 29 | self.io_signal.input(getval) 30 | 31 | async def is_pressed_wait(self): 32 | ret = await self.io_signal.input_wait() 33 | return ret == False 34 | 35 | def state_wait(self, is_pressed): 36 | # get_running_loop() function is preferred on Python >= 3.7 37 | future = asyncio.get_event_loop().create_future() 38 | def onpress(pressed): 39 | def nothing(*args): 40 | pass 41 | if is_pressed == pressed: 42 | self.on_change_for_state_wait = nothing 43 | future.set_result(None) 44 | self.on_change_for_state_wait = onpress 45 | return future -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | from codecs import open 3 | from os import path 4 | import re 5 | 6 | package_name = "obniz" 7 | root_dir = path.abspath(path.dirname(__file__)) 8 | 9 | with open("README.md", "r") as fh: 10 | long_description = fh.read() 11 | 12 | with open(path.join(root_dir, package_name, '__init__.py')) as f: 13 | init_text = f.read() 14 | # license = re.search(r'__license__\s*=\s*[\'\"](.+?)[\'\"]', init_text).group(1) 15 | author = re.search(r'__author__\s*=\s*[\'\"](.+?)[\'\"]', init_text).group(1) 16 | author_email = re.search(r'__author_email__\s*=\s*[\'\"](.+?)[\'\"]', init_text).group(1) 17 | url = re.search(r'__url__\s*=\s*[\'\"](.+?)[\'\"]', init_text).group(1) 18 | 19 | with open(path.join(root_dir, package_name, package_name, '__version__.py')) as f: 20 | init_text = f.read() 21 | version = re.search(r'__version__\s*=\s*[\'\"](.+?)[\'\"]', init_text).group(1) 22 | 23 | setuptools.setup( 24 | name=package_name, 25 | version=version, 26 | author=author, 27 | author_email=author_email, 28 | description="obniz sdk for python", 29 | long_description=long_description, 30 | long_description_content_type="text/markdown", 31 | url=url, 32 | packages=setuptools.find_packages(), 33 | include_package_data=True, 34 | install_requires=[ 35 | 'pyee==6.0.0', 36 | 'websockets==7.0', 37 | 'attrdict==2.0.1', 38 | 'semver==2.8.1' 39 | ], 40 | classifiers=[ 41 | "Programming Language :: Python :: 3", 42 | 'Programming Language :: Python :: 3.6', 43 | 'Programming Language :: Python :: 3.7', 44 | "Operating System :: OS Independent", 45 | ], 46 | ) 47 | -------------------------------------------------------------------------------- /obniz/obniz/libs/measurements/measure.py: -------------------------------------------------------------------------------- 1 | from ..utils.util import ObnizUtil 2 | 3 | 4 | class ObnizMeasure: 5 | def __init__(self, obniz): 6 | self.obniz = obniz 7 | self._reset() 8 | 9 | def _reset(self): 10 | self.observers = [] 11 | 12 | def echo(self, params): 13 | err = ObnizUtil._required_keys( 14 | params, ["io_pulse", "pulse", "pulse_width", "io_echo", "measure_edges"] 15 | ) 16 | if err: 17 | raise Exception( 18 | "Measure start param '" + err + "' required, but not found " 19 | ) 20 | 21 | self.params = ObnizUtil._key_filter( 22 | params, 23 | [ 24 | "io_pulse", 25 | "pulse", 26 | "pulse_width", 27 | "io_echo", 28 | "measure_edges", 29 | "timeout", 30 | "callback", 31 | ], 32 | ) 33 | 34 | echo = {} 35 | echo["io_pulse"] = self.params["io_pulse"] 36 | echo["pulse"] = self.params["pulse"] 37 | echo["pulse_width"] = self.params["pulse_width"] 38 | echo["io_echo"] = self.params["io_echo"] 39 | echo["measure_edges"] = self.params["measure_edges"] 40 | if type(self.params.get("timeout")) is int: 41 | echo["timeout"] = self.params["timeout"] 42 | 43 | self.obniz.send({"measure": {"echo": echo}}) 44 | 45 | if "callback" in self.params: 46 | self.observers.append(self.params["callback"]) 47 | 48 | def notified(self, obj): 49 | if len(self.observers): 50 | callback = self.observers.pop(0) 51 | callback(obj["echo"]) 52 | -------------------------------------------------------------------------------- /obniz/parts/Light/LED/README-ja.md: -------------------------------------------------------------------------------- 1 | # LED 2 | LEDは電流を流すことで光る半導体です。電球よりもずっと少ない電流で光ります。部品にはプラスとマイナスの端子があります。逆だと電流が流れず光りません。プラスの端子は「アノード」マイナスの端子は「カソード」と言われています。プラスの端子のほうが足が長くなっています(これは他の電子部品でも同じです) 3 | 4 | ![](./led.jpg) 5 | 6 | ## obniz.wired("LED", {anode, cathode}) 7 | 2本の足をObnizのピンにそれぞれ繋ぎます。LEDのプラス(足の長い方。アノードといいます)をObnizの0ピンに。マイナスをObnizの1ピンに繋いだ場合、プログラムでは以下のように設定します 8 | 9 | ![](./wired.png) 10 | 11 | 12 | ```Python 13 | # Python Example 14 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) # io0 is connected to anode, io1 is cathode 15 | led.on() 16 | ``` 17 | 18 | もしLEDのプラスだけをObnizにつなぎ、マイナスはどこかのマイナスにつながっている場合は1ピンだけの指定でOKです 19 | 20 | ```Python 21 | # Python Example 22 | led = obniz.wired("LED", {"anode": 0}) # io0 is anode. cathode is connected obniz GND other way. 23 | led.on() 24 | ``` 25 | ## on() 26 | LEDを点灯させます。 27 | 28 | ```Python 29 | # Python Example 30 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 31 | led.on() 32 | ``` 33 | 34 | 35 | ![](./led_on.jpg) 36 | 37 | ## off() 38 | LEDを消灯させます 39 | 40 | ```Python 41 | # Python Example 42 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 43 | led.off() 44 | ``` 45 | 46 | ## output(bool) 47 | valueに従ってledをon,offします 48 | ```Python 49 | # Python Example 50 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 51 | led.output(True) 52 | ``` 53 | 54 | ## blink(interval_ms) 55 | LEDを点滅させます。interval_msで指定した場合はその時間で点滅します。 56 | 57 | ```Python 58 | # Python Example 59 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 60 | led.blink() # 100msec 61 | ``` 62 | ## end_blink() 63 | LEDの点滅をやめます。LEDは最後の状態で止まります。 64 | 65 | ```Python 66 | # Python Example 67 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 68 | led.blink() 69 | led.end_bllink() 70 | ``` -------------------------------------------------------------------------------- /obniz/parts/Light/LED/README.md: -------------------------------------------------------------------------------- 1 | # LED 2 | LED bright by supplying voltage. 3 | It has two pins anode and cathode. 4 | connect anode to plus and cathode to minus then it bright. 5 | anode is a little bit longer than cathode. 6 | ![](./led.jpg) 7 | 8 | ## obniz.wired("LED", {anode, cathode}) 9 | anode is longer leg. 10 | specify obniz io numbers you connected LED. 11 | 12 | ![](./wired.png) 13 | 14 | ```Python 15 | # Python Example 16 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) # io0 is connected to anode, io1 is cathode 17 | led.on() 18 | ``` 19 | 20 | 21 | ```Python 22 | # Python Example 23 | led = obniz.wired("LED", {"anode": 0}) # io0 is anode. cathode is connected obniz GND other way. 24 | led.on() 25 | ``` 26 | ## on() 27 | Simply, Turning on a LED. 28 | It provide 5V to LED. 29 | 30 | ```Python 31 | # Python Example 32 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 33 | led.on() 34 | ``` 35 | 36 | ![](./led_on.jpg) 37 | 38 | ## off() 39 | Turning off a LED. 40 | 41 | ```Python 42 | # Python Example 43 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 44 | led.off() 45 | ``` 46 | 47 | ## output(bool) 48 | on, off a LED regarding argument. 49 | 50 | ```Python 51 | # Python Example 52 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 53 | led.output(True) 54 | ``` 55 | 56 | ## blink(interval_ms) 57 | Start blinking a LED. 58 | default interval is 100msec 59 | ```Python 60 | # Python Example 61 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 62 | led.blink() # 100msec 63 | ``` 64 | 65 | ## end_blink() 66 | Stop blinking. 67 | LED stops last state. 68 | ```Python 69 | # Python Example 70 | led = obniz.wired("LED", {"anode": 0, "cathode": 1}) 71 | led.blink() 72 | led.end_bllink() 73 | ``` -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_remote_service.py: -------------------------------------------------------------------------------- 1 | from .ble_helper import BleHelper 2 | from .ble_remote_attribute_abstract import BleRemoteAttributeAbstract 3 | from .ble_remote_characteristic import BleRemoteCharacteristic 4 | 5 | 6 | class BleRemoteService(BleRemoteAttributeAbstract): 7 | def __init__(self, obj): 8 | super().__init__(obj) 9 | 10 | @property 11 | def parent_name(self): 12 | return "peripheral" 13 | 14 | @property 15 | def children_class(self): 16 | return BleRemoteCharacteristic 17 | 18 | @property 19 | def children_name(self): 20 | return "characteristics" 21 | 22 | # def addCharacteristic(self, params): 23 | # return self.addChild(params) 24 | 25 | # getCharacteristic(params) { 26 | # return self.getChild(params) 27 | # } 28 | 29 | def discover_all_characteristics(self): 30 | return self.discover_children() 31 | 32 | # discoverAllCharacteristicsWait() { 33 | # return self.discover_childrenWait() 34 | # } 35 | 36 | def discover_children(self): 37 | obj = { 38 | "ble": { 39 | "get_characteristics": { 40 | "address": self.get_peripheral().address, 41 | "service_uuid": BleHelper.uuid_filter(self.uuid), 42 | } 43 | } 44 | } 45 | self.parent.obniz.send(obj) 46 | 47 | def ondiscover(self, characteristic): 48 | self.ondiscovercharacteristic(characteristic) 49 | 50 | # ondiscoverfinished(characteristics) { 51 | # self.ondiscovercharacteristicfinished(characteristics) 52 | # } 53 | 54 | def ondiscovercharacteristic(self, characteristic): 55 | pass 56 | 57 | # ondiscovercharacteristicfinished() {} 58 | -------------------------------------------------------------------------------- /obniz/obniz/libs/hw/obnizb2.json: -------------------------------------------------------------------------------- 1 | { 2 | "rev": "2", 3 | "hw": "obnizb2", 4 | "peripherals": { 5 | "io": { 6 | "units": { 7 | "0": {}, 8 | "1": {}, 9 | "2": {}, 10 | "3": {}, 11 | "4": {}, 12 | "5": {}, 13 | "6": {}, 14 | "7": {}, 15 | "8": {}, 16 | "9": {}, 17 | "10": {}, 18 | "11": {} 19 | } 20 | }, 21 | "ad": { 22 | "units": { 23 | "0": {}, 24 | "1": {}, 25 | "2": {}, 26 | "3": {}, 27 | "4": {}, 28 | "5": {}, 29 | "6": {}, 30 | "7": {}, 31 | "8": {}, 32 | "9": {}, 33 | "10": {}, 34 | "11": {} 35 | } 36 | }, 37 | "pwm": { 38 | "units": { 39 | "0": {}, 40 | "1": {}, 41 | "2": {}, 42 | "3": {}, 43 | "4": {}, 44 | "5": {} 45 | } 46 | }, 47 | "uart": { 48 | "units": { 49 | "0": {}, 50 | "1": {} 51 | } 52 | }, 53 | "spi": { 54 | "units": { 55 | "0": {}, 56 | "1": {} 57 | } 58 | }, 59 | "i2c": { 60 | "units": { 61 | "0": {} 62 | } 63 | } 64 | }, 65 | "embeds": { 66 | "ble": {}, 67 | "display": { 68 | "paper_white": true, 69 | "raw_alternate": true, 70 | "width": 128, 71 | "height": 64, 72 | "color_depth": [ 73 | 1 74 | ] 75 | }, 76 | "switch": {} 77 | }, 78 | "protocol": { 79 | "tcp": { 80 | "units": { 81 | "0": {}, 82 | "1": {}, 83 | "2": {}, 84 | "3": {}, 85 | "4": {}, 86 | "5": {}, 87 | "6": {}, 88 | "7": {} 89 | } 90 | } 91 | }, 92 | "extraInterface": {} 93 | } -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/display.py: -------------------------------------------------------------------------------- 1 | class Display: 2 | def __init__(self, obniz): 3 | self.obniz = obniz 4 | self.width = 128 5 | self.height = 64 6 | 7 | # def font: 8 | 9 | def clear(self): 10 | obj = {} 11 | obj['display'] = { 12 | "clear": True 13 | } 14 | self.obniz.send(obj) 15 | 16 | # def pos: 17 | 18 | def print(self, text): 19 | obj = {} 20 | obj['display'] = { 21 | "text": text 22 | } 23 | self.obniz.send(obj) 24 | 25 | # def line 26 | # def rect 27 | # def circle 28 | 29 | def qr(self, text, correction="M"): 30 | obj = {} 31 | obj['display'] = { 32 | "qr": { 33 | "text": text, 34 | "correction": correction 35 | } 36 | } 37 | self.obniz.send(obj) 38 | 39 | def raw(self, data): 40 | obj = {} 41 | obj['display'] = { 42 | "raw": data 43 | } 44 | self.obniz.send(obj) 45 | 46 | def set_pin_name(self, io, module_name, func_name): 47 | obj = {} 48 | obj['display'] = { 49 | 'pin_assign': { 50 | io: { 51 | "module_name": module_name, 52 | "pin_name": func_name 53 | } 54 | } 55 | } 56 | self.obniz.send(obj) 57 | 58 | def set_pin_names(self, module_name, data): 59 | obj = {} 60 | obj['display'] = { 61 | 'pin_assign': {} 62 | } 63 | for key in data: 64 | obj['display']['pin_assign'][key] = { 65 | "module_name": module_name, 66 | "pin_name": data[key] 67 | } 68 | if data: 69 | self.obniz.send(obj) -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_remote_attribute_abstract.py: -------------------------------------------------------------------------------- 1 | from .ble_attribute_abstract import BleAttributeAbstract 2 | 3 | 4 | class BleRemoteAttributeAbstract(BleAttributeAbstract): 5 | def __init__(self, params): 6 | super().__init__(params) 7 | 8 | self.isRemote = False 9 | self.discoverdOnRemote = False 10 | 11 | @property 12 | def ws_child_uuid_name(self): 13 | children_name = self.children_name 14 | if not children_name: 15 | return None 16 | 17 | children_name = children_name[:-1] 18 | return children_name + "_uuid" 19 | 20 | def get_child(self, uuid): 21 | obj = super().get_child(uuid) 22 | if not obj: 23 | obj = self.add_child({"uuid": uuid}) 24 | 25 | return obj 26 | 27 | # discoverChildren() {} 28 | 29 | # discoverChildrenWait() { 30 | # return new Promise(resolve => { 31 | # self.emitter.once('discoverfinished', () => { 32 | # children = self.children.filter(elm => { 33 | # return elm.discoverdOnRemote 34 | # }) 35 | # resolve(children) 36 | # }) 37 | # self.discoverChildren() 38 | # }) 39 | # } 40 | 41 | # 42 | # CALLBACKS 43 | # 44 | def ondiscover(self): 45 | pass 46 | 47 | # ondiscoverfinished() {} 48 | 49 | def notify_from_server(self, notify_name, params): 50 | super().notify_from_server(notify_name, params) 51 | if notify_name == "discover": 52 | child = self.get_child(params[self.ws_child_uuid_name]) 53 | child.discoverdOnRemote = True 54 | child.properties = params.get("properties", []) 55 | self.ondiscover(child) 56 | elif notify_name == "discoverfinished": 57 | children = [elm for elm in self.children if elm.discoverdOnRemote] 58 | self.ondiscoverfinished(children) 59 | -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/GP2Y0A21YK0F/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | import asyncio 4 | import math 5 | 6 | class GP2Y0A21YK0F: 7 | def __init__(self): 8 | self.keys = ['vcc', 'gnd', 'signal'] 9 | self.required_keys = ['signal'] 10 | self.display_io_names = AttrDefault(bool, {'vcc': 'vcc', 'gnd': 'gnd', 'signal': 'signal'}) 11 | self._unit = 'mm' 12 | 13 | @staticmethod 14 | def info(): 15 | return AttrDefault(bool, {'name': 'GP2Y0A21YK0F'}) 16 | 17 | def wired(self, obniz): 18 | self.obniz = obniz 19 | obniz.set_vcc_gnd(*[self.params.vcc, self.params.gnd, '5v']) 20 | self.io_signal = obniz.get_io(*[self.params.signal]) 21 | self.io_signal.end() 22 | self.ad_signal = obniz.get_ad(*[self.params.signal]) 23 | 24 | def start(self, callback): 25 | def ret_distance(val): 26 | distance = self._volt2distance(val) 27 | if callable(callback): 28 | callback(distance) 29 | self.ad_signal.start(ret_distance) 30 | 31 | def _volt2distance(self, val): 32 | if val <= 0: 33 | val = 0.001 34 | distance = 19988.34 * math.pow(*[val / 5.0 * 1024, -1.25214]) * 10 35 | if self._unit == 'mm': 36 | distance = int(*[distance * 10]) / 10 37 | else: 38 | distance *= 0.0393701 39 | distance = int(*[distance * 1000]) / 1000 40 | return distance 41 | 42 | def get_wait(self): 43 | async def get_distance(): 44 | val = await self.ad_signal.get_wait() 45 | distance = self._volt2distance(val) 46 | return distance 47 | return get_distance() 48 | 49 | def unit(self, unit): 50 | if unit == 'mm': 51 | self._unit = 'mm' 52 | elif unit == 'inch': 53 | self._unit = 'inch' 54 | else: 55 | raise Exception('unknown unit ' + str(unit)) -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | import asyncio 4 | 5 | class JoyStick: 6 | def __init__(self): 7 | self.keys = ['sw', 'y', 'x', 'vcc', 'gnd', 'i2c'] 8 | self.required_keys = ['sw', 'y', 'x'] 9 | self.pins = self.keys or ['sw', 'y', 'x', 'vcc', 'gnd'] 10 | self.pinname = AttrDefault(bool, {'sw': 'sw12'}) 11 | self.short_name = 'joyS' 12 | self.onchangex = self.onchangey = self.onchangesw = None 13 | 14 | @staticmethod 15 | def info(): 16 | return AttrDefault(bool, {'name': 'JoyStick'}) 17 | 18 | def wired(self, obniz): 19 | self.obniz = obniz 20 | obniz.set_vcc_gnd(*[self.params.vcc, self.params.gnd, '5v']) 21 | self.io_sig_sw = obniz.get_io(*[self.params.sw]) 22 | self.ad_x = obniz.get_ad(*[self.params.x]) 23 | self.ad_y = obniz.get_ad(*[self.params.y]) 24 | self.io_sig_sw.pull(*['5v']) 25 | 26 | def setx(val): 27 | self.position_x = val / 5.0 28 | if self.onchangex: 29 | self.onchangex(self.position_x * 2 - 1) 30 | self.ad_x.start(setx) 31 | def sety(val): 32 | self.position_y = val / 5.0 33 | if self.onchangey: 34 | self.onchangey(self.position_y * 2 - 1) 35 | self.ad_y.start(sety) 36 | def setsw(val): 37 | self.is_pressed = val == False 38 | if self.onchangesw: 39 | self.onchangesw(self.is_pressed) 40 | self.io_sig_sw.input(setsw) 41 | 42 | async def is_pressed_wait(self): 43 | ret = await self.io_sig_sw.input_wait() 44 | return ret == False 45 | 46 | async def get_x_wait(self): 47 | value = await self.ad_x.get_wait() 48 | self.position_x = value / 5.0 49 | return (self.position_x * 2 - 1) 50 | 51 | async def get_y_wait(self): 52 | value = await self.ad_y.get_wait() 53 | self.position_y = value / 5.0 54 | return (self.position_y * 2 - 1) -------------------------------------------------------------------------------- /tests/libs/embeds/test_switch.py: -------------------------------------------------------------------------------- 1 | from time import sleep 2 | 3 | from ...utils import assert_finished, assert_obniz, assert_send, receive_json 4 | 5 | 6 | class TestObnizSwitch: 7 | def test_onchange(self, mocker, obniz): 8 | stub = mocker.stub() 9 | obniz.switch.onchange = stub 10 | assert_obniz(obniz) 11 | assert stub.call_count == 0 12 | 13 | receive_json(obniz, [{"switch": {"state": "none"}}]) 14 | 15 | assert stub.call_count == 1 16 | assert stub.call_args[0][0] == "none" 17 | 18 | assert_finished(obniz) 19 | 20 | # it.skip('not value changd , but it detect "action":get on onchange func(self, obniz): 21 | # stub = sinon.stub() 22 | # obniz.switch.onchange = stub 23 | # assert_obniz(obniz) 24 | # assert stub.call_count == 0) 25 | 26 | # receive_json(obniz, [ 27 | # { "switch": { "state": 'push', "action": 'get' } }, 28 | # ]) 29 | 30 | # assert stub.call_count == 0) 31 | 32 | # assert_finished(obniz) 33 | # }) 34 | 35 | def test_input_wait_left(self, obniz): 36 | def callback(result): 37 | assert result == "left" 38 | 39 | obniz.switch.get_wait().add_done_callback(callback) 40 | 41 | assert_obniz(obniz) 42 | assert_send(obniz, [{"switch": "get"}]) 43 | assert_finished(obniz) 44 | 45 | sleep(0.01) 46 | receive_json(obniz, [{"switch": {"state": "left", "action": "get"}}]) 47 | 48 | def test_state_wait(self, obniz): 49 | before = True 50 | 51 | def callback(result): 52 | assert before is False 53 | 54 | obniz.switch.state_wait("push").add_done_callback(callback) 55 | 56 | assert_obniz(obniz) 57 | receive_json(obniz, [{"switch": {"state": "left"}}]) 58 | assert_finished(obniz) 59 | 60 | receive_json(obniz, [{"switch": {"state": "right"}}]) 61 | assert_finished(obniz) 62 | 63 | sleep(0.01) 64 | before = False 65 | receive_json(obniz, [{"switch": {"state": "push"}}]) 66 | -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/README-ja.md: -------------------------------------------------------------------------------- 1 | # JoyStick 2 | X軸Y軸とプッシュスイッチをもつジョイスティックです。出力がアナログ値の一般的なものに対して利用できます。 3 | 4 | ![](./joystick.jpg) 5 | 6 | ## wired(obniz, {sw, x, y, vcc, gnd}) 7 | obnizと接続します。vccはジョイスティックの5v入力へ。gndはマイナスへ接続します。 8 | sw: スイッチ 9 | x: x軸のアナログ値出力 10 | y: y軸のアナログ値出力 11 | へ接続して下さい 12 | 13 | ### 注意!いろいろな製品があります! 14 | ジョイスティックは製品によってピン配置が違うので注意して下さい。 15 | 例えば、obnizでよく使われるのはこのピンアサインのものですが 16 | ![](./joystick_pins.jpg) 17 | 18 | 他にもこのようなピンアサインのものもあります。 19 | 20 | ![photo of wired](./wired.png) 21 | 22 | 23 | ```Python 24 | 25 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 26 | 27 | # or 28 | 29 | joystick = obniz.wired("JoyStick", {"gnd": 0, "sw": 1, "y": 2, "x": 3, "vcc": 4}) 30 | 31 | # and mores 32 | ``` 33 | ## onchangex = callback(angle) 34 | ## onchangey = callback(angle) 35 | それぞれX軸,Y軸方向へ動いた場合に呼ばれる関数を指定できます。 36 | ```Python 37 | # Python Example 38 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 39 | def onchangex(val): 40 | print(val) 41 | joystick.onchangex = onchangex 42 | def onchangey(val): 43 | print(val) 44 | joystick.onchangey = onchangey 45 | ``` 46 | 47 | ## onchangesw = callback(pressed) 48 | ボタンが押されたり離された時に呼ばれます。 49 | ```Python 50 | # Python Example 51 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 52 | def onchangesw(pressed): 53 | print(pressed) 54 | joystick.onchangesw = onchangesw 55 | ``` 56 | 57 | 58 | ## [await] is_pressed_wait() 59 | ボタンが押されているかどうかを一度だけ取得します 60 | 61 | ```Python 62 | # Python Example 63 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 64 | is_pressed = await joystick.is_pressed_wait() 65 | if is_pressed: 66 | print("PRESSED") 67 | 68 | ``` 69 | 70 | 71 | ## [await] get_x_wait() 72 | ## [await] get_y_wait() 73 | 74 | X,Yそれぞれの傾きを一度だけ取得します 75 | 76 | ```Python 77 | # Python Example 78 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 79 | x = await joystick.get_x_wait() 80 | y = await joystick.get_y_wait() 81 | 82 | print("x:", x, "y:", y) 83 | 84 | ``` -------------------------------------------------------------------------------- /obniz/parts/Moving/DCMotor/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | class DCMotor: 4 | def __init__(self): 5 | self.keys = ['forward', 'back'] 6 | self.required_keys = ['forward', 'back'] 7 | 8 | @staticmethod 9 | def info(): 10 | return AttrDefault(bool, {'name': 'DCMotor'}) 11 | 12 | def wired(self, obniz): 13 | self.status = AttrDefault(bool, {'direction': None, 'power': None}) 14 | self.pwm1_io_num = self.params.forward 15 | self.pwm2_io_num = self.params.back 16 | self.pwm1 = obniz.get_free_pwm() 17 | self.pwm1.start(*[AttrDefault(bool, {'io': self.pwm1_io_num})]) 18 | self.pwm1.freq(*[100000]) 19 | self.pwm2 = obniz.get_free_pwm() 20 | self.pwm2.start(*[AttrDefault(bool, {'io': self.pwm2_io_num})]) 21 | self.pwm2.freq(*[100000]) 22 | self.power(*[30]) 23 | 24 | def forward(self): 25 | self.move(*[True]) 26 | 27 | def reverse(self): 28 | self.move(*[False]) 29 | 30 | def stop(self): 31 | if self.status.direction == None: 32 | return 33 | self.status.direction = None 34 | self.pwm1.duty(*[0]) 35 | self.pwm2.duty(*[0]) 36 | 37 | def move(self, forward): 38 | if forward: 39 | if self.status.direction == True: 40 | return 41 | self.status.direction = True 42 | else: 43 | if self.status.direction == False: 44 | return 45 | self.status.direction = False 46 | power = self.power() 47 | self.power(*[0]) 48 | self.power(*[power]) 49 | 50 | def power(self, power=None): 51 | if power == None: 52 | return self.status.power 53 | self.status.power = power 54 | if self.status.direction == None: 55 | self.pwm1.duty(*[0]) 56 | self.pwm2.duty(*[0]) 57 | return 58 | if self.status.direction: 59 | self.pwm1.duty(*[power]) 60 | self.pwm2.duty(*[0]) 61 | else: 62 | self.pwm1.duty(*[0]) 63 | self.pwm2.duty(*[power]) -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_remote_descriptor.py: -------------------------------------------------------------------------------- 1 | from .ble_helper import BleHelper 2 | from .ble_remote_attribute_abstract import BleRemoteAttributeAbstract 3 | 4 | 5 | class BleRemoteDescriptor(BleRemoteAttributeAbstract): 6 | def __init__(self, params): 7 | super().__init__(params) 8 | 9 | @property 10 | def parent_name(self): 11 | return "characteristic" 12 | 13 | def read(self): 14 | obj = { 15 | "ble": { 16 | "read_descriptor": { 17 | "address": self.get_characteristic() 18 | .get_service() 19 | .get_peripheral() 20 | .address, 21 | "service_uuid": BleHelper.uuid_filter( 22 | self.get_characteristic().get_service().uuid 23 | ), 24 | "characteristic_uuid": BleHelper.uuid_filter( 25 | self.get_characteristic().uuid 26 | ), 27 | "descriptor_uuid": BleHelper.uuid_filter(self.uuid), 28 | } 29 | } 30 | } 31 | self.get_characteristic().get_service().get_peripheral().obniz.send(obj) 32 | 33 | def write(self, array, need_response=True): 34 | obj = { 35 | "ble": { 36 | "write_descriptor": { 37 | "address": self.get_characteristic() 38 | .get_service() 39 | .get_peripheral() 40 | .address, 41 | "service_uuid": BleHelper.uuid_filter( 42 | self.get_characteristic().get_service().uuid 43 | ), 44 | "characteristic_uuid": BleHelper.uuid_filter( 45 | self.get_characteristic().uuid 46 | ), 47 | "descriptor_uuid": BleHelper.uuid_filter(self.uuid), 48 | "data": array, 49 | "needResponse": need_response, 50 | } 51 | } 52 | } 53 | self.get_characteristic().get_service().get_peripheral().obniz.send(obj) 54 | -------------------------------------------------------------------------------- /tests/obniz_json_validator.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | 3 | from jsonschema import Draft4Validator, RefResolver 4 | import yaml 5 | 6 | 7 | class ObnizJsonValidator: 8 | # def __init__(self): 9 | # self.use_commands = {}s 10 | # self.cache = {} 11 | 12 | def request_validate(self, request_json, type): 13 | valid = self._validate(request_json, "request") 14 | if not valid: 15 | return {"valid": False, "errors": ["json are valid for no command"]} 16 | else: 17 | # self.use_commands[type] = self.use_commands[type] or [] 18 | # self.use_commands[type].push(commands[0]) 19 | return {"valid": True, "errors": []} 20 | 21 | def response_validate(self, request_json, type): 22 | valid = self._validate(request_json, "response") 23 | if not valid: 24 | return {"valid": False, "errors": ["json are valid for no command"]} 25 | else: 26 | # self.use_commands[type] = self.use_commands[type] or [] 27 | # self.use_commands[type].push(commands[0]) 28 | return {"valid": True, "errors": []} 29 | 30 | def _validate(self, json, type): 31 | class Resolver(RefResolver): 32 | def __init__(self): 33 | super().__init__("", None) 34 | 35 | def resolve_remote(self, uri): 36 | if uri == "/obnizId": 37 | uri = "" 38 | uri = "./json_schema" + uri 39 | if os.path.isdir(uri): 40 | uri += "/index" 41 | uri += ".yml" 42 | 43 | if not os.path.isfile(uri): 44 | uri = "./json_schema/index.yml" 45 | 46 | with open(uri) as f: 47 | result = yaml.load(f, Loader=yaml.FullLoader) 48 | 49 | if self.cache_remote: 50 | self.store[uri] = result 51 | return result 52 | 53 | with open("./json_schema/" + type + "/index.yml") as f: 54 | schema = yaml.load(f, Loader=yaml.FullLoader) 55 | 56 | Draft4Validator(schema, resolver=Resolver()).validate(json) 57 | 58 | return True 59 | 60 | 61 | obniz_json_validator = ObnizJsonValidator() 62 | -------------------------------------------------------------------------------- /_tools/DOCKER-README.md: -------------------------------------------------------------------------------- 1 | 2 | ## コンテナ起動 3 | 下記コマンドにて、コンテナを起動する。 4 | working dir は プロジェクトルート 5 | 6 | ```:ターミナル 7 | $ docker-compose -f ./_tools/docker-compose.yml up -d --build 8 | ``` 9 | 10 | ## 使いたいライブラリを追加 11 | 12 | ### コンテナへ接続 13 | ```:ターミナル 14 | $ docker exec -it obniz_python bash 15 | ``` 16 | 17 | ### インストール 18 | volumeマウント後にしないといけないのでdockerfileにかけない? 19 | 20 | - pipenv install --deploy --dev 21 | 22 | or 23 | ```:ターミナル 24 | $ docker exec -it obniz_python pipenv install --deploy --dev 25 | ``` 26 | 27 | 28 | ### テスト 29 | - pipenv run pytest 30 | 31 | → test_init.pyの2個だけfailになる(ポートの関係) 32 | 33 | or 34 | ```:ターミナル 35 | $ docker exec -it obniz_python pipenv run pytest 36 | ``` 37 | 38 | 39 | ### 実行 40 | - pipenv run python xxx.py 41 | or 42 | ```:ターミナル 43 | $ docker exec -it obniz_python pipenv run python xxx.py 44 | ``` 45 | 46 | 47 | 48 | ## 要らなくなったら... 49 | 利用が終わって不要になったらコンテナごと下記コマンドにて削除する。 50 | 51 | ```:ターミナル 52 | $ docker-compose down 53 | ``` 54 | 55 | #デプロイ 56 | 57 | ## キャッシュ削除 58 | find . -name \*.pyc -delete 59 | 60 | ## ファイル作成 61 | python3 setup.py sdist bdist_wheel 62 | 63 | ## ローカルでpip installテスト 64 | pip install dist/xxxx.whl 65 | 66 | **インストール先は`pip show xxx`で確認** 67 | 68 | ## testアップロード 69 | 70 | twine upload --repository pypitest dist/* 71 | OR 72 | twine upload --repository-url https://test.pypi.org/legacy/ dist/* 73 | 74 | 75 | 76 | ## testpypiでの動作テスト 77 | 78 | pip install --index-url https://test.pypi.org/simple/ obniz 79 | 80 | #### docker立ち上げて動くかテスト 81 | 82 | ``` 83 | $ docker-compose -f ./_tools/docker-compose.yml up -d --build 84 | $ docker exec -it obniz_python_prototypetest bash 85 | 86 | # docker内 87 | pipenv run python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple obniz 88 | pipenv run python prototypetest/main.py 89 | ``` 90 | 91 | ※バージョン指定する場合は `obniz==0.1.0` にかえる 92 | 93 | ### repl.itでのテスト 94 | 95 | pyproject.tomlに追加してテスト 96 | ``` 97 | [[tool.poetry.source]] 98 | name = "test-pypi" 99 | url = "https://test.pypi.org/simple/" 100 | ``` 101 | 102 | ## アップロード 103 | twine upload --repository-url https://upload.pypi.org/legacy/ dist/* 104 | OR 105 | twine upload --repository pypi dist/* 106 | -------------------------------------------------------------------------------- /json_schema/request/ioanimation/init.yml: -------------------------------------------------------------------------------- 1 | ## YAML Template. 2 | ##--- 3 | $schema: http://json-schema.org/draft-04/schema# 4 | id: "/request/ioAnimation/init" 5 | commandExample : {"io":{"animation":{"animation":{"name":"animation-1","status":"loop","states":[{"duration":500,"state":{"io0":true}},{"duration":500,"state":{"io0":false}}]}}}} 6 | 7 | 8 | type: object 9 | required : [animation] 10 | properties: 11 | animation : 12 | type: object 13 | required : [name, status, states] 14 | additionalProperties: false 15 | properties: 16 | name : 17 | type: string 18 | description : "Animation name to use pause/resume" 19 | example : "animation-1" 20 | minLength: 1 21 | maxLength: 254 22 | 23 | status : 24 | type: string 25 | default : loop 26 | enum : 27 | - loop 28 | 29 | states: 30 | type: array 31 | default : [] 32 | items: 33 | type : object 34 | required : [duration, state] 35 | additionalProperties: false 36 | properties : 37 | duration : 38 | type: integer 39 | description : "State duration time(ms)" 40 | minimum : 0 41 | maximum : 60000 42 | example : 500 43 | state : 44 | type: [object, array] 45 | description : "io/pwm commands." 46 | filter : pass_all 47 | example : 48 | - {io0: true} 49 | - [{io0: false}, {io0: true}] 50 | # エラーがわかりにくくなるのでソースコードで実施 51 | # additionalProperties: false 52 | # patternProperties : 53 | # "^io[0-9]$" : {$ref : "/request/io" } 54 | # "^io1[0-1]$" : {$ref : "/request/io" } 55 | # "^pwm[0-5]$" : {$ref : "/request/pwm" } 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /obniz/obniz/obniz_parts.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | from .obniz_connection import ObnizConnection 4 | from .libs.utils.util import ObnizUtil 5 | 6 | _parts = {} 7 | 8 | class ObnizParts(ObnizConnection): 9 | def __init__(self, id, options): 10 | super().__init__(id, options) 11 | 12 | def _parts(self): 13 | return _parts 14 | 15 | @staticmethod 16 | def parts_registrate(*args): 17 | if (len(args) == 1): 18 | _parts[args[0].info()["name"]] = args[0] 19 | elif (len(args) == 2): 20 | _parts[args[0]] = args[1] 21 | else: 22 | pass 23 | 24 | @staticmethod 25 | def parts(name): 26 | if name not in _parts: 27 | raise Exception("unknown parts [" + name + "]") 28 | return _parts[name] 29 | 30 | def wired(self, partsname, param=None): 31 | parts = ObnizParts.parts(partsname) 32 | if not parts: 33 | raise Exception("No such a parts [" + partsname + "] found") 34 | 35 | if parts.keys: 36 | if parts.required_keys: 37 | err = ObnizUtil._required_keys(param, parts.required_keys) 38 | if err: 39 | raise Exception( 40 | partsname + " wired param '" + err + "' required, but not found " 41 | ) 42 | 43 | parts.params = AttrDefault(bool, ObnizUtil._key_filter(param, parts.keys)) 44 | 45 | parts.obniz = self 46 | parts.wired(parts.obniz) 47 | if parts.keys or parts.io_keys: 48 | keys = parts.keys if hasattr(parts, "keys") else parts.io_keys 49 | display_parts_name = parts.display_name if hasattr(parts, "display_name") else partsname 50 | io_names = {} 51 | 52 | for key in keys: 53 | pin_name = key 54 | io = param[pin_name] if pin_name in param else None 55 | if self.is_valid_io(io): 56 | if hasattr(parts, "display_io_names") and parts.display_io_names[pin_name]: 57 | pin_name = parts.display_io_names[pin_name] 58 | io_names[io] = pin_name 59 | 60 | self.display.set_pin_names(display_parts_name, io_names) 61 | 62 | return parts 63 | -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/HC-SR04/README-ja.md: -------------------------------------------------------------------------------- 1 | # HC-SR04 2 | 超音波を利用した距離センサーです。 3 | 4 | ## wired(obniz, {vcc, trigger, echo, gnd}) 5 | 6 | ![photo of wired](./wired.png) 7 | ```Python 8 | # Python Example 9 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 10 | def callback(distance): 11 | print("distance", distance, "mm") 12 | hcsr04.measure(callback) 13 | ``` 14 | 15 | ## measure(callback(distance)) 16 | 距離を計測します、計測が完了したらcallback関数が呼ばれます。 17 | 距離の単位はmmで、unit()関数でinchに変えることも出来ます。 18 | もし、反射してくる超音波を受け取れなかった場合はNoneが返ります。 19 | ```Python 20 | # Python Example 21 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 22 | def callback(distance): 23 | print("distance", distance, "mm") 24 | hcsr04.measure(callback) 25 | ``` 26 | 27 | ## [await] measure_wait() 28 | measure()と同様ですが、こちらはfutureを返す関数です。 29 | 30 | ```Python 31 | # Python Example 32 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 33 | while True: 34 | avg = 0 35 | count = 0 36 | for i in range(i): # measure three time. and calculate average 37 | val = await hcsr04.measure_wait() 38 | if val: 39 | count += 1 40 | avg += val 41 | if count > 1: 42 | avg /= count 43 | print(avg) 44 | await obniz.wait(100) 45 | ``` 46 | 47 | ## temp 48 | 超音波は温度により進む速度が違います。 49 | デフォルトで15度で計算していますが、変更して正しい値にすることでより精度の高い結果が欲しい場合はtempから調整します。 50 | ```Python 51 | # Python Example 52 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 53 | hcsr04.temp = 36 54 | distance = await hcsr04.measure_wait() 55 | print("distance", distance, "mm") 56 | ``` 57 | 58 | ## reset_alltime 59 | 一部のHC-SR04では、計測するときに毎回電源のON-OFFが必要なものがあります。 60 | もし、計測がうまくいかない場合はこのプロパティをtrueにすることで 61 | 自動的にリセットを行います。 62 | ```Python 63 | # Python Example 64 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 65 | hcsr04.reset_alltime = True 66 | distance = await hcsr04.measure_wait() 67 | print("distance", distance, "mm") 68 | ``` 69 | 70 | ## unit(unit) 71 | 単位を変更します。 72 | 73 | 1. "mm"(default) 74 | 2. "inch" 75 | 76 | が利用可能です。 77 | 78 | ```Python 79 | # Python Example 80 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 81 | hcsr04.unit("inch") 82 | def callback(distance): 83 | print("distance", distance, "inch") 84 | hcsr04.measure(callback) 85 | ``` -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_peripheral.py: -------------------------------------------------------------------------------- 1 | from .ble_helper import BleHelper 2 | from .ble_service import BleService 3 | 4 | 5 | class BlePeripheral: 6 | def __init__(self, obniz): 7 | self.obniz = obniz 8 | self.services = [] 9 | 10 | def add_service(self, obj): 11 | if not isinstance(obj, BleService): 12 | obj = BleService(obj) 13 | 14 | self.services.append(obj) 15 | obj.peripheral = self 16 | self.obniz.send({"ble": {"peripheral": {"services": [obj]}}}) 17 | 18 | def set_json(self, json): 19 | if "services" in json: 20 | for service in json["services"]: 21 | self.add_service(service) 22 | 23 | def get_service(self, uuid): 24 | uuid = BleHelper.uuid_filter(uuid) 25 | return next( 26 | iter( 27 | [ 28 | element 29 | for element in self.services 30 | if BleHelper.uuid_filter(element.uuid) == uuid 31 | ] 32 | ), 33 | None, 34 | ) 35 | 36 | # removeService(uuid) { 37 | # self.services = self.services.filter(function(element) { 38 | # return BleHelper.uuid_filter(element.uuid) !== uuid 39 | # }) 40 | # } 41 | 42 | # stopAllService() { 43 | # self.obniz.send({ 44 | # "ble": { 45 | # "peripheral": null, 46 | # }, 47 | # }) 48 | # self.services = [] 49 | # } 50 | 51 | def to_json(self): 52 | return {"services": self.services} 53 | 54 | def find_characteristic(self, param): 55 | service_uuid = BleHelper.uuid_filter(param["service_uuid"]) 56 | characteristic_uuid = BleHelper.uuid_filter(param["characteristic_uuid"]) 57 | s = self.get_service(service_uuid) 58 | if s: 59 | return s.get_characteristic(characteristic_uuid) 60 | 61 | return None 62 | 63 | def find_descriptor(self, param): 64 | descriptor_uuid = BleHelper.uuid_filter(param["descriptor_uuid"]) 65 | c = self.find_characteristic(param) 66 | if c: 67 | return c.get_descriptor(descriptor_uuid) 68 | 69 | return None 70 | 71 | def end(self): 72 | self.obniz.send({"ble": {"peripheral": None}}) 73 | 74 | # onconnectionupdates() {} 75 | 76 | # onerror() {} 77 | -------------------------------------------------------------------------------- /obniz/parts/DistanceSensor/HC-SR04/README.md: -------------------------------------------------------------------------------- 1 | # HC-SR04 2 | Ultrasonic Distance Measurement Unit. 3 | 4 | 5 | ## wired(obniz, {vcc, trigger, echo, gnd}) 6 | 7 | ![photo of wired](./wired.png) 8 | ```Python 9 | # Python Example 10 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 11 | def callback(distance): 12 | print("distance", distance, "mm") 13 | hcsr04.measure(callback) 14 | ``` 15 | 16 | ## measure(callback(distance)) 17 | measure distance. 18 | default return unit is "mm". change by calling unit(). 19 | ```Python 20 | # Python Example 21 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 22 | def callback(distance): 23 | print("distance", distance, "mm") 24 | hcsr04.measure(callback) 25 | ``` 26 | 27 | ## [await] measure_wait() 28 | This is async/await version of measure() 29 | 30 | ```Python 31 | # Python Example 32 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 33 | while True: 34 | avg = 0 35 | count = 0 36 | for i in range(i): # measure three time. and calculate average 37 | val = await hcsr04.measure_wait() 38 | if val: 39 | count += 1 40 | avg += val 41 | if count > 1: 42 | avg /= count 43 | print(avg) 44 | await obniz.wait(100) 45 | ``` 46 | 47 | 48 | ## temp 49 | The speed of ultrasonic depends on temperature. 50 | By default calculation temp is 15 degree(Celsius). Change this to get more accurate result. 51 | ```Python 52 | # Python Example 53 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 54 | hcsr04.temp = 36 55 | distance = await hcsr04.measure_wait() 56 | print("distance", distance, "mm") 57 | ``` 58 | 59 | ## reset_alltime 60 | Some HC-SR04 needs power reset after measure. 61 | set true to this property to automatic power reset after each measurement. 62 | ```Python 63 | # Python Example 64 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 65 | hcsr04.reset_alltime = True 66 | distance = await hcsr04.measure_wait() 67 | print("distance", distance, "mm") 68 | ``` 69 | 70 | ## unit(unit) 71 | change unit 72 | 73 | 1. "mm"(default) 74 | 2. "inch" 75 | 76 | are available 77 | 78 | ```Python 79 | # Python Example 80 | hcsr04 = obniz.wired("HC-SR04", {"gnd": 0, "echo": 1, "trigger": 2, "vcc": 3}) 81 | hcsr04.unit("inch") 82 | def callback(distance): 83 | print("distance", distance, "inch") 84 | hcsr04.measure(callback) 85 | ``` -------------------------------------------------------------------------------- /obniz/parts/MovementSensor/JoyStick/README.md: -------------------------------------------------------------------------------- 1 | # JoyStick 2 | 3 | X and Y direction with Push Switch Joystick. 4 | This class accept only analog output. 5 | 6 | ![](./joystick.jpg) 7 | 8 | ## wired(obniz, {sw, x, y, vcc, gnd}) 9 | 10 | connect to an obniz. 11 | 12 | 1. vcc: power supply 13 | 2. gnd: power supply 14 | 3. sw: push switch 15 | 4. x: X axis analog output 16 | 5. Y: T axis analog output 17 | 18 | ### Attention! pins assing is diffrerent in each product! 19 | There are some joystick and these pin assigns are different. 20 | 21 | For example, A joystick commonly used with obniz is below. 22 | 23 | ![](./joystick_pins.jpg) 24 | 25 | But another one has a pinassign like 26 | 27 | ![photo of wired](./wired.png) 28 | 29 | 30 | ```Python 31 | 32 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 33 | 34 | # or 35 | 36 | joystick = obniz.wired("JoyStick", {"gnd": 0, "sw": 1, "y": 2, "x": 3, "vcc": 4}) 37 | 38 | # and mores 39 | ``` 40 | 41 | ## onchangex = callback(angle) 42 | ## onchangey = callback(angle) 43 | callback for on change angle. 44 | 45 | angle = -1 to 1 46 | 47 | ```Python 48 | # Python Example 49 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 50 | def onchangex(val): 51 | print(val) 52 | joystick.onchangex = onchangex 53 | def onchangey(val): 54 | print(val) 55 | joystick.onchangey = onchangey 56 | ``` 57 | 58 | ## onchangesw = function(pressed) 59 | 60 | It's called when button pressed/released. 61 | 62 | ```Python 63 | # Python Example 64 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 65 | def onchangesw(pressed): 66 | print(pressed) 67 | joystick.onchangesw = onchangesw 68 | ``` 69 | 70 | 71 | 72 | ## [await] is_pressed_wait() 73 | 74 | Get button state once. 75 | 76 | ```Python 77 | # Python Example 78 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 79 | is_pressed = await joystick.is_pressed_wait() 80 | if is_pressed: 81 | print("PRESSED") 82 | 83 | ``` 84 | 85 | 86 | ## [await] get_x_wait() 87 | ## [await] get_y_wait() 88 | 89 | Get X or Y angle once 90 | 91 | ```Python 92 | # Python Example 93 | joystick = obniz.wired("JoyStick", {"gnd": 4, "sw": 0, "y": 1, "x" 2, "vcc": 3}) 94 | x = await joystick.get_x_wait() 95 | y = await joystick.get_y_wait() 96 | 97 | print("x:", x, "y:", y) 98 | 99 | ``` -------------------------------------------------------------------------------- /tests/libs/measurements/test_measure.py: -------------------------------------------------------------------------------- 1 | from ...utils import assert_finished, assert_send, receive_json 2 | 3 | 4 | class TestObnizMeasure: 5 | def test_echo(self, obniz): 6 | obniz.measure.echo( 7 | { 8 | "io_pulse": 1, # io for generate pulse 9 | "io_echo": 2, # io to be measured 10 | "pulse": "positive", # generate pulse pattern 11 | "pulse_width": 0.1, # generate pulse width 12 | "measure_edges": 3, # 1 to 4. maximum edges to measure 13 | "timeout": 1000, # self is optional. 1000(1sec) is default 14 | } 15 | ) 16 | 17 | assert_send( 18 | obniz, 19 | [ 20 | { 21 | "measure": { 22 | "echo": { 23 | "io_pulse": 1, 24 | "io_echo": 2, 25 | "pulse": "positive", 26 | "pulse_width": 0.1, 27 | "measure_edges": 3, 28 | "timeout": 1000, 29 | } 30 | } 31 | } 32 | ], 33 | ) 34 | assert_finished(obniz) 35 | 36 | def test_echo_response(self, mocker, obniz): 37 | stub = mocker.stub() 38 | obniz.measure.echo( 39 | { 40 | "io_pulse": 1, # io for generate pulse 41 | "io_echo": 2, # io to be measured 42 | "pulse": "positive", # generate pulse pattern 43 | "pulse_width": 0.1, # generate pulse width 44 | "measure_edges": 3, # 1 to 4. maximum edges to measure 45 | "timeout": 1000, # self is optional. 1000(1sec) is default 46 | "callback": stub, 47 | } 48 | ) 49 | 50 | assert_send( 51 | obniz, 52 | [ 53 | { 54 | "measure": { 55 | "echo": { 56 | "io_pulse": 1, 57 | "io_echo": 2, 58 | "pulse": "positive", 59 | "pulse_width": 0.1, 60 | "measure_edges": 3, 61 | "timeout": 1000, 62 | } 63 | } 64 | } 65 | ], 66 | ) 67 | assert_finished(obniz) 68 | 69 | receive_json(obniz, [{"measure": {"echo": [{"edge": True, "timing": 500}]}}]) 70 | 71 | assert len(stub.call_args[0]) == 1 72 | assert stub.call_args[0][0] == [{"edge": True, "timing": 500}] 73 | -------------------------------------------------------------------------------- /obniz/obniz/libs/embeds/ble/ble_descriptor.py: -------------------------------------------------------------------------------- 1 | from .ble_attribute_abstract import BleAttributeAbstract 2 | from .ble_helper import BleHelper 3 | 4 | 5 | class BleDescriptor(BleAttributeAbstract): 6 | def __init__(self, obj): 7 | super().__init__(obj) 8 | 9 | self.permissions = obj.get("permissions", []) 10 | if type(self.permissions) is not list: 11 | self.permissions = [self.permissions] 12 | 13 | @property 14 | def parent_name(self): 15 | return "characteristic" 16 | 17 | # addPermission(param) { 18 | # if (!self.permissions.includes(param)) { 19 | # self.permissions.push(param) 20 | # } 21 | # } 22 | 23 | # removePermission(param) { 24 | # self.permissions = self.permissions.filter(elm => { 25 | # return elm !== param 26 | # }) 27 | # } 28 | 29 | # toJSON() { 30 | # let obj = super.toJSON() 31 | 32 | # if (self.permissions.length > 0) { 33 | # obj.permissions = self.permissions 34 | # } 35 | # return obj 36 | # } 37 | 38 | def write(self, data_array, need_response=False): 39 | self.get_characteristic().get_service().peripheral.obniz.send( 40 | { 41 | "ble": { 42 | "peripheral": { 43 | "write_descriptor": { 44 | "service_uuid": BleHelper.uuid_filter( 45 | self.get_characteristic().get_service().uuid 46 | ), 47 | "characteristic_uuid": BleHelper.uuid_filter( 48 | self.get_characteristic().uuid 49 | ), 50 | "descriptor_uuid": self.uuid, 51 | "data": data_array, 52 | } 53 | } 54 | } 55 | } 56 | ) 57 | 58 | def read(self): 59 | self.get_characteristic().get_service().peripheral.obniz.send( 60 | { 61 | "ble": { 62 | "peripheral": { 63 | "read_descriptor": { 64 | "service_uuid": BleHelper.uuid_filter( 65 | self.get_characteristic().get_service().uuid 66 | ), 67 | "characteristic_uuid": BleHelper.uuid_filter( 68 | self.get_characteristic().uuid 69 | ), 70 | "descriptor_uuid": self.uuid, 71 | } 72 | } 73 | } 74 | } 75 | ) 76 | -------------------------------------------------------------------------------- /obniz/obniz/libs/io_peripherals/io_.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import datetime 3 | 4 | 5 | class PeripheralIO_: # noqa: N801 6 | def __init__(self, obniz): 7 | self.obniz = obniz 8 | self.observers = [] 9 | self._reset() 10 | 11 | def _reset(self): 12 | for observer in self.observers: 13 | observer.cancel() 14 | self.observers.clear() 15 | self._animation_identifier = 0 16 | 17 | def add_observer(self, name, future): 18 | if asyncio.isfuture(future): 19 | self.observers.append({ 20 | "future": future, 21 | "name": name 22 | }) 23 | 24 | def animation(self, name, status, array=None, repeat=None): 25 | obj = {} 26 | obj["io"] = {"animation": {"name": name, "status": status}} 27 | if type(repeat) is int: 28 | obj["io"]["animation"]["repeat"] = repeat 29 | if array is None: 30 | array = [] 31 | 32 | states = [] 33 | for i in range(0, len(array)): 34 | state = array[i] 35 | duration = state["duration"] 36 | operation = state["state"] 37 | 38 | # dry run. and get json commands 39 | self.obniz.send_pool = [] 40 | operation(i) 41 | pooled_json_array = self.obniz.send_pool 42 | self.obniz.send_pool = None 43 | states.append({"duration": duration, "state": pooled_json_array}) 44 | 45 | if status == "loop": 46 | obj["io"]["animation"]["states"] = states 47 | 48 | self.obniz.send(obj) 49 | 50 | def repeat_wait(self, array, repeat): 51 | if (type(repeat) not in [int, float] or repeat < 1): 52 | raise Exception("please specify repeat count > 0") 53 | if (type(repeat) is not int): 54 | raise Exception("please provide integer number like 1, 2, 3,,,") 55 | 56 | # get_running_loop() function is preferred on Python >= 3.7 57 | future = asyncio.get_event_loop().create_future() 58 | name = "_repeatwait" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") 59 | if self._animation_identifier + 1 > 1000: 60 | self._animation_identifier = 0 61 | 62 | self.animation(name, "loop", array, repeat) 63 | self.add_observer(name, future) 64 | return future 65 | 66 | def notified(self, obj): 67 | if obj["animation"]["status"] == "finish": 68 | matched_list = [observer for observer in self.observers if obj["animation"]["name"] == observer["name"]] 69 | for matched in matched_list: 70 | matched["future"].set_result(None) 71 | self.observers.remove(matched) 72 | -------------------------------------------------------------------------------- /obniz/parts/Moving/ServoMotor/README-ja.md: -------------------------------------------------------------------------------- 1 | # ServoMotor 2 | RCサーボモーターはコンピューターの入っているギヤードモーターです。 3 | 角度を維持することができます。 4 | ただ角度をモーターに指示するだけで良いのです。 5 | 6 | ![](./servomotor.gif) 7 | 8 | ### 電源もobnizに繋げられるモーターについて 9 | 10 | RCサーボの多くは電源も含めそのままobnizに接続できますが、いくつかの(特に小型の)サーボモーターでは電源の電流がリークしやすくobnizの過電流検知により電源を供給できない場合があります。 11 | 12 | その場合は以下のような対策が必要となります。 13 | 14 | - (推奨)電源のみ外部から供給する(obnizのJ1ピンはUSB直結なのでそこから供給することもできます) 15 | - ブレッドボードを介してobnizと接続する(ブレッドボードは抵抗が大きく、過電流検知を回避できることがあります。) 16 | 17 | obnizのioから直接電源供給を確認したサーボモーター 18 | 19 | メーカー | 型番 20 | --- | --- 21 | Tower Pro | SG-5010 22 | Tower Pro | MG92B 23 | Tower Pro | MG90S 24 | Tower Pro | MG90D 25 | Tower Pro | SG90 26 | Tower Pro | SG92R 27 | GWS | S35STD 28 | 29 | obnizのioからでは直接電源供給できないサーボモーター 30 | 31 | メーカー | 型番 32 | --- | --- 33 | Quimat | QKY66-5 34 | FEETECH | FS90R 35 | 36 | ## obniz.wired("ServoMotor", {[vcc, gnd, signal, pwm]}) 37 | 3本の足をObnizにつなぎます。それぞれプラス、信号、マイナスとなっていて、製造メーカーなどにより配置が違います。 38 | 39 | この例はもっともよく使われている配線パターンです。 40 | obnizのセットに入っているサーボモーターもこのパターンです。 41 | 42 | ![](./servocable.jpg) 43 | 44 | 信号(signal)、プラス(vcc)、マイナス(gnd)をそれぞれ obnizの0, 1, 2につないだ場合は以下のようにします。 45 | 46 | ![](./wired.png) 47 | ```Python 48 | # Python Example 49 | servo = obniz.wired("ServoMotor", {"signal": 0, "vcc": 1, "gnd": 2}) 50 | servo.angle(90.0) # half position 51 | ``` 52 | 53 | vccとgndを他の方法で接続している場合はsignalのみの指定でOKです 54 | ```Python 55 | servo = obniz.wired("ServoMotor", {"signal": 2}) 56 | ``` 57 | 58 | また、生成済みのpwmオブジェクトを利用することも出来ます 59 | ```Python 60 | pwm = obniz.get_free_pwm() 61 | servo = obniz.wired("ServoMotor", {"pwm": pwm}) 62 | ``` 63 | 64 | ## angle(degree) 65 | 角度を0~180により指定します。 66 | 67 | ```Python 68 | # Python Example 69 | servo = obniz.wired("ServoMotor", {"signal": 0, "vcc": 1, "gnd": 2}) 70 | 71 | servo.angle(90.0) # half position 72 | ``` 73 | 74 | ## range = {min, max} 75 | 76 | 出力するパルス幅を調整できます。 77 | 0度~180度に応じて0.5~2.4msecのパルスが出力されますが、それを自分で調整したい場合に利用します。 78 | 79 | ```Python 80 | # Python Example 81 | servo = obniz.wired("ServoMotor", {"signal": 0, "vcc": 1, "gnd": 2}) 82 | servo.range = { 83 | "min": 0.8, 84 | "max": 2.4 85 | } 86 | servo.angle(90.0) # half position 87 | ``` 88 | 89 | ## on() 90 | サーボモーターの電源を入れます。wiredを呼んだ段階で電源は入っています。offにした後に再度onにしたい時に呼んでください 91 | 92 | ```Python 93 | # Python Example 94 | servo = obniz.wired("ServoMotor", {"signal": 0, "vcc": 1, "gnd": 2}) 95 | 96 | servo.angle(90.0) # half position 97 | servo.off() 98 | servo.on() 99 | ``` 100 | ## off() 101 | サーボモーターの電源を切ります。信号の供給も停止します。保持力がなくなりますから、モーターに負荷がかかっている場合はoffにすることで勝手に回転します。 102 | 103 | ```Python 104 | # Python Example 105 | servo = obniz.wired("ServoMotor", {"signal": 0, "vcc": 1, "gnd": 2}) 106 | 107 | servo.angle(90.0) # half position 108 | servo.off() 109 | servo.on() 110 | ``` -------------------------------------------------------------------------------- /obniz/parts/TemperatureSensor/i2c/SHT31/__init__.py: -------------------------------------------------------------------------------- 1 | from attrdict import AttrDefault 2 | 3 | import asyncio 4 | 5 | class SHT31: 6 | def __init__(self): 7 | self.required_keys = ['adr', 'addressmode'] 8 | self.keys = ['vcc', 'sda', 'scl', 'gnd', 'adr', 'addressmode', 'i2c', 'pull'] 9 | self.io_keys = ['vcc', 'sda', 'scl', 'gnd', 'adr'] 10 | self.commands = AttrDefault(bool, {}) 11 | self.commands.soft_reset = [0x30, 0xa2] 12 | self.commands.high_repeat_streach = [0x2c, 0x06] 13 | self.commands.middle_repeat_streach = [0x2c, 0x0d] 14 | self.commands.low_repeat_streach = [0x2c, 0x10] 15 | self.commands.high_repeat = [0x24, 0x00] 16 | self.commands.medium_repeat = [0x24, 0x0b] 17 | self.commands.low_repeat = [0x24, 0x16] 18 | self.wait_time = AttrDefault(bool, {}) 19 | self.wait_time.wakeup = 1 20 | self.wait_time.soft_reset = 1 21 | self.wait_time.low_repeat = 4 22 | self.wait_time.medium_repeat = 6 23 | self.wait_time.high_repeat = 15 24 | self.commands.read_status = [0xf3, 0x2d] 25 | 26 | @staticmethod 27 | def info(): 28 | return AttrDefault(bool, {'name': 'SHT31'}) 29 | 30 | def wired(self, obniz): 31 | self.obniz = obniz 32 | self.obniz.set_vcc_gnd(*[self.params.vcc, self.params.gnd, '5v']) 33 | self.io_adr = obniz.get_io(*[self.params.adr]) 34 | if self.params.addressmode == 4: 35 | self.io_adr.output(*[False]) 36 | self.address = 0x44 37 | elif self.params.addressmode == 5: 38 | self.io_adr.pull(*[None]) 39 | self.address = 0x45 40 | self.params.clock = self.params.clock or 100 * 1000 41 | self.params.mode = self.params.mode or 'master' 42 | self.params.pull = self.params.pull or '5v' 43 | self.i2c = obniz.get_i2c_with_config(*[self.params]) 44 | obniz.i2c0.write(*[self.address, self.commands.soft_reset]) 45 | 46 | async def get_data(self): 47 | self.i2c.write(*[self.address, self.commands.high_repeat]) 48 | await self.obniz.wait(*[self.wait_time.high_repeat]) 49 | return await self.i2c.read_wait(*[self.address, 6]) 50 | 51 | async def get_temp_wait(self): 52 | obj = await self.get_all_wait() 53 | return obj.temperature 54 | 55 | async def get_humd_wait(self): 56 | obj = await self.get_all_wait() 57 | return obj.humidity 58 | 59 | async def get_all_wait(self): 60 | ret = await self.get_data() 61 | temp_bin = (ret[0] * 256 + ret[1]) 62 | temperature = (-45 + 175 * temp_bin / (65536 - 1)) 63 | humd_bin = (ret[3] * 256 + ret[4]) 64 | humidity = 100 * humd_bin / (65536 - 1) 65 | return AttrDefault(bool, {'temperature': temperature, 'humidity': humidity}) --------------------------------------------------------------------------------