示例#1
0
    def __init__(self, queue):
        self._connection_lock = threading.Lock()
        self._reader_thread = None
        self._queue = queue
        #self._mpqueue = Queue()
        self._recording_lock = threading.Lock()
        self._recording = False
        self._record_file = None
        self._bytestream = ''
        # self._data_type = data_type
        self._mode = 'c'  #mode

        self.raw_text = 'streamed data'
        # Get the BLE provider for the current  platform.
        self.ble = Adafruit_BluefruitLE.get_provider()
        # add these into the main scope of Serial handler instead of the BLE Class handler.
        self.ble_line = ''
        self.get_line_lock = 0
        self.device = ''
        self.stoprequest = threading.Event()
示例#2
0
文件: app.py 项目: xinterface/OpenEIT
# Distributed under the (new) CC BY-NC-SA 4.0 License. See LICENSE.txt for more info.

"""

from __future__ import absolute_import
import argparse
import logging

from sys import platform

# NOTE: darwin_xx should be just darwin. I've done this as a hack to stop the currently out of date adafruit ble library. More work needs to be done on bluetooth to update. Would appreciate a contribution. Thanks.
if platform == "darwin_xx":
    # OS X
    from OpenEIT.backend.bluetooth import Adafruit_BluefruitLE
    # Get the BLE provider for the current platform.
    ble = Adafruit_BluefruitLE.get_provider()
#import .dashboard
from OpenEIT.dashboard import runGui
from OpenEIT.dashboard import Controller
import serial
import serial.tools.list_ports
#print (Adafruit_BluefruitLE.__file__)

FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logger = logging.getLogger(__name__)


# TODO: Improve State Feedback
# The current connection and playback state should be clearly visible
# at all times
示例#3
0
                def run(self):

                    while not self.stoprequest.isSet():

                        self.ble = Adafruit_BluefruitLE.get_provider()
                        self.ble.initialize()

                        # Clear any cached data because both bluez and CoreBluetooth have issues with
                        # caching data and it going stale.
                        self.ble.clear_cached_data()

                        adapter = self.ble.get_default_adapter()

                        adapter.power_on()

                        print('Using adapter: {0}'.format(adapter.name))
                        # Disconnect any currently connected UART devices.  Good for cleaning up and
                        # starting from a fresh state.
                        print('Disconnecting any connected UART devices...')
                        UART.disconnect_devices()

                        # Scan for UART devices.
                        print('Searching for UART device...')
                        try:
                            adapter.start_scan()
                            # Search for the first UART device found (will time out after 60 seconds
                            # but you can specify an optional timeout_sec parameter to change it).
                            self.device = UART.find_device()
                            if self.device is None:
                                raise RuntimeError(
                                    'Failed to find UART device!')
                        finally:
                            # Make sure scanning is stopped before exiting.
                            adapter.stop_scan()

                        print('Connecting to ', self.device.name)
                        self.device.connect(
                        )  # Will time out after 60 seconds, specify timeout_sec parameter
                        # to change the timeout.

                        # Once connected do everything else in a try/finally to make sure the device
                        # is disconnected when done.
                        try:
                            # Wait for service discovery to complete for the UART service.  Will
                            # time out after 60 seconds (specify timeout_sec parameter to override).
                            print('Discovering services...')
                            UART.discover(self.device)

                            # Once service discovery is complete create an instance of the service
                            # and start interacting with it.
                            self.uart = UART(self.device)
                            serialhandler._connected = True
                            logger.info('connection made now')
                            charline = ''

                            def handle_line(data):
                                #print ('handling the line')
                                # print (data)
                                serialhandler.raw_text = data
                                with serialhandler._recording_lock:
                                    if serialhandler._recording:
                                        # logger.info("serialhandler._recording")
                                        #serialhandler._record_file.write(data + "\n")
                                        serialhandler._bytestream = serialhandler._bytestream + data

                                res = parse_any_line(data, serialhandler._mode)

                                if res is not None:
                                    self._queue.put(res)

                            while self.device.is_connected:
                                if self.uart is not None:
                                    newdata = self.uart.read(timeout_sec=1)
                                    if newdata is None:
                                        print('Received None: {0}'.format(
                                            newdata))
                                    else:
                                        characters = newdata.decode()
                                        if "\r" in characters:
                                            charline = charline + characters
                                            handle_line(charline)
                                            charline = ''
                                        else:
                                            charline = charline + characters
                        finally:
                            logger.info('device disconnecting')
                            try:
                                serialhandler._connected = False
                                print('changed state')
                                self.stoprequest.set()
                                self.device.disconnect()
                                print('disconnected device')
                            except:
                                print('disconnecting problem')