Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flake8 and sonarcloud fixes #225

Draft
wants to merge 42 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
cc5263c
Update prtc.py
shawaj Jan 4, 2023
0632497
Update prtc.py
shawaj Jan 4, 2023
ea26d1e
Update prtc.py
shawaj Jan 4, 2023
cebb162
Update prtc.py
shawaj Jan 4, 2023
20f6b68
Update pwrite_text.py
shawaj Jan 4, 2023
ebbdc30
Update pwrite_text.py
shawaj Jan 4, 2023
be408c4
Update setup.py
shawaj Jan 4, 2023
062b7d6
Update setup.py
shawaj Jan 4, 2023
af1795f
Update setup.py
shawaj Jan 4, 2023
569f3fd
Update __init__.py
shawaj Jan 4, 2023
f2818ef
Update composite.py
shawaj Jan 4, 2023
2d0e287
Update epd.py
shawaj Jan 4, 2023
98f8960
Update epd.py
shawaj Jan 4, 2023
e8eb72d
Update image.py
shawaj Jan 4, 2023
beeb980
Update lm75b.py
shawaj Jan 5, 2023
1c9a446
Update readrtc.py
shawaj Jan 5, 2023
a109136
Update text.py
shawaj Jan 5, 2023
8d036b2
Update textpos.py
shawaj Jan 5, 2023
032e314
Update twitter_feed.py
shawaj Jan 5, 2023
886eca4
Update twitter_feed.py
shawaj Jan 5, 2023
6a743c4
Update setup.py
shawaj Jan 5, 2023
ea509dd
Update setup.py
shawaj Jan 5, 2023
9cadb7c
Update setup.py
shawaj Jan 5, 2023
d355614
Update setup.py
shawaj Jan 5, 2023
48dabe5
Update twitter_feed.py
shawaj Jan 5, 2023
639e07c
Update textpos.py
shawaj Jan 5, 2023
bbbc7dd
Update text.py
shawaj Jan 5, 2023
62fad5b
Update textpos.py
shawaj Jan 5, 2023
fb7413a
Update textpos.py
shawaj Jan 5, 2023
339b0f6
Update readrtc.py
shawaj Jan 5, 2023
b3f6cce
Update lm75b.py
shawaj Jan 5, 2023
7ff353f
Create setup.cfg
shawaj Jan 5, 2023
e259114
Update epd.py
shawaj Jan 5, 2023
a6fcef3
Update textpos.py
shawaj Jan 6, 2023
53ef6b0
Update textpos.py
shawaj Jan 6, 2023
9658bd2
Update textpos.py
shawaj Jan 6, 2023
c08258a
Update textpos.py
shawaj Jan 6, 2023
a34e538
Update text.py
shawaj Jan 6, 2023
82b588a
Update textpos.py
shawaj Jan 6, 2023
0023db4
Update pwrite_text.py
shawaj Jan 6, 2023
80faeea
Update epd.py
shawaj Jan 6, 2023
ac8ab91
Update epd.py
shawaj Jan 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions RTC-Hat-Examples/RTCreboot/prtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

from datetime import datetime
from calendar import isleap
from smbusf import SMBus

RTCADR = 0x6f
STBIT = 0x80
LPYR = 0x20
LPYR = 0x20
almbase = [0xa, 0x11]


