Skip to content

Commit

Permalink
Init Save Code
Browse files Browse the repository at this point in the history
  • Loading branch information
EloiStree committed Nov 16, 2024
1 parent cae4acb commit 4063f07
Show file tree
Hide file tree
Showing 60 changed files with 1,333 additions and 0 deletions.
Empty file added .Trash-1000
Empty file.
Empty file added .Trashes
Empty file.
Empty file added .fseventsd/no_log
Empty file.
Empty file added .metadata_never_index
Empty file.
237 changes: 237 additions & 0 deletions PicoCaderCodeV2.py.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
#import board support libraries, including HID.
import board
import digitalio
import analogio
import usb_hid

#Libraries for the OLED Display
from adafruit_display_text import label
import adafruit_displayio_ssd1306
import terminalio
import displayio
import busio

from time import sleep

#Libraries for communicating as a Keyboard device
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

#library for communicating as a gamepad
from hid_gamepad import Gamepad

from adafruit_hid.mouse import Mouse
mouse = Mouse(usb_hid.devices)

from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.consumer_control import ConsumerControl

mediacontrol = ConsumerControl(usb_hid.devices)

keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)
gp = Gamepad(usb_hid.devices)

#Create a collection of GPIO pins that represent the buttons
#This includes the digital pins for the Directional Pad.
#They can be used as regular buttons if using the analog inputs instead
button_pins = (board.GP0, board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7,board.GP10, board.GP11,
board.GP12, board.GP13, board.GP14, board.GP15, board.GP16, board.GP17)



# Map the buttons to button numbers on the Gamepad.
# gamepad_buttons[i] will send that button number when buttons[i]
# is pushed.
gamepad_buttons = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

#Keyboard Mode Button Definitions
keyboard_buttons = {0 Keycode.UP_ARROW, 1 Keycode.LEFT_ARROW, 2 Keycode.DOWN_ARROW, 3 Keycode.RIGHT_ARROW,
4 Keycode.LEFT_CONTROL, 5 Keycode.SPACE, 6 Keycode.W, 7 Keycode.ENTER, 8 Keycode.LEFT_ALT
, 9 Keycode.ENTER, 10 Keycode.ENTER, 11 Keycode.ENTER, 12 Keycode.ENTER, 13 Keycode.ENTER
, 14 Keycode.ENTER, 15 Keycode.ENTER}

#FPS Mode Button Definitions
fps_buttons = {0 Keycode.W, 1 Keycode.A, 2 Keycode.S, 3 Keycode.D,
4 Keycode.LEFT_CONTROL, 5 Keycode.SPACE, 6 Keycode.LEFT_ALT, 7 Keycode.ENTER,
8 Keycode.ENTER, 9 Keycode.ENTER, 10 Keycode.ENTER, 11 Keycode.ENTER, 12 Keycode.ENTER
, 13 Keycode.ENTER, 14 Keycode.ENTER, 15 Keycode.ENTER}

#List of defind mode names
mode_names = {1 'Gamepad', 2 'Keyboard', 3 'FPS', 4 Mouse, 5 Multimedia}

#Set Default Mode To 1
mode = 1

#Define OLED Parameters
WIDTH = 128
HEIGHT = 64
BORDER = 5

buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]

modeChangeButton = digitalio.DigitalInOut(board.GP22)
modeChangeButton.direction = digitalio.Direction.INPUT
modeChangeButton.pull = digitalio.Pull.UP

#Initialize The Buttons
for button in buttons
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

# Setup for Analog Joystick as X and Y
ax = analogio.AnalogIn(board.GP26)
ay = analogio.AnalogIn(board.GP27)

displayio.release_displays()


# Use for I2C for display
i2c = busio.I2C(board.GP9, board.GP8)
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# Equivalent of Arduino's map() function.
def range_map(x, in_min, in_max, out_min, out_max)
return (x - in_min) (out_max - out_min) (in_max - in_min) + out_min


display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)

# Make the display context
splash = displayio.Group()
display.show(splash)

# Draw a label

text = Mode + str(mode)
text_area = label.Label(
terminalio.FONT, text=text, color=0xFFFFFF, x=3, y=3
)
splash.append(text_area)
text = mode_names[mode]
text_area = label.Label(
terminalio.FONT, text=text, color=0xFFFFFF, x=3, y=23
)
splash.append(text_area)

