import time def WioClock_v02o11_2023DApr05_i(): #A version is fully functional #by D@CC on: 2023DApr05 (tested by D@CC) #Purpose: to display the (UNIX) clock on the WioTerminal #Common Name: WioClock #Method Invoked improvised function (not as main.py) #Comment: This is my first useful program for the Wio Terminal #Versioned Name: WioClock_v02o^^_2023DApr05.py #Versioned Name: occurs: 3 times top, def, #def end, invocation #Requirements: ArduPy_wio_terminal_lastest.uf2 (MicroPython) # This program restarts unattended & unconnected. #Device-related: If device="RPi",this program works fine. # If device="RPi" Unix time remove fractional seconds #Issues: 1. L should sense device to work on the RPi # 2. M should use wifi on the RPi # 3. Ishould show HH:M1 in large block letters # 4. M should allow the user to adjust the time # 5. L should allow the user to specify screen dimensions # 6. M should allow writing/reading time from uSD card # 7. L Some of the main parameters should become # external to the program code # 8. L The serial file read should really be in a function # that uses a grep-like function. # 9. M I plan to build versions of it using wioBuild: # wioBuild WioClock_v02o03_2023DApr04 f o # where f points to a function directory and # where o points to the output file name(s) # 10. L A future version will get time from wifi # 11. L How can colored text or background be printed? # 12. M Should use the isDevice() function # 13. L No strip() in MicroPython (I have a work-around function. # 14. M EST cannot be changed (as of 2023DApr06) #Description: 1. prints progname & minicom cmd # 2. requests current UnixTime (deprecated)) # 3. Doesn't yet use device but reads GMT Unix Time from disk # 4. CLS then displays updated clock every 5 seconds # 5. Version WioClock_v02o10_2023DApr05 (stable) works fine # 6. It starts up with GMT Unix Time from the Flash Drive at # media/rtc/previousUnixTimeSec with grepable contents: # PreviousUnixTime 1680564200 (2023DApr03 23:23 GMT) # 7. A future version will let the user adjust the time # after start-up using buttons on the WioTerminal. # For now, the user needs to adjust the start-up # time flashed into the file on the Flash Drive. # 8. The RPi Terminal window can be sized to view # the timing of the screen refreshes. Note # the "|" and "_" are there for this purpose. # 9. Will use CCYY MO DD HH:MI SS to conserve LCD space # 10. Defaults to 2023DApr03 if no UnixTime in media/rtc # 11. On a RPi, media/rtc is subfolder of Desktop. #Functions Coded and Used: # beep() # future # createSquareRows() # exists # isDevice() # future # listForSquareDigit()# exists # localDateTime() # exists # setLocalDateTimeCorrectionForCurrentLocalTime() # exists # show() # exists # showList() # exists # strHHMIfromClock() # exists # strip() # exists # waitForInput() # exists # workInProgress() # exists #External Files: media/rtc/previousUnixTimeSec # # WioClock_v02o^^_2023DApr04.py in MicroPython for the WioTerminal # # displays date/time clock # This versioned program is improvised as a function ..._i(): # it is called by an unchanging stub at the end of this code. # This stub can include the versionized name or the Common # Name. # # I had a problem because "import time" was in the versionned code # and it was not available to a function that needed it. Moved it up. print();print() progName="WioClock_v02o11_2023DApr05_i.py" print("progName:",progName) print("v01o04? actually uses time.time and time.localtime(a)") ###################### initializ main parameters isShow=False #strHHMI="00x00" # will be accurate in next display # read in a previous Unix Time from the last time wioBuild ran to flash WioTerminal # #read from file f=open("media/rtc/previousUnixTimeSec","r") # open previousUnixTimeSec=f.readline() # read print("previousUnixTimeSec:",previousUnixTimeSec) # display only (don't use f.close() # close print("full record:",previousUnixTimeSec) # displayfull record temp=previousUnixTimeSec[0:16] # grep name print("temp:",temp) temp2=previousUnixTimeSec[1:16] # result is the Unix Time print("temp2:",temp2) # at start-up prevUnixTimeFromDisk= previousUnixTimeSec[17:27] # prevUnixTime (in seconds) print("prevUnixTimeFromDisk:",prevUnixTimeFromDisk,":") minicom="minicom -o -D /dev/ttyACM0" print("minicom:",minicom) #import time x=dir(time) print("x:",x) a=time.time() print("a:",a) try: if False : a=time.localtime(a) print("a:",a) print("after try:") b=time.localtime(1046684836) #+ ~10% print("b:",b) #= 946684836 c= 946684836+94668483 # + 10% # c was too big for localtime(c) #print("c:",c) c= 946684836+94668483+94668483+94668483 # + 30% print("c:",c) c=time.localtime(c) print("c:",c) d= 946684836+94668483+94668483+94668483 # + 30% d= d+94668483+94668483+94668483 # + 60% print("d:",d) d=time.localtime(d) print("d:",d) d= 946684836+94668483+94668483+94668483 d= d+94668483+94668483+94668483 # + 60% e= d+94668483+94668483 # + 20% = +80% print("e:",e) e=time.localtime(e) print("e:",e) f= 1688254620 print("f:",f) f=time.localtime(f) print("f:",f) #if end #try end except: print("after except:") #c=time.localtime(a) c="abc" print("c:",c) b=c finally: b="b" print("after finally:") print("b:",b) #try end print() print("progName:",progName) device="WioTerminal" print("device:",device) g=time.time() print("g:",g) timeRTC_UnixSeconds=g print("timeRTC_UnixSeconds:",timeRTC_UnixSeconds) if device=="WioTerminal": dateTimeRTC=time.localtime(timeRTC_UnixSeconds) #convert time.time to date & time else: dateTimeRTC=time.ctime(timeRTC_UnixSeconds) #convert time.time to date & time #if end #dateTimeRTC=1680402493.0272472 print("localClock (without correction):",dateTimeRTC) #print it out # INPUT #timeNow_UnixSeconds=int(input("Enter localTime (in Pythix seconds)~1688254620 ? ")) #next line deprecated #timeNow_UnixSecondsStr=input("Enter localTime (in Unix seconds)~1680422400 ? ") if True: # always use the Unix Time read from disk if prevUnixTimeFromDisk=="": timeNow_UnixSeconds = 1680647000 # a valid time Apr 4, 2023 else: timeNow_UnixSeconds = int(prevUnixTimeFromDisk) # time from last wioBuild #if end else: # old deprecated code if timeNow_UnixSecondsStr=="": timeNow_UnixSeconds = int(prevUnixTimeFromDisk) else: timeNow_UnixSeconds = int(timeNow_UnixSecondsStr) #if end #if end # if device=="WioTerminal": dateTimeEntered=time.localtime(timeNow_UnixSeconds) #convert time.time to date & time else: dateTimeEntered=time.ctime(timeNow_UnixSeconds) #convert time.time to date & time #if end print("dateTimeEntered:",dateTimeEntered) #print it out #print("d") #timeCorrection_UnixSeconds = timeNow_UnixSeconds - timeRTC_UnixSeconds #print("timeCorrection_UnixSeconds wo function:",timeCorrection_UnixSeconds) #print("e") timeCorrection_UnixSeconds=setLocalDateTimeCorrectionForCurrentLocalTime(timeNow_UnixSeconds,device) #print("timeCorrection_UnixSeconds w function:",timeCorrection_UnixSeconds) #progName="WioClock_v01o28_2023DApr03.py" # this loop (updates &) displays the clock ################################ strHHMI="00x00" # will be accurate in next display while 1: t=time.time()+ timeCorrection_UnixSeconds #print("t:",t) print() #flush out the current line print(" progName:",progName) #at top of LCD print(" minicom cmd:","minicom -o -D /dev/ttyACM0 ",end="") print(strHHMI,"|") print(" media/rtc/previousUnixTimeSec: ","device:WioTerminal") print("",previousUnixTimeSec) #listForSquareDigits("0") #print("@209 strHHMI=") #print("invoking :","strHHMIfromClock") strHHMI=strHHMIfromClock(timeCorrection_UnixSeconds,t,device,isShow) #get strHHMI #print("tf",strHHMI) #strHHMI="23:45" #HH:MM to display bit strPreface=". " msgIn="" #print("@216") isHang=False if isHang : while msgIn=="": #TimeOut will return null #TimeOut time is 10 seconds #enter Key will return "/r" #print("@223") # if wait. . . . isFalse # no waiting # and returns "" #msgIn will be "" ie null print("@228") msgIn=waitForInput("@233",10,False) ##### Wait for CR ################ print("@238") if not msgIn=="/r": msgIn="" #ignore all except "/" else : msgIn = "" break #break out of while # Enter (ie "/r" breaks out of while #while end else : #Don't hang Doh=1 #just continue #if end #print("@251") #show("strHHMI",strHHMI,isShow) #for end #show("strDisplay",strDisplay,True) #show("listDisplay",listDisplay,True) for z in range(2):print() #print("@257 before createSquareRows()") #print("before createSquareRows() which will print 5 rows") createSquareRows(5,strPreface,strHHMI,True) manyBlanks=". " print(manyBlanks,"EST") #print("@261") #waitForInput("@262",10,True) ##### Wait for CR ################ #print("@263") #print("after createSquareRows()") #show("rows5",rows5,True) #returns a 5 row (formatted for LCD) #strDisplay=strPreface #list to contain) #waitForInput("270",10,True) #for z in range(5): # print(" HH:MI ***** ***** * ***** ***** ") #for end for z in range(2): print() #for end clockNow=localDateTime(timeCorrection_UnixSeconds,device) #at bottom of LCD #print("****************************") bottomrightcorner=") _" print("clockNow:",clockNow,"EST(Unix time:",t,bottomrightcorner,end="") # the end="" above suppresses the CRLF. #print(" _|") #print("timeCorrection(UnixSeconds):",timeCorrection_UnixSeconds) time.sleep(5) #5 seconds #while end # version WioClock_v01o19.py worked pretty well # Renamed to main.py # Copied to Ardupy top folder on 2023DApr02 00:16 #main.py output: #b: struct_time(tm_year=2003, tm_mon=3, tm_mday=3, tm_hour=9, tm_min=47, tm_sec=16, tm_wday=0, tm_y #day=62, tm_isdst=-1) #g: 946689854 return #def end #end WioClock_v02o05_2023DApr04_i ######################################################################################### #strHHMIfromClock() def strHHMIfromClock(timeCorrection_UnixSeconds,t,device,isShow): #returns strHHMI #clockUp=localDateTime(timeCorrection_UnixSeconds,device) #at bottom of LCD clockUp=localDateTime(timeCorrection_UnixSeconds,device) #at bottom of LCD strClockUp=str(clockUp) show("strClockUp",strClockUp,isShow) #tm_hr=22 tm_min=12 in ClockUp colHH=strClockUp.find(" tm_hour=")+9 #show("colHH",colHH,isShow) endHH=colHH+2 strHH=strip(strClockUp[colHH:endHH]) #show("strHH",strHH,True) if strHH[-1]=="," : #print("need to insert a0") strHH="0"+strHH[0:1] #show("strHH",strHH,True) #if end colMI=strClockUp.find(" tm_min=")+8 #show("colMI",colMI,isShow) endMI=colMI+2 strMI=strip(strClockUp[colMI:endMI]) #show("strMI",strMI,True) if strMI[-1]=="," : #print("need to insert a0") strMI="0"+strMI[0:1] #show("strMI",strMI,True) #if end strHHMI=strHH+":"+strMI #show("strHHMI",strHHMI,True) #colMI=clockUp.find(" tm_min=") #show("colMI",colMI,True) #strHHMI="18:53" #strHHMI=strHH+":"+strMI #show("@325 strHHMI",strHHMI,True) bottomrightcorner=" _" #print("clockUp:",clockUp,"EST Unix time:",t,bottomrightcorner,end="") #strHHMI=time.strftime('%H:%M') #Without correction # doesn't work in MicroPython on WioTerminal #waitForInput("@341",10,True) #Hang Here #show("strHHMI",strHHMI,True) return strHHMI #end of strHHMIfromClock(isShow) ######################################################################################### def createSquareRows(nRows,strPreface,strHHMI,isShow): if nRows<1 : print("Error S07 createSquareRows, nRows is <1:",nRows) #if end #print("into createSquareRows") #Purpose: to create 5 rows of square DIGITs #Returns: nothing, but prints 5 lines # each row contains strPreface, row of sqChar for each of HH:MI #strDisplay=strPreface #list to contain rows to display #strDigit="0" # digit to display #strDigit=strHHMI[0:1] # get 1st char to display #show("strDigit",strDigit,True) #show("@359, strHHMI:",strHHMI,True) listDisplay=[] #listDisplay.append("2") #listDisplay.append("3") #print("@352") #showList("listDisplay",True,listDisplay,True) showList("listDisplay",True,listDisplay,True) #print("@365") for i in range(nRows): #for each of the 5 rows strRow=strPreface for iC in range(len(strHHMI)): c=strHHMI[iC:iC+1] # for each character to show #show("c",c,isShow) #strDigit="0" # digit to display listForSqChar=listForSquareDigit(c) #2dim list of this sq char #showList("listForSqChar",True,listForSqChar,isShow) strSqCharRow=listForSqChar[i] #str of row i of this sq char #showList("listSqCharRow",True,strSqCharRow,isShow) #print("@376") #strSqCharRow =str(strSqCharRow) #show("strSqCharRow",strSqCharRow,isShow) strRow=strRow+strSqCharRow+" " # make it into a str #for end #show("strRow",strRow,True) # full row #print("@382") #show("strRow",strRow,isShow) #print(strRow) listDisplay.append(strRow) #for end showList("listDisplay",False,listDisplay,True) #strDisplay=strDisplay+str(listSquareDigit[0])+" " #create row return #def end #listOfObjecInList.def def show(objName,obj,isShow): if not isShow: return if not isinstance(objName,str): print("Error S01 in show Name is not a str:",objname) #if end if isinstance(obj, str) or isinstance(obj,list) or isinstance(obj,int)or isinstance(obj,bool): #is ok print(objName,":",obj,":") else: typeObj=type(obj) print("Error S02 in show obj is not a str, list, int nor bool:",objName,"type:",typeObj,"value:",obj) return #if end #def end ############################################################### def showList(listName,isPreface,listA,isShow): if not isShow : return if not isinstance(listA, list): typeObj=type(listA) print("Error S05 in showList, obj is not alist:",listName,"type:",typeObj,"value:",listA,":") return #if end rows_listA=len(listA) #show("rows_listA",rows_listA,isShow) for i in range(rows_listA): if isPreface : print(listName,":") print(listName,"[",i,"]:",listA[i]) else: print(listA[i]) return #def end ############################################################### def waitForInput(strAt,timeOutSecs,isShow): #Note TimeOut is not yet implemented #if isShow is False, return null string immediately if isShow==False :return "" print("statement @:",strAt,end="") result=input(" hanging till CR") if result=="": result="/r" #convert null (CR) into "/r" #print("hangForInput, result:",result) return result #def end ############################################################### def strip(strA,isShow=False): strC=strA #print(" strC:",strC,":") # before changes if strA=="": return strC lenC=len(strC) "lenC:",lenC if not (strC[0:1]==" "): # if no preceding spaces #no preceding spaces Doh=1 else: # strip blanks from front show("strC:",strC,isShow) while strC[0:1]==" ": strB=strC #print("strB:",strB,":") # remove 1 char from front strB=strC[1:] #remove first blank #print("strB:",strB,":") strC=strB #while end #print("done removing preceding blanks") #if end #check for blanks at end #check if any blanks at end if (strC==""): return strC if not (strC[-1]==" "): return strC #strip blanks from rear while strC[-1]==" ": #test if last char blank strB=strC #print("strB:",strB,":") # remove 1 char from rear lenC=len(strC) lenCLess1=len(strC)-1 strB=strC[0:lenCLess1] #remove last blank strC=strB #while end #print("done removing trailing blanks") #print("returning:",strC,":") return strC #def end ##################################################################### ################################################################ def setLocalDateTimeCorrectionForCurrentLocalTime(currentLocalTime_UnixSeconds,device): #print("in setLocalDateTime....") #print("currentLocalTime_UnixSeconds entered was:",currentLocalTime_UnixSeconds) #print("Date Time Now:",time.localtime(currentLocalTime_UnixSeconds)) #print("Date Time Now entered:",time.ctime(currentLocalTime_UnixSeconds)) timeRTC_UnixSeconds = time.time() #print("timeRTC_UnixSeconds:",timeRTC_UnixSeconds) if device=="WioTerminal": print("timeRTC_DateTime:",time.localtime(timeRTC_UnixSeconds)) else : print("timeRTC_DateTime:",time.ctime(timeRTC_UnixSeconds)) #if end timeCorrection_UnixSeconds = currentLocalTime_UnixSeconds - timeRTC_UnixSeconds #if end #print("timeCorrection_UnixSeconds:",timeCorrection_UnixSeconds) return timeCorrection_UnixSeconds #def end def localDateTime(timeCorrection_UnixSeconds,device): #print("in localDateTime") localTime_UnixSeconds= time.time()+ timeCorrection_UnixSeconds #print("localTime_UnixSeconds+:",localtime_UnixSeconds) #localTime_UnixSeconds= time.time()- timeCorrection_UnixSeconds #print("localTime_UnixSeconds-:",localtime_UnixSeconds) if device=="WioTerminal": localDateTime = time.localtime(localTime_UnixSeconds) else: localDateTime = time.ctime(localTime_UnixSeconds) #if end #print("localDateTime:",localDateTime) return localDateTime #def end def listForSquareDigit(digit): #print("into listForSquareDigit, digit:",digit) #Purpose: returns a Python list containing 5 rows (0 to 4) of the digit # in big square format (5x5) # default return is a big "X" # digit must be a single str in "0123456789 :" result=workInProgress(digit) #print("@522f") #showList("result",True,result,True) return result #def end def workInProgress(digit): #Returns list of 5 rows big quare digit # digit must be char 0 to 9 or ":" or space # default output is a big "X" listSquare=[ \ "* *", \ " * * ", \ " * ", \ " * * ", \ "* *" ] #if not objtype(digit)=="str": return listSquare if not digit in "0123456789: " : # invalid digit (":" and " " are acceptable) return listSquare #if end isOK=False if digit=="0" : listSquare=[ \ " *** ", \ "* *", \ "* *", \ "* *", \ " *** " ] isOK=True #if end if digit=="1" : listSquare=[ \ " * ", \ " ** ", \ " * ", \ " * ", \ " *** " ] isOK=True #if end if digit=="2" : listSquare=[ \ " *** ", \ "* *", \ " * ", \ " * ", \ "*****" ] isOK=True #if end if digit=="3" : listSquare=[ \ " *** ", \ "* *", \ " * ", \ "* *", \ " *** " ] isOK=True #if end if digit=="4" : listSquare=[ \ " * ", \ " ** ", \ " * * ", \ "*****", \ " * " ] isOK=True #if end if digit=="5" : listSquare=[ \ "**** ", \ "* ", \ "**** ", \ " *", \ " *** " ] isOK=True isOK=True #if end if digit=="6" : listSquare= [ \ " *** ", \ "* ", \ "**** ", \ "* *", \ " *** " ] isOK=True #if end if digit=="7" : listSquare=[ \ "*****", \ " * ", \ " * ", \ " * ", \ " * " ] isOK=True #if end if digit=="8" : listSquare= [ \ " *** ", \ "* *", \ " *** ", \ "* *", \ " *** " ] isOK=True #if end if digit=="9" : listSquare= [ \ " *** ", \ "* *", \ " ****", \ " *", \ " *** " ] isOK=True #if end if digit==":" : listSquare= [ \ " ", \ " * ", \ " ", \ " * ", \ " " ] isOK=True #if end if digit==" " : listSquare= [ \ " ", \ " ", \ " ", \ " ", \ " " ] isOK=True #if end if isOK : Doh=0 else: # digit not created yet beep() return listSquare #if end return listSquare #def end def beep(): # need to insert code for buzzer return #def end ############################################################################# #invoke the program as a function Doh=WioClock_v02o11_2023DApr05_i() # WioClock_v02o^^_2023DApr04 #/ WioClock_v02o^^_2023DApr04.py