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

Add logging function and fix various issues #9

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions Logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (c) 2021 Rene Juen
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT

import logging
from logging.handlers import RotatingFileHandler

class Logger:

def __init__(self, name, debug):
self.name = name
self.level = debug
if self.level:
self.loglevelflag = logging.DEBUG
else:
self.loglevelflag = logging.INFO
self._config()

def start(self):
pass

def stop(self):
pass

def info(self, message):
self.logger.info(message)

def error(self, message):
self.logger.error(message)

def warning(self, message):
self.logger.warning(message)

def critical(self, message):
self.logger.critical(message)

def debug(self, message):
self.logger.debug(message)

def _config(self):
self.logger = logging.getLogger(self.name)
filename = '/var/log/nr1ui.log'
hdlr = RotatingFileHandler(filename, mode='a', maxBytes=5*1024*1024,backupCount=2, encoding=None, delay=0)
formatter = logging.Formatter('%(asctime)s [%(name)s] %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
self.logger.addHandler(hdlr)
self.logger.setLevel(self.loglevelflag)
8 changes: 6 additions & 2 deletions modules/display1306.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
from PIL import ImageDraw
from PIL import ImageFont

import Logger as logger

log = logger.Logger(__name__, True)

def show_logo(filename, device):
logoImage = Image.new('1', (device.width, device.height))
img_path = os.path.dirname(os.path.realpath(__file__)) + '/../img/'
try:
logoImage = Image.open(img_path + filename).convert('1') #.resize((device.width, device.height), Image.ANTIALIAS)
except IOError:
print("Cannot open file %s" % filename)
log.error("Cannot open file %s" % filename)
pass
device.display(logoImage)

Expand All @@ -19,7 +23,7 @@ def load_font(filename, font_size):
try:
font = ImageFont.truetype(font_path + filename, font_size)
except IOError:
print('font file not found -> using default font')
log.error('font file not found -> using default font')
font = ImageFont.load_default()
return font

Expand Down
7 changes: 5 additions & 2 deletions modules/display1322.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import Logger as logger

log = logger.Logger(__name__, True)

def show_logo(filename, device):
logoImage = Image.new('RGB', (device.width, device.height))
img_path = os.path.dirname(os.path.realpath(__file__)) + '/../img/'
try:
logoImage = Image.open(img_path + filename).convert('RGB') #.resize((device.width, device.height), Image.ANTIALIAS)
except IOError:
print("Cannot open file %s" % filename)
log.error("Cannot open file %s" % filename)
pass
device.display(logoImage)

Expand All @@ -19,7 +22,7 @@ def load_font(filename, font_size):
try:
font = ImageFont.truetype(font_path + filename, font_size)
except IOError:
print('font file not found -> using default font')
log.error('font file not found -> using default font')
font = ImageFont.load_default()
return font

Expand Down
7 changes: 5 additions & 2 deletions modules/display1351.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import Logger as logger

log = logger.Logger(__name__, True)

def show_logo(filename, device):
logoImage = Image.new('RGB', (device.width, device.height))
img_path = os.path.dirname(os.path.realpath(__file__)) + '/../img/'
try:
logoImage = Image.open(img_path + filename).convert('RGB') #.resize((device.width, device.height), Image.ANTIALIAS)
except IOError:
print("Cannot open file %s" % filename)
log.error("Cannot open file %s" % filename)
pass
device.display(logoImage)

Expand All @@ -19,7 +22,7 @@ def load_font(filename, font_size):
try:
font = ImageFont.truetype(font_path + filename, font_size)
except IOError:
print('font file not found -> using default font')
log.error('font file not found -> using default font')
font = ImageFont.load_default()
return font

Expand Down
11 changes: 7 additions & 4 deletions modules/display7735.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import Logger as logger

log = logger.Logger(__name__, True)

def show_logo(filename, device):
print(device.width)
print(device.height)
log.debug("Device width [%d]" % device.width)
log.debug("Device height [%d]" % device.height)
logoImage = Image.new('RGB', (device.width, device.height))
img_path = os.path.dirname(os.path.realpath(__file__)) + '/../img/'
try:
logoImage = Image.open(img_path + filename).convert('RGB') #.resize((device.width, device.height), Image.ANTIALIAS)
except IOError:
print("Cannot open file %s" % filename)
log.error("Cannot open file %s" % filename)
pass
device.display(logoImage)

Expand All @@ -21,7 +24,7 @@ def load_font(filename, font_size):
try:
font = ImageFont.truetype(font_path + filename, font_size)
except IOError:
print('font file not found -> using default font')
log.error('font file not found -> using default font')
font = ImageFont.load_default()
return font

Expand Down
7 changes: 5 additions & 2 deletions modules/displayBraun.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import Logger as logger

log = logger.Logger(__name__, True)

def show_logo(filename, device):
logoImage = Image.new('RGB', (device.width, device.height))
img_path = os.path.dirname(os.path.realpath(__file__)) + '/../img/'
try:
logoImage = Image.open(img_path + filename).convert('RGB') #.resize((device.width, device.height), Image.ANTIALIAS)
except IOError:
print("Cannot open file %s" % filename)
log.error("Cannot open file %s" % filename)
pass
device.display(logoImage)

Expand All @@ -19,7 +22,7 @@ def load_font(filename, font_size):
try:
font = ImageFont.truetype(font_path + filename, font_size)
except IOError:
print('font file not found -> using default font')
log.error('font file not found -> using default font')
font = ImageFont.load_default()
return font

Expand Down
5 changes: 4 additions & 1 deletion modules/pushbutton.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import Logger as logger

log = logger.Logger(__name__, True)

class PushButton:

Expand All @@ -27,5 +30,5 @@ def callback(self, channel):
measured_time += 0.02
sleep(0.02)
if measured_time >= self.minimum_time and channel == self.pin and self.callbackFunction:
print('PIN: '+str(self.pin)+', time: '+str(measured_time))
log.debug('PIN: [%d] \t time: [%d]' % (self.pin, measured_time))
return self.callbackFunction(measured_time)
14 changes: 11 additions & 3 deletions modules/rotaryencoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import RPi.GPIO as GPIO
import Logger as logger

log = logger.Logger(__name__, True)

class RotaryEncoder:

Expand All @@ -13,15 +16,18 @@ def __init__(self, pinA, pinB, pulses_per_cycle=4):
self.ppc = pulses_per_cycle
self.direction = RotaryEncoder.UNKNOWN
self.prevState = 0b11
self.relposition = 0;
self.relposition = 0

GPIO.setup(self.pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def setCallback(self, callback_function):
self.callbackFunction = callback_function
GPIO.add_event_detect(self.pinA, GPIO.BOTH, callback=self.decodeRotation)
GPIO.add_event_detect(self.pinB, GPIO.BOTH, callback=self.decodeRotation)
try:
GPIO.add_event_detect(self.pinA, GPIO.BOTH, callback=self.decodeRotation)
GPIO.add_event_detect(self.pinB, GPIO.BOTH, callback=self.decodeRotation)
except:
log.error("No encoder detected. Please make shure you have wired it rigth up")

def decodeRotation(self, channel):
self.direction = RotaryEncoder.UNKNOWN
Expand All @@ -37,11 +43,13 @@ def decodeRotation(self, channel):
if self.relposition <= -self.ppc:
self.relposition = 0
self.direction = RotaryEncoder.LEFT
log.debug("Rotaryencoder direction set to [LEFT]")
elif (sm == 0b1110 or sm == 0b0111 or sm == 0b0001 or sm == 0b1000):
self.relposition += 1
if self.relposition >= self.ppc:
self.relposition = 0
self.direction = RotaryEncoder.RIGHT
log.debug("Rotaryencoder direction set to [Right]")

if newState == 0b11: #locking position
self.relposition = 0
Expand Down
Loading