Created
March 22, 2020 17:46
-
-
Save owencsmith/1c2f62cc8c3af54050d81645cbb33d69 to your computer and use it in GitHub Desktop.
A python file that defines the class used to communicate to an Arduino from a Raspberry Pi over I2C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smbus | |
import time | |
class Communication: | |
def __init__(self, address=0x00): | |
# This is the address we setup in the Arduino Program | |
self._address = address | |
# for RPI version 1, use “bus = smbus.SMBus(0)” | |
self._bus = smbus.SMBus(1) | |
print("Communication Class initialized") | |
# This is the address we setup in the Arduino Program | |
def writeNumber(self, value): | |
self._bus.write_byte(self._address, value) | |
# bus.write_byte_data(address, 0, value) | |
print("Value sent") | |
return -1 | |
def readNumber(self): | |
number = self._bus.read_byte(self._address) | |
# number = bus.read_byte_data(address, 1) | |
return number | |
if __name__ == "__main__": | |
ac = Communication() | |
while True: | |
var = input("Enter 1 – 9: ") | |
if not var: | |
continue | |
ac.writeNumber(var) | |
print(f"RasPI: Hi Arduino, I sent you {0}\n", var) | |
# sleep one second | |
time.sleep(1) | |
number = ac.readNumber() | |
print(f"Arduino: Hey RasPI, I received a digit {0}\n", number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment