B4J Question GPIO SPI pi4 python to b4j

paddy12309

Member
Licensed User
Hi Everyone,

I have been trying to get a pi4 to control a custom PCB through SPI, I have example python code from the PCB designer that works however can't seem to create a B4J program that uses the SPI side of things.

python script:
#!/usr/bin/python3
import mcp23s17 # From https://github.com/petrockblog/RPi-MCP23S17
import time
print('Controller HW Test')
# Interface uses SPI bus 0, and CS0
mcp = mcp23s17.MCP23S17(bus=0x00, pin_cs=0x00, device_id=0x00)
mcp.open()
# Port A pins all configured to inputs (here numbered 0-7)
# Port B pins all configured as outputs (here numbered 8-15)
 # Relay 1 is connected to the SOUND terminals, and is activated on port B.0 (aka
8 here)
# Relay 2 is connected to the TEST terminals and is activated on port B.1 (aka 9
here)
# WAKE signal from the watchdog to the Pi is port A.7
# DONE signal from the Pi to the watchdog is port B.7
# (Note schematic error where WAKE/DONE are swapped is fixed by mod on the proto
PCB)
buttons = [
      {'name': 'CA', 'pin': 3, 'state': True},
      {'name': 'M', 'pin': 0, 'state': True},
      {'name': 'B', 'pin': 1, 'state': True},
      {'name': 'O', 'pin': 2, 'state': True},
]
for x in range(0,8):
   mcp.setDirection(x, mcp.DIR_INPUT)
   mcp.setPullupMode(x, mcp.PULLUP_ENABLED)
   mcp.setDirection(x+8, mcp.DIR_OUTPUT)
for i in range(7):
    mcp.digitalWrite(i+8, mcp.LEVEL_HIGH)
    time.sleep(0.1)
    mcp.digitalWrite(i+8, mcp.LEVEL_LOW)
    time.sleep(0.1)
while 1:
   for b in buttons:
      state = mcp.digitalRead(b['pin'])
      if state != b['state']:
         b['state'] = state
         if state:
            print('Button %s released.' % (b['name']))
            if b['name'] == 'B':
               print('\tDeactivating SOUND relay')
               mcp.digitalWrite(8, mcp.LEVEL_LOW)
            elif b['name'] == 'O':
               print('\tDeactivating TEST relay')
               mcp.digitalWrite(9, mcp.LEVEL_LOW)
         else:
            print('Button %s pressed.' % (b['name']))
            if b['name'] == 'CA':
               print('\tKick the watchdog, 100msec pulse HIGH')
               print('\n\t*** Watchdog timout reset ***\n')
               mcp.digitalWrite(15, mcp.LEVEL_HIGH)
               time.sleep(0.1)
               mcp.digitalWrite(15, mcp.LEVEL_LOW)
            elif b['name'] == 'M':
               if mcp.digitalRead(15):
                  print('\tToggle the watchdog DONE line - now LOW')
                  print('\n\t*** Watchdog timout reset ***\n')
                  mcp.digitalWrite(15, mcp.LEVEL_LOW)
else:
 
print('\tToggle the watchdog DONE line - now HIGH')
               mcp.digitalWrite(15, mcp.LEVEL_HIGH)
         elif b['name'] == 'B':
            print('\tActivating SOUND relay')
            mcp.digitalWrite(8, mcp.LEVEL_HIGH)
         elif b['name'] == 'O':
            print('\tActivating TEST relay')
            mcp.digitalWrite(9, mcp.LEVEL_HIGH)
uptime = int(time.monotonic())
if (uptime % 15 == 0):
   if flag_uptime:
      print('Uptime %im %is' % (int(uptime / 60), uptime % 60))
      flag_uptime = False
else:
   flag_uptime = True



any help translating this to a B4J non ui program would be greatly appreciated!
 

MichalK73

Well-Known Member
Licensed User
Longtime User
It's not that easy to rewrite to B4X from python. In addition, there is an import of other libraries that would also need to be rewritten.
What can you do?
1. You can look for a python to java transcompiler. Then such a code is easier to put in the B4X code.
2. Use jShell to call python subroutine.

Nobody on the forum will sit for many hours to rewrite the code and match it to B4X. There is simply nothing to it. If you would like to have the full B4X code, you'd rather have to pay for your time.
 
Upvote 0
Top