Skip to main content

Turn your microbit into a controller

Project description

bitctrl

IMPORTANT:

(IF YOUR 'COM' PORT IS DIFFERENT, YOU WILL HAVE TO CHANGE THAT MANUALLY IN THE MODULE SCRIPT) YOUR MICROBIT WILL HAVE TO HAVE THIS SCRIPT. PASTE IT INTO MAKECODE (OR SOMETHING SIMILAR), TURN IT INTO A .hex FILE - AND TRANSFER IT TO YOUR MICROBIT (THIS IS A PYTHON SCRIPT):

serial.redirect_to_usb()
def on_forever():
    if input.button_is_pressed(Button.AB):
        while input.button_is_pressed(Button.AB):
            serial.write_line("AB")
            pause(500)
    if input.button_is_pressed(Button.A):
        while input.button_is_pressed(Button.A):
            if input.button_is_pressed(Button.B):
                serial.write_line('AB')
                pause(500)
            else:
                serial.write_line("A")
                pause(500)
    if input.button_is_pressed(Button.B):
        while input.button_is_pressed(Button.B):
            if input.button_is_pressed(Button.A):
                serial.write_line('AB')
                pause(500)
            else:
                serial.write_line("B")
                pause(500)
    if input.rotation(Rotation.PITCH):
        serial.write_line(str(input.rotation(Rotation.PITCH))+' PITCH')
        pause(15)
    if input.rotation(Rotation.ROLL):
        serial.write_line(str(input.rotation(Rotation.ROLL))+' ROLL')
        pause(15)
forever(on_forever)

EXAMPLE CODE/HOW TO USE IT:

If you plan on using this in pygame, your milliseconds in "pause" should be half of that number, and then half of that number again; let's say you have 60 fps in pygame, then you will have to divide that by two (which is 30), and then divide 30 by two, which is 15 - hence why I have it pausing for 15 milliseconds

When bitctrl returns something, like 'B', it sends back the string 'B' which you can use for other stuff in your main script(s). Example:

from bitctrl import Bitctrl
while True:
    bitctrl = Bitctrl.bitctrl('a','b','c', False)
    if bitctrl == 'B':
        print("epic") 
    if bitctrl == 'A':
        print("epic1")
    if bitctrl == 'AB':
        print("epic2")

Another example using Bitctrl.bitctrl('a','b','c',False)

from bitctrl import Bitctrl
while True:
    bitctrl = Bitctrl.bitctrl('a','s','d',False)
    if str(bitctrl) != 'None':
        print(bitctrl)

Explanation

When you pass "False" through Bitctrl.bitctrl('a','s','d', False) You tell the module that you do not want to press any keys on your keyboard. So that means that the module only returns the string of the button you pressed on your microbit.

If you were to write Bitctrl.bitctrl('a','s','d'), the module will press the keys you passed through the parameters. So that means it will return the button you pressed on your microbit, but also press the key resembling the button on your microbit.

Hotkeys with your keyboard

Since my module is using another module called "keyboard", you can pass through hotkeys like 'ctrl+shift+tab' which will press ctrl+shift+tab - you can use that to shift through tabs on your browser. To make hotkeys efficient, you will have to change the paused milliseconds on your microbit .hex file - maybe something like pause(100), this will make it so it doesn't shift tabs like crazy.

HOW TO USE _bit_tilt()

_bit_tilt() checks and returns a value depending on how you tilt your microbit. If you tilt your microbit to the left, the module will detect that, and return a value, which you can use later!

EXAMPLE CODE:

from bitctrl import Bitctrl
import keyboard
while True:
    bit_tilt = Bitctrl._bit_tilt('UP','DOWN','RIGHT','LEFT')
    if bit_tilt == 'UP':
        keyboard.press('up')
    if bit_tilt == 'DOWN':
        keyboard.press('down')      
    if bit_tilt == 'RIGHT':
        keyboard.press('d')
    if bit_tilt == 'LEFT':
        keyboard.press('a') 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitctrl-0.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

bitctrl-0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file bitctrl-0.2.tar.gz.

File metadata

  • Download URL: bitctrl-0.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for bitctrl-0.2.tar.gz
Algorithm Hash digest
SHA256 2cd16b0bba24e90062573744c477f687d5f8b99b8e8ccb4bc09fc36b4f4db8fd
MD5 2b40eba56264a24d000b4840fec9b10a
BLAKE2b-256 a91daa31883db5412e90771e3fa95d6688fa7a433233ac825100c18ced4bf245

See more details on using hashes here.

File details

Details for the file bitctrl-0.2-py3-none-any.whl.

File metadata

  • Download URL: bitctrl-0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for bitctrl-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f072e829aaba75cc8d6e8dc33f6ef33b2eaa7b803d3944aa649cdb2f4e3074bf
MD5 ff673f0cbfa386115ff7b8afac011421
BLAKE2b-256 7fc6937aae74e4cfd4ad6797dd8f9f2394ce6160de8df32da9a3dd1c480274fb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page