Skip to content

Instantly share code, notes, and snippets.

@CloudyPadmal
Created November 17, 2020 20:34
Show Gist options
  • Save CloudyPadmal/fd2a302c4581c9e3fb5f65d61533b4b3 to your computer and use it in GitHub Desktop.
Save CloudyPadmal/fd2a302c4581c9e3fb5f65d61533b4b3 to your computer and use it in GitHub Desktop.
import serial
import time
import struct
import datetime
Byte = struct.Struct("B")
NONSTANDARD_IO = Byte.pack(14)
DS1307 = Byte.pack(4)
s = serial.Serial(port='/dev/ttyUSB0', baudrate=1000000, timeout=0.1)
TT = datetime.datetime.now()
tenSeconds = '{0:04b}'.format(TT.second // 10)
norSeconds = '{0:04b}'.format(TT.second % 10)
seconds = int(tenSeconds + norSeconds, 2)
tenMinutes = '{0:04b}'.format(TT.minute // 10)
norMinutes = '{0:04b}'.format(TT.minute % 10)
minutes = int(tenMinutes + norMinutes, 2)
tenHours = '{0:02b}'.format(TT.hour // 10)
norHours = '{0:04b}'.format(TT.hour % 10)
hour = int('01' + tenHours + norHours, 2)
day = int('{0:08b}'.format(TT.weekday()), 2)
tenDate = '{0:04b}'.format(TT.day // 10)
norDate = '{0:04b}'.format(TT.day % 10)
date = int(tenDate + norDate, 2)
tenMonth = '{0:04b}'.format(TT.month // 10)
norMonth = '{0:04b}'.format(TT.month % 10)
month = int(tenMonth + norMonth, 2)
year = int(str(TT.year)[2:])
tenYear = '{0:04b}'.format(year // 10)
norYear = '{0:04b}'.format(year % 10)
year = int(tenYear + norYear, 2)
seconds = Byte.pack(seconds)
minutes = Byte.pack(minutes)
hour = Byte.pack(hour)
day = Byte.pack(day)
date = Byte.pack(date)
month = Byte.pack(month)
year = Byte.pack(year)
control = Byte.pack(0)
readTime = True
s.write(NONSTANDARD_IO);
s.write(DS1307);
if (readTime):
s.write(Byte.pack(0));
toRead = 7
nArr = []
while(1):
A = list(s.read(1))
if (A):
nArr.append(A[0])
toRead = toRead - 1
if (toRead == 0):
break
time.sleep(0.05)
ss = '{0:08b}'.format(int(nArr[0]))
mm = '{0:08b}'.format(int(nArr[1]))
hh = '{0:08b}'.format(int(nArr[2]))
dd = '{0:08b}'.format(int(nArr[3]))
dt = '{0:08b}'.format(int(nArr[4]))
mo = '{0:08b}'.format(int(nArr[5]))
ye = '{0:08b}'.format(int(nArr[6]))
second = int(ss[:4], 2) * 10 + int(ss[4:], 2)
minute = int(mm[:4], 2) * 10 + int(mm[4:], 2)
hour = int(hh[2:4], 2) * 10 + int(hh[4:], 2)
day = int(dd[5:], 2)
date = int(dt[2:4], 2) * 10 + int(dt[4:], 2)
month = int(mo[3], 2) * 10 + int(mo[4:], 2)
year = 2000 + int(ye[:4], 2) * 10 + int(ye[4:], 2)
Week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
currentTime = str(year) + '-'+ str("{:02.0f}".format(month)) + '-' + str("{:02.0f}".format(date)) + ' | ' + str("{:02.0f}".format(hour)) + ':' + str("{:02.0f}".format(minute)) + ':' + str("{:02.0f}".format(second)) + ' -- [' + Week[day] + ']'
print(currentTime)
else:
s.write(Byte.pack(1));
s.write(seconds)
s.write(minutes)
s.write(hour)
s.write(day)
s.write(date)
s.write(month)
s.write(year)
s.write(control)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment