2014-03-15 19:18:31 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of sensors2osc package
|
|
|
|
#
|
|
|
|
# sensors2osc is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# sensors2osc 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 General Public License
|
|
|
|
# along with sensors2osc. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Stefan Kögl
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import atexit
|
|
|
|
import os.path
|
|
|
|
import serial
|
|
|
|
import socket
|
2014-03-30 16:34:26 +00:00
|
|
|
import time
|
2014-04-28 05:20:28 +00:00
|
|
|
import sys
|
2014-03-15 19:18:31 +00:00
|
|
|
|
2014-04-03 15:44:36 +00:00
|
|
|
from chaosc.argparser_groups import ArgParser
|
2014-05-25 13:52:40 +00:00
|
|
|
from chaosc.lib import logger
|
2014-03-25 21:30:30 +00:00
|
|
|
|
2014-03-15 19:18:31 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from chaosc.c_osc_lib import OSCMessage
|
|
|
|
except ImportError as e:
|
|
|
|
print(e)
|
|
|
|
from chaosc.osc_lib import OSCMessage
|
|
|
|
|
2014-03-16 10:20:36 +00:00
|
|
|
|
2014-03-16 09:43:56 +00:00
|
|
|
class Platform(object):
|
|
|
|
def __init__(self, args):
|
|
|
|
self.args = args
|
2014-04-28 05:20:28 +00:00
|
|
|
self.remote = (self.args.chaosc_host, self.args.chaosc_port)
|
2014-03-16 09:43:56 +00:00
|
|
|
self.serial_sock = None
|
2014-03-25 21:30:30 +00:00
|
|
|
self.osc_sock = socket.socket(args.address_family, 2, 17)
|
2014-03-16 09:43:56 +00:00
|
|
|
self.osc_sock.connect((self.args.chaosc_host, self.args.chaosc_port))
|
2014-03-15 19:18:31 +00:00
|
|
|
|
|
|
|
|
2014-03-16 09:43:56 +00:00
|
|
|
def connect(self):
|
2014-05-25 13:52:40 +00:00
|
|
|
logger.info("connect serial")
|
|
|
|
logger.info("waiting for the device %r to come up", self.args.device)
|
2014-03-30 16:34:26 +00:00
|
|
|
self.serial_sock = serial.Serial()
|
|
|
|
self.serial_sock.port = self.args.device
|
|
|
|
self.serial_sock.baudrate = 115200
|
|
|
|
self.serial_sock.timeout = 1
|
2014-03-23 11:37:31 +00:00
|
|
|
while 1:
|
|
|
|
try:
|
2014-03-30 16:34:26 +00:00
|
|
|
self.serial_sock.open()
|
|
|
|
except (serial.serialutil.SerialException, os.error), e:
|
2014-05-25 13:52:40 +00:00
|
|
|
logger.exception(e)
|
2014-03-30 16:34:26 +00:00
|
|
|
time.sleep(0.5)
|
2014-03-23 11:37:31 +00:00
|
|
|
pass
|
2014-03-26 13:34:05 +00:00
|
|
|
else:
|
|
|
|
break
|
2014-03-15 19:18:31 +00:00
|
|
|
|
|
|
|
|
2014-03-16 09:43:56 +00:00
|
|
|
def close(self):
|
|
|
|
if self.serial_sock is not None:
|
2014-05-25 13:52:40 +00:00
|
|
|
logger.info("close serial")
|
2014-03-16 09:43:56 +00:00
|
|
|
self.serial_sock.close()
|
2014-03-15 19:18:31 +00:00
|
|
|
|
2014-03-16 09:43:56 +00:00
|
|
|
|
|
|
|
def reconnect(self):
|
2014-05-25 13:52:40 +00:00
|
|
|
logger.info("reconnect serial")
|
2014-03-16 09:43:56 +00:00
|
|
|
self.close()
|
|
|
|
self.connect()
|
2014-03-15 19:18:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_args(name):
|
2014-04-03 15:44:36 +00:00
|
|
|
arg_parser = ArgParser(name)
|
|
|
|
arg_parser.add_global_group()
|
|
|
|
arg_parser.add_chaosc_group()
|
2014-12-04 15:38:55 +00:00
|
|
|
client_group = arg_parser.add_client_group()
|
|
|
|
arg_parser.add_argument(client_group, "-D", '--device',
|
|
|
|
help='device node under /dev')
|
|
|
|
arg_parser.add_argument(client_group, "-a", '--actor',
|
|
|
|
help='actor name')
|
|
|
|
arg_parser.add_argument(client_group, '-b', '--baudrate', type=int, default=115200, choices=sorted(serial.baudrate_constants.keys()),
|
|
|
|
help='selects the baudrate, default=115200, for valid values execute "import serial;print sorted(serial.baudrate_constants.keys())"')
|
2014-03-15 19:18:31 +00:00
|
|
|
|
2014-04-03 15:44:36 +00:00
|
|
|
args = arg_parser.finalize()
|
2014-03-15 20:39:12 +00:00
|
|
|
return args
|
2014-03-15 19:18:31 +00:00
|
|
|
|
2014-03-16 09:43:56 +00:00
|
|
|
|
2014-12-04 15:38:55 +00:00
|
|
|
def init(prog_name, port=None):
|
|
|
|
args = create_args(prog_name)
|
|
|
|
if port:
|
|
|
|
args.chaosc_host = "lucas"
|
|
|
|
args.chaosc_port = 6002
|
2014-03-16 09:43:56 +00:00
|
|
|
platform = Platform(args)
|
|
|
|
platform.connect()
|
2014-03-16 10:20:36 +00:00
|
|
|
atexit.register(platform.close)
|
2014-03-15 19:18:31 +00:00
|
|
|
|
2014-03-16 09:43:56 +00:00
|
|
|
return platform
|