Device | Port Pin | Pin Sequence No |
---|---|---|
RLED |
P2_6 |
GPIO70 |
BLED |
P2_0 |
GPIO64 |
GLED |
P2_1 |
GPIO65 |
KEY1 |
P2_5 |
GPIO69 |
KEY2 |
P2_3 |
GPIO67 |
/sys/class/gpio/ | -- gpio<no> | -- value ` -- direction
$ echo 65 > /sys/class/gpio/export
$ echo 65 > /sys/class/gpio/unexport
$ echo out > /sys/class/gpio/gpio65/direction
$ echo in > /sys/class/gpio/gpio65/direction
$ echo 1 > /sys/class/gpio/gpio65/value
$ cat /sys/class/gpio/gpio65/value
LED = "64" export_led = open("/sys/class/gpio/export", "w") export_led.write(LED) export_led.close() led_direction = open("/sys/class/gpio/gpio"+LED+"/direction", "w") led_direction.write("out") led_direction.close() led_value = open("/sys/class/gpio/gpio"+LED+"/value", "r+") led_value.write("0") led_value.close()
from os.path import exists __gpio_path = "/sys/class/gpio/gpio{}" __gpio_export_path = "/sys/class/gpio/export" __gpio_unexport_path = "/sys/class/gpio/unexport" __gpio_direction_path = "/sys/class/gpio/gpio{}/direction" __gpio_value_path = "/sys/class/gpio/gpio{}/value" def cat(file): file = open(file, "r") value = file.read() file.close() return value def echo(value, file): file = open(file, "w") file.write(value) file.close() def gpio_export_pin(pin): if not exists(__gpio_path.format(pin)): echo(str(pin), __gpio_export_path) def gpio_unexport_pin(pin): if exists(__gpio_path.format(pin)): echo(str(pin), __gpio_unexport_path) def gpio_get_direction(pin): return cat(__gpio_direction_path.format(pin)) def gpio_set_direction(pin, direction): echo(direction, __gpio_direction_path.format(pin)) def gpio_get_value(pin): value = cat(__gpio_value_path.format(pin)) return int(value) def gpio_set_value(pin, value): echo(str(value), __gpio_value_path.format(pin))
The GPIO related actions are implemented as a GPIO class and it is used in blink code gpio.py
from time import sleep from gpio import * LED = 64 gpio_export_pin(LED) gpio_set_direction(LED, "out") for count in range(5): gpio_set_value(LED, 1) sleep(1) gpio_set_value(LED, 0) sleep(1) gpio_unexport_pin(LED)
from time import sleep from gpio import * KEY = 67 gpio_export_pin(KEY) gpio_set_direction(KEY, "in") print("Press Key1") while True: key_value = gpio_get_value(KEY) if key_value == 0: print("Key is pressed") break sleep(0.1) gpio_unexport_pin(KEY)
from gpio import * ON = 64 gpio_export_pin(ON) gpio_set_direction(ON, "out") gpio_set_value(ON, 1) gpio_pins = ["70", "69", "67"] for i in gpio_pin: gpio_export_pin(gpio_pins[i]) gpio_set_direction(gpio_pins[i], "out") gpio_set_value(gpio_pins[i], 1)
from gpio import * KEY = 67 gpio_export_pin(KEY) gpio_set_direction(KEY, "in") print("Press Key1") while True: key_state = gpio_get_value(KEY) if key_state == 0: print("Key is pressed\n") break gpio_unexport_pin(KEY)