function for write to chronodot - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: function for write to chronodot (/thread-4862.html) |
function for write to chronodot - djdan_23 - Sep-11-2017 function for write to chronodot Edit Delete post Report this post Quote Mon Sep 11, 2017 12:34 pm Hi! all 1: Is there a way to simplify the "write_chronodot(s,m,h)", like using a list [ ] instead ? 2: Is there a way to write like in VB, all in one line : sec=1 : min=2 : hour=3 : write_chronodot(sec,min,hour) Here's what I have that run OK : def write_chronodot(s,m,h): data=[s,m,h] i2c_bus.write_i2c_block_data(0x68,0x00,data) # write 3 chronodot registers (sec-min-hour) print ("1= ",data[0]) print ("2= ",data[1]) print ("3= ",data[2]) return def read_chronodot(): chronodot=i2c_bus.read_i2c_block_data(0x68,0x00,3) print ("11= ",chronodot[0]) print ("12= ",chronodot[1]) print ("13= ",chronodot[2]) return chronodot sec=1 min=2 hour=3 write_chronodot(sec,min,hour) # to set time time.sleep(1) # Wait for the chronodot to change the "sec" register a=read_chronodot() print (" sec= ",a[0]) # To verify the time print (" min= ",a[1]) print ("hour= ",a[2])give: 1= 1 2= 2 3= 3 11= 2 # one sec more = OK 12= 2 13= 3 sec= 2 # one sec more = OK min= 2 hour= 3 Thanks RE: function for write to chronodot - hbknjr - Sep-11-2017 (Sep-11-2017, 02:10 PM)djdan_23 Wrote: 1: Is there a way to simplify the "write_chronodot(s,m,h)", like using a list [ ] instead ? 1- what do you mean by simplify? reducing lines of code which of course reduces the readability of the code. eg- with use of list def write_chronodot(data): i2c_bus.write_i2c_block_data(0x68,0x00,data) # write 3 chronodot registers (sec-min-hour) print (f"1= {data[0]} \n2= {data[1]} \n3= {data[2]}") # these are fstrings will only work with python 3.6 return data =[1,2,3] # list[sec,min,hour] write_chronodot(data)2- declaring multiple variables in one line. you can use list else you can do this. sec,min,hour = 1,2,3 # sec = 1, min = 2, hour = 3 |