def debounce()
sleep(0.2)

while True
#Check to see if the mode change button is pressed
if modeChangeButton.value
mode = mode + 1
if mode 5
mode = 1
sleep(1)
splash = displayio.Group()
display.show(splash)
# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(WIDTH - BORDER 2, HEIGHT - BORDER 2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000 # Black
inner_sprite = displayio.TileGrid(
inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
)
splash.append(inner_sprite)
display.show(splash)
# Draw a label
text = Mode + str(mode)
text_area = label.Label(
terminalio.FONT, text=text, color=0xFFFFFF, x=3, y=3
)
splash.append(text_area)
text = mode_names[mode]
text_area = label.Label(
terminalio.FONT, text=text, color=0xFFFFFF, x=3, y=23
)
splash.append(text_area)

if mode == 1
# Check value of up, down, left, and right buttons
# and set the joystick values appropriately.
# can be replaced with values from analog stick instead
setx = 0
sety = 0
#Not keyboard presses for directional
#So check them seperately first
if not buttons[0].value
sety = -127
if not buttons[2].value
sety = 127
if not buttons[1].value
setx = -127
if not buttons[3].value
setx = 127
#Set Joystick movements
gp.move_joysticks(
x=setx,
y=sety,
)

# Go through all the button definitions, and
# press or release as appropriate
for i, button in enumerate(buttons)
if i 3 #Skip the first 4, since they're the directionals
gamepad_button_num = gamepad_buttons[i - 4] # Minus 4 to ignore directionals
if button.value
gp.release_buttons(gamepad_button_num)
else
gp.press_buttons(gamepad_button_num)

if mode == 2 # Keyboard Mode

for i, button in enumerate(buttons)
if button.value
keyboard.release(keyboard_buttons[i])
else
keyboard.press(keyboard_buttons[i])

#FPS Mode
if mode == 3
for i, button in enumerate(buttons)
gamepad_button_num = gamepad_buttons[i]
if button.value
keyboard.release(fps_buttons[i])
else
keyboard.press(fps_buttons[i])

if mode == 4
if not buttons[0].value
mouse.move(y=-4)
if not buttons[2].value
mouse.move(y=4)
if not buttons[1].value
mouse.move(x=-4)
if not buttons[3].value
mouse.move(x=4)
if not buttons[4].value
mouse.click(Mouse.LEFT_BUTTON)
sleep(0.2)
if not buttons[5].value
mouse.click(Mouse.RIGHT_BUTTON)
sleep(0.2)

if mode == 5
if not buttons[0].value
mediacontrol.send(ConsumerControlCode.VOLUME_INCREMENT)
debounce()
if not buttons[2].value
mediacontrol.send(ConsumerControlCode.VOLUME_DECREMENT)
debounce()
if not buttons[1].value
mediacontrol.send(ConsumerControlCode.SCAN_PREVIOUS_TRACK)
debounce()
if not buttons[3].value
mediacontrol.send(ConsumerControlCode.SCAN_NEXT_TRACK)
debounce()
if not buttons[4].value
mediacontrol.send(ConsumerControlCode.PLAY_PAUSE)
debounce()
if not buttons[5].value
mediacontrol.send(ConsumerControlCode.STOP)
debounce()
if not buttons[9].value
mediacontrol.send(ConsumerControlCode.MUTE)
debounce()
62 changes: 62 additions & 0 deletions bluetooth_electronics_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

import random

class BluetoothElectronicsBuilder:




def create_text(self, char: str, charText: str, end_line: bool=True):
"""*charText*"""
if end_line:
return f"*{char}{charText}\n*"
return f"*{char}{charText}*"

def create_light(self, char: str="L", red255:bytes=0, green255:bytes=0, blue255:bytes=0):
"""*charRGB*"""
return f"*{char}R{red255}G{green255}B{blue255}*"

def create_light_random_color(self, char: str):
"""*charRND*"""
return self.create_light(char, random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))


def create_sound(self, char: str="S", sound: bytes=50):
"""*charSOUND*"""
#clamp 0 to 100
sound = max(0, min(100, sound))
return f"*{char}V{sound}*"

