Python Forum
function for write to chronodot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function for write to chronodot
#1
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
Reply
#2
(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 ?
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)

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Better way to write this function SephMon 1 816 Feb-08-2023, 10:05 PM
Last Post: Gribouillis
  write new function or change the old one to work "smartter? korenron 3 1,977 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,431 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  How to write a code with İF function? Aycaaxx 1 1,843 Nov-03-2020, 05:46 AM
Last Post: deanhystad
  Print/write to file function tpolim008 4 2,789 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,124 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  write an integer to file using a function xvkxvo 4 3,195 Dec-10-2019, 11:27 PM
Last Post: xvkxvo
  how can I write my function output on CSV file go127a 13 5,541 May-05-2019, 01:55 PM
Last Post: go127a
  Use the write function manonB 2 2,535 Apr-17-2019, 05:55 AM
Last Post: manonB
  Help trying to write a function Rochense 3 4,264 Apr-12-2019, 02:34 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020