I've been using it as my 'software intern'. It doesn't come up with amazingly insightful code, and sometimes it misunderstands the task at hand, but it does save some coding drudge work.
For example, 'In Python on a raspberry pi, write a class that contains functions to read the time from a DS1307 using I2C, use that time to set the system time, can retrieve the time from the internet, and save the internet time to the DS1307. Additionally, write code to check whether the time has been retrieved successfully from the internet and choose whether to update system time from the internet or the DS1307 accordingly'
It said this :
import smbus
from datetime import datetime
import ntplib
import time
class RTC_DS1307:
def __init__(self, bus=1, addr=0x68):
self.bus = smbus.SMBus(bus)
self.addr = addr
def bcd_to_decimal(self, bcd):
return (bcd & 0x0F) + ((bcd >> 4) * 10)
def decimal_to_bcd(self, decimal):
return ((decimal // 10) << 4) + (decimal % 10)
def read_time(self):
self.bus.write_byte(self.addr, 0x00)
data = self.bus.read_i2c_block_data(self.addr, 0, 7)
year = self.bcd_to_decimal(data[6]) + 2000
month = self.bcd_to_decimal(data[5])
day = self.bcd_to_decimal(data[4])
hour = self.bcd_to_decimal(data[2] & 0x3F)
minute = self.bcd_to_decimal(data[1])
second = self.bcd_to_decimal(data[0])
return datetime(year, month, day, hour, minute, second)
def set_time(self, dt):
self.bus.write_byte_data(self.addr, 0x00, self.decimal_to_bcd(dt.second))
self.bus.write_byte_data(self.addr, 0x01, self.decimal_to_bcd(dt.minute))
self.bus.write_byte_data(self.addr, 0x02, self.decimal_to_bcd(dt.hour))
self.bus.write_byte_data(self.addr, 0x03, self.decimal_to_bcd(dt.isoweekday()))
self.bus.write_byte_data(self.addr, 0x04, self.decimal_to_bcd(dt.day))
self.bus.write_byte_data(self.addr, 0x05, self.decimal_to_bcd(dt.month))
self.bus.write_byte_data(self.addr, 0x06, self.decimal_to_bcd(dt.year - 2000))
def update_from_internet(self):
try:
c = ntplib.NTPClient()
response = c.request('pool.ntp.org')
t = datetime.fromtimestamp(response.tx_time)
self.set_time(t)
return True
except:
return False
def update_system_time(self):
if self.update_from_internet():
print("Time updated from internet")
else:
t = self.read_time()
print("Time updated from DS1307")
set_time(t)
rtc = RTC_DS1307()
rtc.update_system_time()
I haven't tested it, it probably needs some tweaking, but that was written in about a minute, freeing me up to do other, more interesting, coding.