|
Forum Index : Microcontroller and PC projects : Cute little I2C gamepad
| Author | Message | ||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 8667 |
Found HERE Pretty small and basic, but might be useful. Availability seems reasonable with several places advertising it. There's a 3D printable case HERE Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
| karlelch Guru Joined: 30/10/2014 Location: GermanyPosts: 321 |
I got one. It is really small but has nice buttons (not too clicky) and a nice joystick. Here is code (generated with a little help by Gemini): ' Test program for Adafruit Mini I2C Gamepad with seesaw ' ----------------------------------------------------------------------- Option EXPLICIT Option DEFAULT INTEGER ' ----------------------------------------------------------------------- ' Definitions ' ----------------------------------------------------------------------- Const GPAD_ADDR = &H50 ' I2C Address Const GPAD_SDA_PIN = MM.Info(PinNo GP0) ' SDA pin Const GPAD_SCL_PIN = MM.Info(PinNo GP1) ' SCL pin Const GPAD_I2C_FREQ = 400 ' Seesaw Registers Const GPAD_REG_GPIO = &H01 Const GPAD_REF_ADC = &H09 ' Seesaw GPIO Functions Const GPIO_DIRCLR = &H03 ' Direction Clear (Set to Input) Const GPIO_BULK = &H04 ' Read all GPIO Const GPIO_SET = &H05 ' Set Output High (for pull-ups) Const GPIO_PULLEN = &H0B ' Pull-up Enable ' Seesaw ADC Functions Const ADC_READ = &H07 ' Button Pin Mappings (Bit positions) ' Select=0, B=1, Y=2, A=5, X=6, Start=16 Const PIN_SELECT = 0 Const PIN_B = 1 Const PIN_Y = 2 Const PIN_A = 5 Const PIN_X = 6 Const PIN_START = 16 ' Joystick Pins Const JOY_X_PIN = 14 Const JOY_Y_PIN = 15 ' Gamepad data type Type TGamePad joyX As Integer ' Joystick values joyY As Integer StartBtn As Integer ' Buttons SelBtn As Integer BtnA As Integer BtnB As Integer BtnX As Integer BtnY As Integer End Type ' ----------------------------------------------------------------------- Dim gpData As TGamePad Do gpData = SeesawGamePad() Print @(0,0) "Joystick x=";Str$(gpData.joyX, 4, 0); Print " y=";Str$(gpData.joyY, 4, 0);" "; Print "Buttons: "; If gpData.StartBtn Then Print "START " Else Print "_____ "; If gpData.SelBtn Then Print "SEL "; Else Print "___ "; If gpData.BtnA Then Print "A "; Else Print "_ "; If gpData.BtnB Then Print "B "; Else Print "_ "; If gpData.BtnX Then Print "X "; Else Print "_ "; If gpData.BtnY Then Print "Y "; Else Print "_ "; Pause 50 Loop End ' ======================================================================= ' Gamepad function ' ----------------------------------------------------------------------- Function SeesawGamePad() As TGamePad ' Reads out the connected Adafruit Mini I2C Gamepad with seesaw ' On the first call, it initializes the GamePad Static Integer isFirst = 1, rBuf(3), buttons If isFirst Then ' Configure I2C Print "Initializing I2C ..." SetPin GPAD_SDA_PIN, GPAD_SCL_PIN, I2C I2C OPEN 400, 500 ' Create Pin Mask for all buttons. This tells the controller which ' pins we are configuring. Seesaw expects 4 bytes (32-bit word), ' Big Endian (MSB first), hence mask bytes: 00, 01, 00, 67 Print "Configuring Gamepad ..." ' 1. Set Direction to INPUT (DIRCLR) I2C WRITE GPAD_ADDR, 0, 6, GPAD_REG_GPIO, GPIO_DIRCLR, 0, 1, 0, &H67 Pause 10 ' 2. Enable Pull-up resistors (PULLENSET) I2C WRITE GPAD_ADDR, 0, 6, GPAD_REG_GPIO, GPIO_PULLEN, 0, 1, 0, &H67 Pause 10 ' 3. Set Pull-ups High (GPIO_SET) I2C WRITE GPAD_ADDR, 0, 6, GPAD_REG_GPIO, GPIO_SET, 0, 1, 0, &H67 Pause 10 Print "Done." IsFirst = 0 EndIf ' Read buttons I2C WRITE GPAD_ADDR, 1, 2, GPAD_REG_GPIO, GPIO_BULK Pause 3 I2C READ GPAD_ADDR, 0, 4, rBuf() ' Reconstruct the 32-bit status from 4 bytes (Big Endian) ' Note: Logic is inverted because of Pull-ups (0=Pressed, 1=Released) buttons = (rBuf(0) << 24) Or (rBuf(1) << 16) Or (rBuf(2) << 8) Or rBuf(3) SeesawGamePad.StartBtn = (buttons And (1 << PIN_START)) = 0 SeesawGamePad.SelBtn = (buttons And (1 << PIN_SELECT)) = 0 SeesawGamePad.BtnA = (buttons And (1 << PIN_A)) = 0 SeesawGamePad.BtnB = (buttons And (1 << PIN_B)) = 0 SeesawGamePad.BtnX = (buttons And (1 << PIN_X)) = 0 SeesawGamePad.BtnY = (buttons And (1 << PIN_Y)) = 0 ' Read joystick x I2C WRITE GPAD_ADDR, 0, 2, GPAD_REF_ADC, ADC_READ +JOY_X_PIN Pause 3 I2C READ GPAD_ADDR, 0, 2, rBuf() SeesawGamePad.joyX = 1023 -((rBuf(0) << 8) Or rBuf(1)) ' Read joystick y I2C WRITE GPAD_ADDR, 0, 2, GPAD_REF_ADC, ADC_READ +JOY_Y_PIN Pause 3 I2C READ GPAD_ADDR, 0, 2, rBuf() SeesawGamePad.joyY = 1023 -((rBuf(0) << 8) Or rBuf(1)) End Function ' ----------------------------------------------------------------------- |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |