diff --git a/examples/sonar_disable.py b/examples/sonar_disable.py new file mode 100644 index 0000000..55a3fff --- /dev/null +++ b/examples/sonar_disable.py @@ -0,0 +1,85 @@ +""" + Copyright (c) 2020 Alan Yorinks All rights reserved. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + Version 3 as published by the Free Software Foundation; either + or (at your option) any later version. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +""" + +import asyncio +import sys +import time +from telemetrix_aio import telemetrix_aio + +""" +This program continuously monitors an HC-SR04 Ultrasonic Sensor +It reports changes to the distance sensed. +""" +TRIGGER_PIN = 9 +ECHO_PIN = 10 + +# indices into callback data +REPORT_TYPE = 0 +TRIG_PIN = 1 +DISTANCE = 2 +TIME = 3 + + +# A callback function to display the distance +async def the_callback(data): + """ + The callback function to display the change in distance + :param data: [report_type = PrivateConstants.SONAR_DISTANCE, trigger pin number, distance, timestamp] + """ + date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[3])) + print(f'Sonar Report: Trigger Pin: {data[1]} Distance: {data[2]} Time: {date}') + + +async def sonar(my_board, trigger_pin, echo_pin, callback): + """ + Set the pin mode for a sonar device. Results will appear via the + callback. + + :param my_board: a telemetrix_aio instance + :param trigger_pin: Arduino pin number + :param echo_pin: Arduino pin number + :param callback: The callback function + """ + + # set the pin mode for the trigger and echo pins + await my_board.set_pin_mode_sonar(trigger_pin, echo_pin, callback) + # wait forever + while True: + try: + await asyncio.sleep(5) + await my_board.sonar_disable() + await asyncio.sleep(5) + await my_board.sonar_enable() + + except KeyboardInterrupt: + await my_board.shutdown() + sys.exit(0) + + +# get the event loop +loop = asyncio.new_event_loop() +asyncio.set_event_loop(loop) + +# instantiate telemetrix_aio +board = telemetrix_aio.TelemetrixAIO() + +try: + # start the main function + loop.run_until_complete(sonar(board, TRIGGER_PIN, ECHO_PIN, the_callback)) +except KeyboardInterrupt: + loop.run_until_complete(board.shutdown()) + sys.exit(0) diff --git a/examples/wifi/sonar_disable.py b/examples/wifi/sonar_disable.py new file mode 100644 index 0000000..7eecb8e --- /dev/null +++ b/examples/wifi/sonar_disable.py @@ -0,0 +1,84 @@ +""" + Copyright (c) 2020 Alan Yorinks All rights reserved. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + Version 3 as published by the Free Software Foundation; either + or (at your option) any later version. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +""" + +import asyncio +import sys +import time +from telemetrix_aio import telemetrix_aio + +""" +This program continuously monitors an HC-SR04 Ultrasonic Sensor +It reports changes to the distance sensed. +""" +TRIGGER_PIN = 4 +ECHO_PIN = 5 + +# indices into callback data +REPORT_TYPE = 0 +TRIG_PIN = 1 +DISTANCE = 2 +TIME = 3 + + +# A callback function to display the distance +async def the_callback(data): + """ + The callback function to display the change in distance + :param data: [report_type = PrivateConstants.SONAR_DISTANCE, trigger pin number, distance, timestamp] + """ + date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[3])) + print(f'Sonar Report: Trigger Pin: {data[1]} Distance: {data[2]} Time: {date}') + + +async def sonar(my_board, trigger_pin, echo_pin, callback): + """ + Set the pin mode for a sonar device. Results will appear via the + callback. + + :param my_board: telemetrix-aio instance + :param trigger_pin: Arduino pin number + :param echo_pin: Arduino pin number + :param callback: The callback function + """ + + # set the pin mode for the trigger and echo pins + await my_board.set_pin_mode_sonar(trigger_pin, echo_pin, callback) + # wait forever + while True: + try: + await asyncio.sleep(5) + await my_board.sonar_disable() + await asyncio.sleep(5) + await my_board.sonar_enable() + + except KeyboardInterrupt: + await my_board.shutdown() + sys.exit(0) + +# get the event loop +loop = asyncio.new_event_loop() +asyncio.set_event_loop(loop) + +# instantiate telemetrix_aio +board = telemetrix_aio.TelemetrixAIO(ip_address='192.168.2.220') + +try: + # start the main function + loop.run_until_complete(sonar(board, TRIGGER_PIN, ECHO_PIN, the_callback)) +except KeyboardInterrupt: + loop.run_until_complete(board.shutdown()) + sys.exit(0) diff --git a/html/telemetrix_aio/index.html b/html/telemetrix_aio/index.html index 331703b..3a7295e 100644 --- a/html/telemetrix_aio/index.html +++ b/html/telemetrix_aio/index.html @@ -1083,6 +1083,20 @@
telemetrix_aio.telemetrix_aio
+async def sonar_disable(self)
+
Disable sonar scanning for all sonar sensors
async def sonar_disable(self):
+ """
+ Disable sonar scanning for all sonar sensors
+ """
+ command = [PrivateConstants.SONAR_DISABLE]
+ await self._send_command(command)
+
+async def sonar_enable(self)
+
Enable sonar scanning for all sonar sensors
async def sonar_enable(self):
+ """
+ Enable sonar scanning for all sonar sensors
+ """
+ command = [PrivateConstants.SONAR_ENABLE]
+ await self._send_command(command)
+
async def spi_cs_control(self, chip_select_pin, select)
set_pin_mode_spi
set_pin_mode_stepper
shutdown
sonar_disable
sonar_enable
spi_cs_control
spi_read_blocking
spi_set_format