text stringlengths 1 93.6k |
|---|
import struct
|
import time
|
import sys
|
import os
|
try:
|
import machine
|
gettime = lambda: time.ticks_ms()
|
SOCK_TIMEOUT = 0
|
except ImportError:
|
const = lambda x: x
|
gettime = lambda: int(time.time() * 1000)
|
SOCK_TIMEOUT = 0.05
|
def dummy(*args):
|
pass
|
MSG_RSP = const(0)
|
MSG_LOGIN = const(2)
|
MSG_PING = const(6)
|
MSG_TWEET = const(12)
|
MSG_NOTIFY = const(14)
|
MSG_BRIDGE = const(15)
|
MSG_HW_SYNC = const(16)
|
MSG_INTERNAL = const(17)
|
MSG_PROPERTY = const(19)
|
MSG_HW = const(20)
|
MSG_HW_LOGIN = const(29)
|
MSG_EVENT_LOG = const(64)
|
MSG_REDIRECT = const(41) # TODO: not implemented
|
MSG_DBG_PRINT = const(55) # TODO: not implemented
|
STA_SUCCESS = const(200)
|
STA_INVALID_TOKEN = const(9)
|
DISCONNECTED = const(0)
|
CONNECTING = const(1)
|
CONNECTED = const(2)
|
print("""
|
___ __ __
|
/ _ )/ /_ _____ / /__
|
/ _ / / // / _ \\/ '_/
|
/____/_/\\_, /_//_/_/\\_\\
|
/___/ for Python v""" + __version__ + " (" + sys.platform + ")\n")
|
class EventEmitter:
|
def __init__(self):
|
self._cbks = {}
|
def on(self, evt, f=None):
|
if f:
|
self._cbks[evt] = f
|
else:
|
def D(f):
|
self._cbks[evt] = f
|
return f
|
return D
|
def emit(self, evt, *a, **kv):
|
if evt in self._cbks:
|
self._cbks[evt](*a, **kv)
|
class BlynkProtocol(EventEmitter):
|
def __init__(self, auth, tmpl_id=None, fw_ver=None, heartbeat=50, buffin=1024, log=None):
|
EventEmitter.__init__(self)
|
self.heartbeat = heartbeat*1000
|
self.buffin = buffin
|
self.log = log or dummy
|
self.auth = auth
|
self.tmpl_id = tmpl_id
|
self.fw_ver = fw_ver
|
self.state = DISCONNECTED
|
self.connect()
|
def virtual_write(self, pin, *val):
|
self._send(MSG_HW, 'vw', pin, *val)
|
def send_internal(self, pin, *val):
|
self._send(MSG_INTERNAL, pin, *val)
|
def set_property(self, pin, prop, *val):
|
self._send(MSG_PROPERTY, pin, prop, *val)
|
def sync_virtual(self, *pins):
|
self._send(MSG_HW_SYNC, 'vr', *pins)
|
def log_event(self, *val):
|
self._send(MSG_EVENT_LOG, *val)
|
def _send(self, cmd, *args, **kwargs):
|
if 'id' in kwargs:
|
id = kwargs.get('id')
|
else:
|
id = self.msg_id
|
self.msg_id += 1
|
if self.msg_id > 0xFFFF:
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.