def create_gauge(self, char: str="D", value: int=50):
"""*charGAUGE*"""
return f"*{char}{value}*"
def create_temperature(self, char: str="T", value: int=50):
"""*charTEMP*"""
return f"*{char}{value}*"
def create_gauge_blue(self, char: str="D", value: int=50):
"""*charGAUGE*"""
return self.create_gauge(char, value)
def create_gauge_orange(self, char: str="G", value: int=50):
"""*charGAUGE*"""
return self.create_gauge(char, value)

def create_graph_clear(self, char: str="H"):
"""*charCLEAR*"""
return f"*{char}C*"
def create_graph_add(self, char: str="H", x:float = 0.0, y:float = 0.0, xx:float = 0.0, yy:float = 0.0):
"""*charADD*"""
return f"*{char}X{x:.2f}Y{y:.2f}X{xx:.2f}Y{yy:.2f}*"



def create_integer_from_char1(self, c):
return ord(c)
def create_integer_from_char2(self, c_left_right_0, c_left_right_1):
utf8_string = "AAAA"
utf8_bytes = utf8_string.encode('utf-8')
# Interpret the bytes in little-endian order
little_endian_value = int.from_bytes(utf8_bytes, byteorder='little')




72 changes: 72 additions & 0 deletions boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import usb_hid
import usb_midi
import supervisor
import board
import usb_cdc
# Set the USB serial device name
#usb_cdc.console.serial = "LoveFrenchFries"
import board
import usb_cdc
import time
from adafruit_hid import find_device
import usb_hid
import usb_hid

# IMPORTANT !!!!!!!!! IF YOU CHANGE YOU NEED A HARD RESET TO RECOGNIZE THE DEVICE
# TURN OFF THE DEVICE THEN GO IN BOOT MODE THEN COME BACK TO NORMAL MODE
# This is an updated example of a gamepad report descriptor with 32 buttons.
GAMEPAD_REPORT_DESCRIPTOR = bytes((
0x05, 0x01, # Usage Page (Generic Desktop Ctrls)
0x09, 0x05, # Usage (Game Pad)
0xA1, 0x01, # Collection (Application)
0x85, 0x04, # Report ID (4)

# Buttons (32 buttons)
0x05, 0x09, # Usage Page (Button)
0x19, 0x01, # Usage Minimum (Button 1)
0x29, 0x20, # Usage Maximum (Button 32)
0x15, 0x00, # Logical Minimum (0)
0x25, 0x01, # Logical Maximum (1)
0x75, 0x01, # Report Size (1)
0x95, 0x20, # Report Count (32)
0x81, 0x02, # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)

# Axes (X, Y, Z, Rz)
0x05, 0x01, # Usage Page (Generic Desktop Ctrls)
0x15, 0x81, # Logical Minimum (-127)
0x25, 0x7F, # Logical Maximum (127)
0x09, 0x30, # Usage (X)
0x09, 0x31, # Usage (Y)
0x09, 0x32, # Usage (Z)
0x09, 0x35, # Usage (Rz)
0x75, 0x08, # Report Size (8)
0x95, 0x04, # Report Count (4)
0x81, 0x02, # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)

# Triggers (Rx, Ry)
0x09, 0x33, # Usage (Rx) - Left Trigger
0x09, 0x34, # Usage (Ry) - Right Trigger
0x15, 0x00, # Logical Minimum (0)
0x25, 0xFF, # Logical Maximum (255)
0x75, 0x08, # Report Size (8)
0x95, 0x02, # Report Count (2)
0x81, 0x02, # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)

0xC0 # End Collection
))

gamepad = usb_hid.Device(
report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
usage_page=0x01, # Generic Desktop Control
usage=0x05, # Gamepad
report_ids=(4,), # Descriptor uses report ID 4.
in_report_lengths=(10,), # This gamepad sends 10 bytes in its report.
out_report_lengths=(0,), # It does not receive any reports.
)
usb_hid.enable(
(usb_hid.Device.KEYBOARD,
usb_hid.Device.MOUSE,
usb_hid.Device.CONSUMER_CONTROL,
gamepad)
)
usb_midi.enable()
5 changes: 5 additions & 0 deletions boot_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Adafruit CircuitPython 9.2.0 on 2024-10-28; Raspberry Pi Pico W with rp2040
Board ID:raspberry_pi_pico_w
UID:E661A4D4173ABD2E
MAC:28:CD:C1:0D:5F:7F
boot.py output:
Loading

0 comments on commit 4063f07

Please sign in to comment.