Coding style enforced

This commit is contained in:
2022-05-28 12:46:31 +02:00
parent 4a5c844510
commit 7620db8abb
8 changed files with 197 additions and 125 deletions

View File

@@ -1,29 +1,28 @@
from time import sleep
import os
def DoubleReset(Time=5,File="doublereset.txt"):
"""Use this at the beginning of the file.
DoubleReset(Time=5,File="doublereset.txt")
Determines, if the board had two successive resets in the last <n> seconds.
This is done by writing a file in <File> and deleting it after <n> seconds.
If the file is already there, the reset was doubled."""
try:
a=open(File)
Check=a.read().strip()
a.close()
a=open(File,"w")
a.write(str(int(Check)+1))
a.close()
sleep(Time)
os.remove(File)
return int(Check)+1
except OSError:
pass
a=open(File,"w")
a.write("1")
a.close()
sleep(Time)
os.remove(File)
return False
from time import sleep
import os
def DoubleReset(Time=5, File="doublereset.txt"):
"""Use this at the beginning of the file.
DoubleReset(Time=5,File="doublereset.txt")
Determines, if the board had two successive resets in the last <n> seconds.
This is done by writing a file in <File> and deleting it after <n> seconds.
If the file is already there, the reset was doubled."""
try:
a = open(File)
Check = a.read().strip()
a.close()
a = open(File, "w")
a.write(str(int(Check) + 1))
a.close()
sleep(Time)
os.remove(File)
return int(Check) + 1
except OSError:
pass
a = open(File, "w")
a.write("1")
a.close()
sleep(Time)
os.remove(File)
return False

View File

@@ -1,49 +1,85 @@
from machine import Pin
from time import sleep
class LED:
"""Define a LED on a defined pin"""
def __init__(self,LEDPin=2):
self.LEDPin=Pin(LEDPin,Pin.OUT)
print ("New LED defined on Pin %s"%LEDPin)
def Blink(self,number,timeon=0.2,timeoff=0.2):
def __init__(self, LEDPin=2):
self.LEDPin = Pin(LEDPin, Pin.OUT)
print("New LED defined on Pin %s" % LEDPin)
def Blink(self, number, timeon=0.2, timeoff=0.2):
"""Blinks the LED (number) times, (timeon) and (timeoff) are self explanatory"""
for x in range (number):
for x in range(number):
self.LEDPin.off()
sleep(timeon)
self.LEDPin.on()
sleep(timeoff)
def Pulse(self,seconds,Speed=1):
def Pulse(self, seconds, Speed=1):
"""Helper function for Morse Code"""
self.LEDPin.off()
sleep(seconds)
self.LEDPin.on()
sleep (0.1*Speed)
sleep(0.1 * Speed)
def Morse(self,text,Speed=1):
def Morse(self, text, Speed=1):
"""Blinks (text) in morse code. Speed is around 60 cpm at 1 and proportional"""
Speed=1/Speed
Dot=0.1*Speed
Dash=0.3*Speed
SpaceInLetter=0.1*Speed
SpaceBetweenLetters=0.3*Speed
Space=0.7*Speed
alphabet={' ':' ','a':'.-','b':'-...','c':'-.-.','d':'-..','e':'.','f':'..-.','g':'--.',
'h':'....','i':'..','j':'.---','k':'-.-','l':'.-..','m':'--','n':'-.','o':'---',
'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'-','u':'..-','v':'...-','w':'.--',
'x':'-..-','y':'-.--','z':'--..','1':'.----','2':'..---','3':'...--','4':'....-',
'5':'.....','6':'-....','7':'--...','8':'---..','9':'----.','0':'-----',
'.':'.-.-.-',}
Speed = 1 / Speed
Dot = 0.1 * Speed
Dash = 0.3 * Speed
SpaceInLetter = 0.1 * Speed
SpaceBetweenLetters = 0.3 * Speed
Space = 0.7 * Speed
alphabet = {
" ": " ",
"a": ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
".": ".-.-.-",
}
print ("morsing %s"%text)
print("morsing %s" % text)
for c in text:
for m in alphabet[c.lower()]:
if m == '.':
self.Pulse(Dot,Speed)
if m == '-':
self.Pulse(Dash,Speed)
if m == ' ':
if m == ".":
self.Pulse(Dot, Speed)
if m == "-":
self.Pulse(Dash, Speed)
if m == " ":
sleep(Space)
sleep(SpaceBetweenLetters)

View File

@@ -2,5 +2,3 @@ from aabiot.LED import LED
from aabiot.DoubleReset import DoubleReset
print("AABIOT v0.1 imported")

View File

@@ -7,12 +7,13 @@ weigand.py - read card IDs from a wiegand card reader
from machine import Pin, Timer
import utime
CARD_MASK = 0b11111111111111110 # 16 ones
FACILITY_MASK = 0b1111111100000000000000000 # 8 ones
CARD_MASK = 0b11111111111111110 # 16 ones
FACILITY_MASK = 0b1111111100000000000000000 # 8 ones
# Max pulse interval: 2ms
# pulse width: 50us
class Wiegand:
def __init__(self, pin0, pin1, callback):
"""
@@ -35,8 +36,11 @@ class Wiegand:
self.timer.init(period=50, mode=Timer.PERIODIC, callback=self._cardcheck)
self.cards_read = 0
def _on_pin0(self, newstate): self._on_pin(0, newstate)
def _on_pin1(self, newstate): self._on_pin(1, newstate)
def _on_pin0(self, newstate):
self._on_pin(0, newstate)
def _on_pin1(self, newstate):
self._on_pin(1, newstate)
def _on_pin(self, is_one, newstate):
now = utime.ticks_ms()
@@ -46,22 +50,24 @@ class Wiegand:
self.last_bit_read = now
self.next_card <<= 1
if is_one: self.next_card |= 1
if is_one:
self.next_card |= 1
self._bits += 1
def get_card(self):
if self.last_card is None:
return None
return ( self.last_card & CARD_MASK ) >> 1
return (self.last_card & CARD_MASK) >> 1
def get_facility_code(self):
if self.last_card is None:
return None
# Specific to standard 26bit wiegand
return ( self.last_card & FACILITY_MASK ) >> 17
return (self.last_card & FACILITY_MASK) >> 17
def _cardcheck(self, t):
if self.last_bit_read is None: return
if self.last_bit_read is None:
return
now = utime.ticks_ms()
if now - self.last_bit_read > 50:
# too slow - new start!