robedney
Posts: 2
Joined: Tue Jun 15, 2021 9:02 pm

keyboard input to Pi - reversing normally off to on?

Fri Jun 18, 2021 2:37 am

I'm new to this all but learning fast. I've setting up a conventional manual typewriter with sensors to feed key strokes to a Pi. I need to know if something is possible in software. My keys, unlike a normal keyboard, will all be in the "on" state (due to the mechanics of it all). In other words, pressing a key will send an off signal instead of the normal on. Can this be managed in Python, or do I need to look for a hardware solution? Any hints much appreciated!

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: keyboard input to Pi - reversing normally off to on?

Fri Jun 18, 2021 4:27 am

So you want to convert an "on" to an "off" and vice versa. Done in electronics typically one might have a high voltage level representing "on" and a low voltage level representing "off". Where the voltages could be 0 and 5volts or 0 and 3.3 volts. Or whatever depending on your implementation. The act of converting is then called "inversion" and can be done with an "inverter" circuit or logical "NOT" gate:
https://www.petervis.com/Education/logi ... erter.html
https://en.wikipedia.org/wiki/Inverter_(logic_gate)
https://www.allaboutcircuits.com/textbo ... and-gates/

To avoid all that messing around with hardware circuitry one could just do this inversion after the signal is read into your software. Use the logical "NOT" operator in the programming language of your choice. In Python that is "~". See https://wiki.python.org/moin/BitwiseOperators.
Memory in C++ is a leaky abstraction .

User avatar
Burngate
Posts: 6462
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: keyboard input to Pi - reversing normally off to on?

Fri Jun 18, 2021 9:18 am

robedney wrote:
Fri Jun 18, 2021 2:37 am
I've setting up a conventional manual typewriter with sensors to feed key strokes to a Pi.
I do hope you post some pictures when you're done!
My keys, unlike a normal keyboard, will all be in the "on" state (due to the mechanics of it all). In other words, pressing a key will send an off signal instead of the normal on.
How are you defining "on" and "off"?

The Pi's GPIOs only know about high or low voltages, close to 3.3v or close to 0v.
So a mechanical switch can be used to connect a GPIO to either of those voltages, handily available on the GPIO header.

Or you can arrange for the switch to connect it to one of them, allowing a resistor to pull it to the other when the switch isn't closed.
It's also convenient that each GPIO can have an internal pull-up or pull-down resistor active, so you don't have to provide your own physical resistor!

If I were thinking about doing this, given that the main frame of the typewriter is probably steel, I would connect the Pi's gnd to the frame, and allow the keys to disconnect the GPIOs from that frame. A pull-up on each GPIO would then give you a 3.3v signal - "high" - when the key's pressed.

Return to “Python”