Posts: 1,150
Threads: 42
Joined: Sep 2016
LOL indeed, I was wondering who'd be first to comment it :)
Posts: 2,953
Threads: 48
Joined: Sep 2016
Apr-05-2017, 06:03 AM
(This post was last modified: Apr-05-2017, 01:18 PM by Kebap.)
Well, put the message as a Python comment. It will be coloured green. Replace 'code' with 'Python code'. Attract the attention to that word Python. Even upper case it. I don't know...
How about this post? He uses the python tags for the first two code snippets but when the big one comes it is a plain text.
(Apr-05-2017, 04:59 AM)gray Wrote: i want to set time & date by ds1307 IC...I added rtc-ds1307 to /etc/modules my program is
[
from RTC_DS1307 import RTC
rtc = RTC()
rtc.setDate(seconds = 0, minutes = 47, hours = 9, dow = 3,
day = 8, month = 6, year = 16)
print ("Date set to:", rtc.getDateStr()) ][/python]
'setDate' is a function in 'RTC_DS1307.py' that gets value of 'second', 'minute','hour',... and then puts every one of these values in itself register by '_write' function & 'write_byte_data' command i considered a seperate register for 'second','minute',...by defining a hex value for every register. now, my problem is i get the following error..please helpppp
[
Traceback (most recent call last):
File "/home/pi/start/time/set-get.py", line 5, in <module>
day = 8, month = 6, year = 16)
File "/home/pi/start/time/RTC_DS1307.py", line 124, in setDate
self._write(self._REG_SECONDS, intToBcd(seconds))
File "/home/pi/start/time/RTC_DS1307.py", line 54, in _write
self._bus.write_byte_data(self._addr, register, data)
OSError: [Errno 16] Device or resource busy 'RTC_DS1307.py' is as following
# RTC_DS1307.py
# Code inspired from GitHub: sorz/DS1307.py
import smbus
def bcdToInt(bcd):
'''
Converts byte interpreted as two digit bcd to integer
(e.g. 88 = b01011000->bcd0101'1000 = 58)
'''
out = 0
for d in (bcd >> 4, bcd):
for p in (1, 2, 4 ,8):
if d & 1:
out += p
d >>= 1
out *= 10
return out // 10
def intToBcd(n):
'''
Converts integer 0..99 to byte interpreted in two digit bcd format.
(e.g. 58 = bcd0101'1000->b01011000 = 88)
'''
bcd = 0
for i in (n // 10, n % 10):
for p in (8, 4, 2, 1):
if i >= p:
bcd += 1
i -= p
bcd <<= 1
return bcd >> 1
class RTC():
_REG_SECONDS = 0x00
_REG_MINUTES = 0x01
_REG_HOURS = 0x02
_REG_DOW = 0x03
_REG_DAY = 0x04
_REG_MONTH = 0x05
_REG_YEAR = 0x06
_REG_CONTROL = 0x07
def __init__(self, type = 1, addr = 0x68):
'''
Creates a Real Time Clock abstraction using given SMBus type and I2C address
@param type: 0 for RPi model A, 1 for higher versions (default: 1)
@param addr: I2C address (default: 0x68)
'''
self._bus = smbus.SMBus(type)
self._addr = addr
def _write(self, register, data):
self._bus.write_byte_data(self._addr, register, data)
def _read(self, data):
returndata = self._bus.read_byte_data(self._addr, data)
return returndata
def getSeconds(self):
'''
Returns current seconds.
@return: seconds of current date/time (0..59)
'''
return bcdToInt(self._read(self._REG_SECONDS))
def getMinutes(self):
'''
Returns current minutes.
@return: minutes of current date/time (0..59)
'''
return bcdToInt(self._read(self._REG_MINUTES))
def getHours(self):
'''
Returns current hours.
@return: hours of current date/time (0..23)
'''
d = self._read(self._REG_HOURS)
if (d == 0x64):
d = 0x40
return bcdToInt(d & 0x3F)
def getDow(self):
'''
Returns current day of week.
@return: day number of current date/time (1..7, 1 for Monday)
'''
return bcdToInt(self._read(self._REG_DOW))
def getDay(self):
'''
Returns current day of month.
@return: day of current date/time (1..31)
'''
return bcdToInt(self._read(self._REG_DAY))
def getMonth(self):
'''
Returns current month.
@return: month of current date/time (1..12)
'''
return bcdToInt(self._read(self._REG_MONTH))
def getYear(self):
'''
Returns current year.
@return: year of current date/time (0..99)
'''
return bcdToInt(self._read(self._REG_YEAR))
def setDate(self, seconds = None, minutes = None, hours = None, dow = None,
day = None, month = None, year = None):
'''
Sets the current date/time.
Range: seconds [0,59], minutes [0,59], hours [0,23],
day_of_week [1,7], day [1-31], month [1-12], year [0-99].
If a parameter is None (default), the current value is unchanged
'''
if seconds is not None:
if seconds < 0 or seconds > 59:
raise ValueError('Seconds is out of range [0,59].')
self._write(self._REG_SECONDS, intToBcd(seconds))
if minutes is not None:
if minutes < 0 or minutes > 59:
raise ValueError('Minutes is out of range [0,59].')
self._write(self._REG_MINUTES, intToBcd(minutes))
if hours is not None:
if hours < 0 or hours > 23:
raise ValueError('Hours is out of range [0,23].')
self._write(self._REG_HOURS, intToBcd(hours))
if year is not None:
if year < 0 or year > 99:
raise ValueError('Years is out of range [0,99].')
self._write(self._REG_YEAR, intToBcd(year))
if month is not None:
if month < 1 or month > 12:
raise ValueError('Month is out of range [1,12].')
self._write(self._REG_MONTH, intToBcd(month))
if day is not None:
if day < 1 or day > 31:
raise ValueError('Day is out of range [1,31].')
self._write(self._REG_DAY, intToBcd(day))
if dow is not None:
if dow < 1 or dow > 7:
raise ValueError('Day Of Week is out of range [1,7].')
self._write(self._REG_DOW, intToBcd(dow))
def getDate(self):
'''
Returns the current date/time.
@return: date/time in a tuple with order
(year, month, day, dow, hours, minutes, seconds)
'''
return (self.getYear(), self.getMonth(), self.getDay(),
self.getDow(), self.getHours(), self.getMinutes(),
self.getSeconds())
def getDateStr(self):
'''
Returns the current date/time.
@return: date/time in a string with format
year-month-day hours:minutes:seconds
'''
return "20%02d-%02d-%02d %02d:%02d:%02d" %(self.getYear(), self.getMonth(), self.getDay(), self.getHours(), self.getMinutes(), self.getSeconds())
]
Posts: 470
Threads: 29
Joined: Sep 2016
Apr-05-2017, 09:34 AM
(This post was last modified: Apr-05-2017, 09:34 AM by Kebap.)
(Apr-04-2017, 12:03 AM)metulburr Wrote: I added default messages for the forums "general coding help" and "homework" of ....
[python]my code here[ /python]
How about this default then?
Quote:my question
[python]my code here[ /python]
By the way, I would not moderate and fix the layout in the linked posts then just yet just for your continued comparison.
edit: Maybe include screenshots for comparison in this thread, so we can repair the original posts?
Posts: 5,151
Threads: 396
Joined: Sep 2016
Apr-05-2017, 12:20 PM
(This post was last modified: Apr-05-2017, 12:20 PM by metulburr.)
You have got to be kidding me? seriously? LMFAO
(Apr-05-2017, 09:34 AM)Kebap Wrote: By the way, I would not moderate and fix the layout in the linked posts then just yet just for your continued comparison.
edit: Maybe include screenshots for comparison in this thread, so we can repair the original posts? changed the layout to
Quote:my question here
[python]my code here[ /python]
and also quoted each post example in this thread for reference, but havent fixed the originals
Recommended Tutorials:
Posts: 2,953
Threads: 48
Joined: Sep 2016
How did I quote a post?
Posts: 470
Threads: 29
Joined: Sep 2016
I have fixed the originals now, apart from the first one which is just too cute and also got quoted and commented there so would be weird. Maybe if the user posts again. Also the last one, Larz did already fix before, so I rebroke your quote above :)
Posts: 5,151
Threads: 396
Joined: Sep 2016
Apr-05-2017, 04:52 PM
(This post was last modified: Apr-05-2017, 04:52 PM by metulburr.)
whoops the last few posts in the past 4 hours were my fault for the incorrect tags. I had a space in the ending tag brackets, breaking the BBCode for the default messages. fixed now.
Quote:my question
[python]my code here[ /python]
Recommended Tutorials:
Posts: 470
Threads: 29
Joined: Sep 2016
Apr-07-2017, 10:00 AM
(This post was last modified: Apr-07-2017, 10:00 AM by Kebap.)
Posts: 5,151
Threads: 396
Joined: Sep 2016
Apr-07-2017, 12:11 PM
(This post was last modified: Apr-07-2017, 12:27 PM by Kebap.)
where is this guy getting his code tags from? They are inside the python tags.
https://python-forum.io/Thread-HELP-Stri...2#pid14392
(Apr-07-2017, 03:04 AM)schwasskin Wrote: I have some code that I am using to convert a file to bytes then those bytes to strings of 0's and 1's.
[code]with open(file, mode='rb') as file:
content = file.read()
b = bytearray(content)
for number in b:
data = bin(number)[2:].zfill(8)
print data[/code] I get output like 10110100, 00000001, etc, etc as it should be.
I want to take those strings of bits I get in my output and convert them back to bytes, then write those bytes back to a new file.
I have tried with the following code:
[code]list = ('11111111', '11111110', '01101000', '00000000', '01101001', '00000000', '00001101', '00000000', '00001010', '00000000')
def writer(stuff):
blob = struct.pack('!H', int(stuff, 2))
with open('test.txt', "a+b") as file:
file.write(blob)
for strings in list:
writer(strings)[/code] The original text file contains the word "hi" and that is all, but when I write the new file the output is "▒▒hi"
Recommended Tutorials:
Posts: 2,342
Threads: 62
Joined: Sep 2016
@ Kebap, that first one actually has a lot of posts already.
|