def tobcd(val):
return (val % 10) | (val // 10) << 4


def writertc(i2cbus, dt):
sec = dt.second
min = dt.minute
hour = dt.hour
# rtc-ds1307 uses weekday convention Sun = 1, Sat = 7
wkday = (dt.weekday() + 1)%7 + 1
wkday = (dt.weekday() + 1) % 7 + 1
day = dt.day
month = dt.month
year = dt.year
leap = isleap(year)

data = [0,0,0,0,0,0,0]
data = [0, 0, 0, 0, 0, 0, 0]
data[0] = tobcd(sec) | STBIT
data[1] = tobcd(min)
data[2] = tobcd(hour)
Expand All @@ -39,6 +40,7 @@ def writertc(i2cbus, dt):

i2cbus.write_i2c_block_data(RTCADR, 0, data)


def writealm(i2cbus, alm, dt):
if alm > 0:
alm = 1
Expand All @@ -48,13 +50,13 @@ def writealm(i2cbus, alm, dt):
min = dt.minute
hour = dt.hour
# rtc-ds1307 uses weekday convention Sun = 1, Sat = 7
# wkday in alarm has to match the wkday of rtc time for the alarm to trigger
wkday = (dt.weekday() + 1)%7 + 1
# wkday in alarm has to match the wkday of rtc time
# for the alarm to trigger
wkday = (dt.weekday() + 1) % 7 + 1
day = dt.day
month = dt.month
year = dt.year

data = [0,0,0,0,0,0]

data = [0, 0, 0, 0, 0, 0]
data[0] = tobcd(sec)
data[1] = tobcd(min)
data[2] = tobcd(hour)
Expand All @@ -64,71 +66,81 @@ def writealm(i2cbus, alm, dt):

i2cbus.write_i2c_block_data(RTCADR, almbase[alm], data)


def readrtc(i2cbus):
data=i2cbus.read_i2c_block_data(RTCADR, 0, 7)
data = i2cbus.read_i2c_block_data(RTCADR, 0, 7)

sec = (data[0] & 0x7f) // 16 * 10 + (data[0] & 0x0f)
min = data[1] // 16 * 10 + (data[1] & 0x0f)
hour = data[2] // 16 * 10 + (data[2] & 0x0f)
day = data[4] // 16 * 10 + (data[4] & 0x0f)
sec = (data[0] & 0x7f) // 16 * 10 + (data[0] & 0x0f)
min = data[1] // 16 * 10 + (data[1] & 0x0f)
hour = data[2] // 16 * 10 + (data[2] & 0x0f)
day = data[4] // 16 * 10 + (data[4] & 0x0f)
month = (data[5] & 0x10) // 16 * 10 + (data[5] & 0x0f)
year = data[6] // 16 * 10 + (data[6] & 0x0f)
year = data[6] // 16 * 10 + (data[6] & 0x0f)
dt = datetime(2000+year, month, day, hour, min, sec)
return dt


def readalm(i2cbus, alm):
if alm > 0:
alm = 1
else:
alm = 0
data = i2cbus.read_i2c_block_data(RTCADR, almbase[alm], 6)

sec = data[0] // 16 * 10 + (data[0] & 0x0f)
min = data[1] // 16 * 10 + (data[1] & 0x0f)
hour = data[2] // 16 * 10 + (data[2] & 0x0f)
day = data[4] // 16 * 10 + (data[4] & 0x0f)
sec = data[0] // 16 * 10 + (data[0] & 0x0f)
min = data[1] // 16 * 10 + (data[1] & 0x0f)
hour = data[2] // 16 * 10 + (data[2] & 0x0f)
day = data[4] // 16 * 10 + (data[4] & 0x0f)
month = data[5] // 16 * 10 + (data[5] & 0x0f)
# year not used in alarm time
dt = datetime(2000, month, day, hour, min, sec)
return dt


def enablealm0(i2cbus):
data = i2cbus.read_byte_data(RTCADR, 7)
data |= 0x10
i2cbus.write_byte_data(RTCADR, 7, data)


def enablealm1(i2cbus):
data = i2cbus.read_byte_data(RTCADR, 7)
data |= 0x20
i2cbus.write_byte_data(RTCADR, 7, data)


def disablealm0(i2cbus):
# When disabling the alarm, keep the mfp output high (otherwise we'll get an immediate reboot)
# When disabling the alarm, keep the mfp output high
# (otherwise we'll get an immediate reboot)
data = i2cbus.read_byte_data(RTCADR, 7)
data &= 0xef
data |= 0x80
i2cbus.write_byte_data(RTCADR, 7, data)


def disablealm1(i2cbus):
# When disabling the alarm, keep the mfp output high (otherwise we'll get an immediate reboot)
# When disabling the alarm, keep the mfp output high
# (otherwise we'll get an immediate reboot)
data = i2cbus.read_byte_data(RTCADR, 7)
data &= 0xdf
data |= 0x80
i2cbus.write_byte_data(RTCADR, 7, data)


def enablesqw(i2cbus):
# Set 1 Hz square wave output
i2cbus.write_byte_data(RTCADR, 7, 0x40)


def disablesqw(i2cbus):
# Disable square wave and set ouput high
i2cbus.write_byte_data(RTCADR, 7, 0x80)


def mfpoutput(i2cbus, val):
# set MFP output directly
if val == 0:
data = 0x00
else:
data = 0x80
i2cbus.write_byte_data(RTCADR, 7, data)

31 changes: 18 additions & 13 deletions RTC-Hat-Examples/RTCreboot/pwrite_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from smbusf import SMBus
import os

WHITE = 1
Expand All @@ -13,28 +12,32 @@

lock = False


def main():
papirus = Papirus()
i2cbus=SMBus(1)

write_text(papirus, 'Line 1', save=True)
write_text(papirus, 'Line 2', y=20, load=True, ldfile='save.bmp')

def write_text(papirus, text, x=0, y=0, size=20, load=False, ldfile=' ', save=False, file='save.bmp'):

def write_text(papirus, text, x=0, y=0,
size=20, load=False, ldfile=' ',
save=False, file='save.bmp'):
global image, draw, font

if os.path.isfile(ldfile):
image = Image.open(ldfile)
image.load()
os.remove(ldfile)
if load and os.path.isfile(ldfile):
image = Image.open(ldfile)
image.load()
os.remove(ldfile)
else:
# set all white background
image = Image.new('1', papirus.size, WHITE)
# set all white background
image = Image.new('1', papirus.size, WHITE)

# prepare for drawing
draw = ImageDraw.Draw(image)

font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', size)
font = ImageFont.truetype('/usr/share/fonts/truetype'
'/freefont/FreeMonoBold.ttf', size)

# Calculate the max number of char to fit on line
line_size = ((papirus.width - x) / (size*0.65))
Expand All @@ -54,15 +57,16 @@ def write_text(papirus, text, x=0, y=0, size=20, load=False, ldfile=' ', save=Fa
text_lines[current_line] += " " + word

current_line = 0
for l in text_lines:
draw.text( (x, (size*current_line + y)), l, font=font, fill=BLACK)
for line in text_lines:
draw.text((x, (size*current_line + y)), line, font=font, fill=BLACK)
current_line += 1

papirus.display(image)
papirus.partial_update()
if (save):
image.save(file)


def replace_line(papirus, x, y, text, size=20):
global image, draw, font, lock

Expand All @@ -73,11 +77,12 @@ def replace_line(papirus, x, y, text, size=20):
draw.text((x, y), text, font=font, fill=BLACK)
lock = False


def update_lines(papirus):
global image
papirus.display(image)
papirus.partial_update()


if __name__ == '__main__':
main()

22 changes: 12 additions & 10 deletions RTC-Hat-Examples/py-smbusf/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from distutils.core import setup, Extension

setup( name="smbusf",
version="1.1",
description="Python bindings for Linux SMBus access through i2c-dev, forced access version",
author="Mark M. Hoffman",
author_email="mhoffman@lightlink.com",
maintainer="Mark M. Hoffman",
maintainer_email="linux-i2c@vger.kernel.org",
license="GPLv2",
url="http://lm-sensors.org/",
ext_modules=[Extension("smbusf", ["smbusmodule.c"])])
setup(
name="smbusf",
version="1.1",
description=("Python bindings for Linux SMBus access through i2c-dev, "
"forced access version"),
author="Mark M. Hoffman",
author_email="mhoffman@lightlink.com",
maintainer="Mark M. Hoffman",
maintainer_email="linux-i2c@vger.kernel.org",
license="GPLv2",
url="http://lm-sensors.org/",
ext_modules=[Extension("smbusf", ["smbusmodule.c"])])
1 change: 0 additions & 1 deletion papirus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
from papirus.textpos import PapirusTextPos
from papirus.composite import PapirusComposite
from papirus.readrtc import get_hwclock

33 changes: 16 additions & 17 deletions papirus/composite.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import os
import sys
import uuid

from PIL import Image
from PIL import ImageOps
from papirus import Papirus
from papirus import PapirusTextPos
import uuid

WHITE = 1
BLACK = 0


# Class for holding the details of the img
class DispImg(object):
def __init__(self, image, x, y, size):
Expand All @@ -20,14 +18,15 @@ def __init__(self, image, x, y, size):
self.endx = 0
self.endy = 0


class PapirusComposite(PapirusTextPos):

def __init__(self, autoUpdate=True, rotation=0):
super(PapirusComposite, self).__init__(autoUpdate, rotation)
self.allImg = dict()
self.image = Image.new('1', self.papirus.size, WHITE)

def AddImg(self, image, x=0, y=0, size = (10,10), Id=None):
def AddImg(self, image, x=0, y=0, size=(10, 10), Id=None):
# Create a new Id if none is supplied
if Id is None:
Id = str(uuid.uuid4())
Expand All @@ -42,7 +41,7 @@ def AddImg(self, image, x=0, y=0, size = (10,10), Id=None):
self.allImg[Id] = DispImg(image, x, y, size)
# add the img to the image
self.addToImageImg(Id)
#Automatically show?
# Automatically show?
if self.autoUpdate:
self.WriteAll()

Expand All @@ -56,12 +55,13 @@ def UpdateImg(self, Id, image):
image = image.convert("1", dither=Image.FLOYDSTEINBERG)

self.allImg[Id].image = image

# Remove from the old img from the image (that doesn't use the actual img)

# Remove from the old img from the image
# (that doesn't use the actual img)
self.removeImageImg(Id)
# Add the new img to the image
self.addToImageImg(Id)
#Automatically show?
# Automatically show?
if self.autoUpdate:
self.WriteAll()

Expand All @@ -71,21 +71,20 @@ def RemoveImg(self, Id):
self.removeImageImg(Id)
del self.allImg[Id]

#Automatically show?
# Automatically show?
if self.autoUpdate:
self.WriteAll()

def removeImageImg(self, Id):
# prepare for drawing
filler = Image.new('1', self.allImg[Id].size, WHITE)
# Draw over the top of the img with a rectangle to cover it
x = self.allImg[Id].x
y = self.allImg[Id].y
self.image.paste(filler,(x,y))
x = self.allImg[Id].x
y = self.allImg[Id].y
self.image.paste(filler, (x, y))

def addToImageImg(self, Id):
x = self.allImg[Id].x
y = self.allImg[Id].y

self.image.paste(self.allImg[Id].image,(x,y))
x = self.allImg[Id].x
y = self.allImg[Id].y

self.image.paste(self.allImg[Id].image, (x, y))
Loading