11#!/usr/bin/env python3
22
3- # Micro Python library that brings out-of-the-box Blynk support to
4- # the WiPy. Requires a previously established internet connection
5- # and a valid token string.
6- #
7- # Example usage:
8- #
9- # import BlynkLib
10- # import time
11- #
12- # blynk = BlynkLib.Blynk('08a46fbc7f57407995f576f3f84c3f72')
13- #
14- # # define a virtual pin read handler
15- # def v0_read_handler():
16- # # we must call virtual write in order to send the value to the widget
17- # blynk.virtual_write(0, time.ticks_ms() // 1000)
18- #
19- # # register the virtual pin
20- # blynk.add_virtual_pin(0, read=v0_read_handler)
21- #
22- # # define a virtual pin write handler
23- # def v1_write_handler(value):
24- # print(value)
25- #
26- # # register the virtual pin
27- # blynk.add_virtual_pin(1, write=v1_write_handler)
28- #
29- # # register the task running every 3 sec
30- # # (period must be a multiple of 50 ms)
31- # def my_user_task():
32- # # do any non-blocking operations
33- # print('Action')
34- #
35- # blynk.set_user_task(my_user_task, 3000)
36- #
37- # # start Blynk (this call should never return)
38- # blynk.run()
39- #
40- # -----------------------------------------------------------------------------
41- #
42- # This file is part of the Micro Python project, http://micropython.org/
43- #
443# The MIT License (MIT)
454#
46- # Copyright (c) 2015 Daniel Campora
475# Copyright (c) 2015 Volodymyr Shymanskyy
6+ # Copyright (c) 2015 Daniel Campora
487#
498# Permission is hereby granted, free of charge, to any person obtaining a copy
509# of this software and associated documentation files (the "Software"), to deal
7130import sys
7231try :
7332 import machine
33+ idle_func = machine .idle
7434except ImportError :
75- import MachineStub as machine
7635 const = lambda x : x
36+ idle_func = lambda : 0
37+ setattr (sys .modules ['time' ], 'ticks_ms' , lambda : int (time .time () * 1000 ))
7738
7839HDR_LEN = const (5 )
7940HDR_FMT = "!BHH"
10667
10768MAX_VIRTUAL_PINS = const (32 )
10869
109- DISCONNECTED = 0
110- CONNECTING = 1
111- AUTHENTICATING = 2
112- AUTHENTICATED = 3
70+ DISCONNECTED = const ( 0 )
71+ CONNECTING = const ( 1 )
72+ AUTHENTICATING = const ( 2 )
73+ AUTHENTICATED = const ( 3 )
11374
11475EAGAIN = const (11 )
11576
11677def sleep_from_until (start , delay ):
11778 while time .ticks_diff (start , time .ticks_ms ()) < delay :
118- machine . idle ()
79+ idle_func ()
11980 return start + delay
12081
121- class HwPin :
122- _TimerMap = { 'GP9' : (2 , machine .Timer .B ),
123- 'GP10' : (3 , machine .Timer .A ),
124- 'GP11' : (3 , machine .Timer .B ),
125- 'GP24' : (0 , machine .Timer .A ),
126- 'GP25' : (1 , machine .Timer .A )}
127-
128- def __init__ (self , pin_num , mode , pull ):
129- self ._mode = mode
130- self ._pull = pull
131- self ._function = ''
132- self ._pin = None
133- self ._apin = None
134- self ._pwm = None
135- pin_num = int (pin_num )
136- self ._name = 'GP' + str (pin_num )
137-
138- def _config (self , duty_cycle = 0 ):
139- if self ._function == 'dig' :
140- _mode = machine .Pin .OUT if self ._mode == 'out' else machine .Pin .IN
141- if self ._pull == 'pu' :
142- _pull = machine .Pin .PULL_UP
143- elif self ._pull == 'pd' :
144- _pull = machine .Pin .PULL_DOWN
145- else :
146- _pull = None
147- self ._pin = machine .Pin (self ._name , mode = _mode , pull = _pull , drive = machine .Pin .MED_POWER )
148- elif self ._function == 'ana' :
149- adc = machine .ADC (bits = 12 )
150- self ._apin = adc .channel (pin = self ._name )
151- else :
152- timer = machine .Timer (HwPin ._TimerMap [self ._name ][0 ], mode = machine .Timer .PWM )
153- self ._pwm = timer .channel (HwPin ._TimerMap [self ._name ][1 ], freq = 20000 , duty_cycle = (duty_cycle * 100 ))
154-
155- def digital_read (self ):
156- if self ._function != 'dig' :
157- self ._function = 'dig'
158- self ._config ()
159- return self ._pin ()
160-
161- def digital_write (self , value ):
162- if self ._function != 'dig' :
163- self ._function = 'dig'
164- self ._config ()
165- self ._pin (value )
166-
167- def analog_read (self ):
168- if self ._function != 'ana' :
169- self ._function = 'ana'
170- self ._config ()
171- return self ._apin ()
172-
173- def analog_write (self , value ):
174- if self ._function != 'pwm' :
175- self ._function = 'pwm'
176- self ._config (value * 100 )
177- else :
178- self ._pwm .duty_cycle (value * 100 )
179-
18082class VrPin :
18183 def __init__ (self , read = None , write = None ):
18284 self .read = read
@@ -238,13 +140,7 @@ def _handle_hw(self, data):
238140 if cmd == 'info' :
239141 pass
240142 elif cmd == 'pm' :
241- pairs = zip (params [0 ::2 ], params [1 ::2 ])
242- for (pin , mode ) in pairs :
243- pin = int (pin )
244- if mode != 'in' and mode != 'out' and mode != 'pu' and mode != 'pd' :
245- raise ValueError ("Unknown pin %d mode: %s" % (pin , mode ))
246- #self._hw_pins[pin] = HwPin(pin, mode, mode)
247- #self._pins_configured = True
143+ pass
248144 elif cmd == 'vw' :
249145 pin = int (params .pop (0 ))
250146 if pin in self ._vr_pins and self ._vr_pins [pin ].write :
@@ -258,25 +154,8 @@ def _handle_hw(self, data):
258154 self ._vr_pins [pin ].read ()
259155 else :
260156 print ("Warning: Virtual read from unregistered pin %d" % pin )
261- elif self ._pins_configured :
262- if cmd == 'dw' :
263- pin = int (params .pop (0 ))
264- val = int (params .pop (0 ))
265- self ._hw_pins [pin ].digital_write (val )
266- elif cmd == 'aw' :
267- pin = int (params .pop (0 ))
268- val = int (params .pop (0 ))
269- self ._hw_pins [pin ].analog_write (val )
270- elif cmd == 'dr' :
271- pin = int (params .pop (0 ))
272- val = self ._hw_pins [pin ].digital_read ()
273- self ._send (self ._format_msg (MSG_HW , 'dw' , pin , val ))
274- elif cmd == 'ar' :
275- pin = int (params .pop (0 ))
276- val = self ._hw_pins [pin ].analog_read ()
277- self ._send (self ._format_msg (MSG_HW , 'aw' , pin , val ))
278- else :
279- raise ValueError ("Unknown message cmd: %s" % cmd )
157+ else :
158+ raise ValueError ("Unknown message cmd: %s" % cmd )
280159
281160 def _new_msg_id (self ):
282161 self ._msg_id += 1
@@ -389,7 +268,7 @@ class Decorator():
389268 def __init__ (self , func ):
390269 self .func = func
391270 blynk ._vr_pins [pin ] = VrPin (func , None )
392- print (blynk , func , pin )
271+ # print(blynk, func, pin)
393272 def __call__ (self ):
394273 return self .func ()
395274 return Decorator
@@ -424,7 +303,6 @@ def run(self):
424303 self ._hw_pins = {}
425304 self ._rx_data = b''
426305 self ._msg_id = 1
427- self ._pins_configured = False
428306 self ._timeout = None
429307 self ._tx_count = 0
430308 self ._m_time = 0
0 commit comments