def should_have_printed_usage_instructions(monkeypatch) -> None: print_coloured_calls = [] mock_parse_argv(MODULE_NAME, 'Bluetooth', monkeypatch) monkeypatch.setattr(f"{MODULE_NAME}.print_coloured", lambda *a, **k: print_coloured_calls.append('')) Bluetooth().usage() assert len(print_coloured_calls) == 2
def execute(self) -> str: log('Hello!') automations = [ Bluetooth(['on']), Wifi(['on']), ] for automation in automations: automation.execute() return ''
def should_have_executed(monkeypatch, argv: List[str], on: bool) -> None: def mock_execute_cmd(*args: tuple, **kwargs: dict) -> str: assert args[0] == ['blueutil', '-p', ('1' if on else '0')] return '\n' mock_parse_argv(MODULE_NAME, 'Bluetooth', monkeypatch, argv) monkeypatch.setattr(f"{MODULE_NAME}.execute_cmd", mock_execute_cmd) mute_logs(MODULE_NAME, monkeypatch) assert Bluetooth().execute() == ''
def __init__(self): self.monitor = CE_Monitor(self) self.player = xbmc.Player() self.actions = Actions() self.blue = Bluetooth() self.lightIsON=False self.deviceFound=False self.discoveryIsOn=False self.delayToggle = 0
def should_not_have_parsed_argv_with_wrong_or_no_option( monkeypatch, argv: List[str]) -> None: def mock_raise_error(*args: tuple, **kwargs: dict) -> None: assert kwargs['usage'] is not None raise SystemExit(0) # Controlled early exit monkeypatch.setattr(f"{MODULE_NAME}.raise_error", mock_raise_error) with pytest.raises(SystemExit) as e: Bluetooth(argv) assert e.type == SystemExit assert e.value.code == 0
def execute(self) -> str: log('Good night!') automations = [ Volume(['0.0']), Bluetooth(['off']), Wifi(['off']), Sleep(), ] for automation in automations: automation.execute() return ''
# LAB3 # Jimmy Hoang # William Gentry # Daniel Burr import sensor, image, math, time, pyb, motors, bluetooth from pyb import Pin, Timer, UART, LED, ADC from math import sqrt from motors import DCMotor, ServoMotor from bluetooth import Bluetooth ##### Global Variables and Initialization ##### ## uart uart = UART(1, 115200) bt = Bluetooth(uart) ## DC motor control dc_motor = DCMotor(tim_num=2, channel=4, frequency=100, pin="P5") dc_motor.set_control(in_a="P2", in_b="P3", en_a="P4", en_b="P8") dc_motor.set_current_sense(cs="P6", cs_dis="P9") dutycyclePW = 0 ## Servo motor control servo_max = 0.0019 servo_min = 0.0010 servo_center = (servo_max + servo_min) / 2 servo_offset = servo_max - servo_center servo_motor = ServoMotor(tim_num=4, channel=1, frequency=300, pin="P7") servo_motor.set_range(max_pw=servo_max, min_pw=servo_min) sec = servo_center
def should_have_parsed_argv(argv: List[str]) -> None: assert Bluetooth(argv).argv == argv
from tableur import ReadSpreadsheet from bluetooth import Bluetooth import time from pprint import pprint if __name__ == '__main__': stop = ["stop", "stop"] spreadsheet = ReadSpreadsheet("tableau.csv") bluetooth = Bluetooth("COM9", 9600) #bluetooth.send_data("start") print("Début de l'envoie") print("bon nom") print("Bonnes syllabes 2") for syllable, index in zip(spreadsheet.good_syllable, spreadsheet.index_good_syllable): a = 1 data_received = "" while a == 1: data_sent = syllable + "-" + str(index) bluetooth.send_data(data_sent) data_received = bluetooth.receive_data().strip() print("La donnée reçue est : " + data_received) print("La donnée envoyée est : " + syllable + "-" + str(index)) if data_received == data_sent: a = 0 else: a = 1
# from subprocess import call from time import sleep import Adafruit_MCP3008 try: from gpiozero import Button runningOnRaspi = True except: runningOnRaspi = False VOLUME_CHANNEL = 0 mcp = Adafruit_MCP3008.MCP3008(clk=13, cs=26, miso=25, mosi=9) mode = None radio = Radio() bluetooth = Bluetooth() def switchToBluetooth(): global mode if mode == "bluetooth": return mode = "bluetooth" print("Load Bluetooth") nextButton.when_pressed = None prevButton.when_pressed = None radio.stop() bluetooth.start()
class Service: def __init__(self): self.monitor = CE_Monitor(self) self.player = xbmc.Player() self.actions = Actions() self.blue = Bluetooth() self.lightIsON=False self.deviceFound=False self.discoveryIsOn=False self.delayToggle = 0 def run(self): while not self.monitor.abortRequested(): waitForTime = 5 timeIntervalHit = True if settings.general["use_time_interval"]: startTime = settings.general["start_time"] endTime = settings.general["end_time"] currentTime = time.localtime() if currentTime >= startTime and currentTime <= endTime: timeIntervalHit = True xbmc.log("ChyFy::service.py - Time Interval Hit", level=xbmc.LOGDEBUG) else: timeIntervalHit = False xbmc.log("ChyFy::service.py - Current Time not in Time Interval", level=xbmc.LOGDEBUG) if settings.general["service_enabled"] and timeIntervalHit: self.startDiscovery() waitForTime = settings.general["waiting_time"] + self.delayToggle self.delayToggle = 0 else: self.stopDiscovery() #SleepTime in Minutes waitForTime = settings.general["sleep_time"] # Sleep/wait for abort for $waitForTime seconds xbmc.log("ChyFy::service.py - Start waiting for %d seconds" % waitForTime, level=xbmc.LOGDEBUG) if self.monitor.waitForAbort(waitForTime): # Abort was requested while waiting. We should exit break xbmc.log("ChyFy::service.py - Run after wait", level=xbmc.LOGDEBUG) if self.discoveryIsOn: self.scan() xbmc.log("ChyFy::service.py - discovery is on", level=xbmc.LOGDEBUG) else: xbmc.log("ChyFy::service.py - discovery is off", level=xbmc.LOGDEBUG) def scan(self): dbusDevices = self.blue.get_devices() #Check if not None if dbusDevices is not None: deviceFound=False rssiSensity = settings.general["rssi"] #Get Devices device_names = [] device_names.append(settings.bt_devices["bt_1_name"]) device_numbers = settings.bt_devices["bt_numbers"] if device_numbers > 1: for i in range(2,device_numbers): device_names.append(settings.bt_devices["bt_"+i+"_name"]) for key in dbusDevices: if 'Name' in dbusDevices[key]: devName = dbusDevices[key]['Name'] devAddress = dbusDevices[key]['Address'] devRSSI = int(dbusDevices[key]['RSSI']) xbmc.log("ChyFy::service.py - Device: %s -> %s (%s >= %s)" % (key, devName, devRSSI, rssiSensity), level=xbmc.LOGDEBUG) if (devName in device_names or devAddress in device_names) and devRSSI >= rssiSensity: xbmc.log("ChyFy::service.py - Device was found: %s" % devName, level=xbmc.LOGDEBUG) deviceFound=True break if deviceFound: self.handle_device_found() else: self.handle_device_notfound() else: xbmc.log("ChyFy::service.py - dbusDevices is None", level=xbmc.LOGERROR) def handle_device_found(self): if not self.lightIsON: for i in range(1,4): if settings.wireless_switch["ws_"+str(i)+"_auto_on"]: self.actions.ws_control(str(i), "1") if settings.media_control["mc_auto_play"] and not self.player.isPlaying(): self.player.pause() self.delayToggle = 30 self.lightIsON=True def handle_device_notfound(self): if self.lightIsON: for i in range(1,4): if settings.wireless_switch["ws_"+str(i)+"_auto_off"]: self.actions.ws_control(str(i), "0") if settings.media_control["mc_auto_pause"] and self.player.isPlaying(): self.player.pause() self.lightIsON=False def startDiscovery(self): if not self.discoveryIsOn: self.blue.start_service() self.blue.start_discovery() self.discoveryIsOn = True def stopDiscovery(self): if self.discoveryIsOn: self.blue.stop_discovery() self.blue.stop_service() self.discoveryIsOn = False