Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
Board and BCM update
Browse files Browse the repository at this point in the history
- Join OPi PC and PCPLUS
- Update mode BCM
- Prepare to pypi
  • Loading branch information
Jeremie-C committed Jun 3, 2018
1 parent f970491 commit 1ee7587
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 26 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global-include *.md *.txt *.py *.c *.h
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
# OrangePi.GPIO
RPi.GPIO drop-in replacement library for Orange Pi Boards

This is a modified version of **RPi.GPIO** for Orange Pi Boards.

It is based on the original [RPi.GPIO](https://pypi.python.org/pypi/RPi.GPIO).

## Installation

#### With PIP

sudo pip install OrangePi.GPIO

#### Manual

sudo apt-get update
sudo apt-get install python-dev git
git clone https://github.com/Jeremie-C/OrangePi.GPIO
cd /OrangePi.GPIO
sudo python setup.py install

## Supported Boards

* OPi ZERO
* OPi ZERO PLUS
* OPi ZERO PLUS2 H3
* OPi ZERO PLUS2 H5
* OPi R1
* OPi PC & PC PLUS
* OPi ONE
* OPi LITE
* OPi PC2
* OPi PRIME

## Usage

Same as RPi.GPIO but with a new function to choose OrangePi Board.

import OPi.GPIO as GPIO
GPIO.setboard(GPIO.ZEROPLUS)
GPIO.setmode(GPIO.BOARD)
GPIO.output(5, 1)

Many demo is on the example folder
29 changes: 29 additions & 0 deletions example/blink_led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OPi.GPIO as GPIO
from time import sleep # this lets us have a time delay

GPIO.setboard(GPIO.PCPCPLUS) # Orange Pi PC board
GPIO.setmode(GPIO.BOARD) # set up BOARD BCM numbering
GPIO.setup(7, GPIO.OUT) # set BCM7 (pin 26) as an output (LED)

try:
print ("Press CTRL+C to exit")
while True:
GPIO.output(7, 1) # set port/pin value to 1/HIGH/True
sleep(0.1)
GPIO.output(7, 0) # set port/pin value to 0/LOW/False
sleep(0.1)

GPIO.output(7, 1) # set port/pin value to 1/HIGH/True
sleep(0.1)
GPIO.output(7, 0) # set port/pin value to 0/LOW/False
sleep(0.1)

sleep(0.5)

except KeyboardInterrupt:
GPIO.output(7, 0) # set port/pin value to 0/LOW/False
GPIO.cleanup() # Clean GPIO
print ("Bye.")
27 changes: 27 additions & 0 deletions example/mode_soc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OPi.GPIO as GPIO
from time import sleep # this lets us have a time delay

GPIO.setboard(GPIO.ZERO) # Orange Pi Zero board
GPIO.setmode(GPIO.SOC) # set up SOC numbering

sled = GPIO.PA+17 # Status led is on PA17
GPIO.setup(sled, GPIO.OUT) # set PA17 as an output (Status led of board)

try:
while True:
GPIO.output(sled, 1) # set port/pin value to 1/HIGH/True
sleep(0.1)
GPIO.output(sled, 0) # set port/pin value to 0/LOW/False
sleep(0.1)
GPIO.output(sled, 1) # set port/pin value to 1/HIGH/True
sleep(0.1)
GPIO.output(sled, 0) # set port/pin value to 0/LOW/False
sleep(0.5)

except KeyboardInterrupt:
GPIO.output(sled, 0)
GPIO.cleanup() # clean up after yourself
print ("Bye.")
17 changes: 17 additions & 0 deletions example/opi_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OPi.GPIO as GPIO
from time import sleep

GPIO.setboard(GPIO.ZERO)
GPIO.setmode(GPIO.BOARD)

print(GPIO.gpio_function(3))
sleep(0.1)
GPIO.setup(3, GPIO.IN)
sleep(0.1)
print(GPIO.gpio_function(3))
sleep(0.1)
GPIO.cleanup()

25 changes: 25 additions & 0 deletions example/pull_up_down.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OPi.GPIO as GPIO
from time import sleep # this lets us have a time delay

GPIO.setboard(GPIO.ZERO) # Orange Pi Zero board
GPIO.setmode(GPIO.BOARD) # set up BOARD GPIO numbering
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # set pin 15 as input (button)
GPIO.setup(11, GPIO.OUT) # set pin 11 as an output (LED)

try:
while True: # this will carry on until you hit CTRL+C
if GPIO.input(15): # if pin 15 == 1
print "Port 15 is 1/HIGH/True - LED ON"
GPIO.output(11, 1) # set port/pin value to 1/HIGH/True
else:
print "Port 15 is 0/LOW/False - LED OFF"
GPIO.output(11, 0) # set port/pin value to 0/LOW/False
sleep(0.1) # wait 0.1 seconds

finally: # this block will run no matter how the try block exits
print("Finally")
GPIO.output(11, 0)
GPIO.cleanup() # clean up after yourself
25 changes: 25 additions & 0 deletions example/pwm_dutycycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OPi.GPIO as GPIO
from time import sleep

GPIO.setboard(GPIO.ZERO)
GPIO.setmode(GPIO.BOARD) # set up BOARD BCM numbering
GPIO.setup(26, GPIO.OUT) # set pin 26 as an output (LED)

p = GPIO.PWM(26, 10) # new PWM on channel=26 frequency=10Hz
p.start(0)
try:
while 1:
for dc in range(0, 101, 5):
p.ChangeDutyCycle(dc)
sleep(0.1)
for dc in range(100, -1, -5):
p.ChangeDutyCycle(dc)
sleep(0.1)
except KeyboardInterrupt:
pass
p.stop()
GPIO.output(26, 0)
GPIO.cleanup()
15 changes: 15 additions & 0 deletions example/simple_pwm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OPi.GPIO as GPIO
from time import sleep

GPIO.setboard(GPIO.ZERO)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(26, GPIO.OUT)

p = GPIO.PWM(26, 0.5) # channel=26 frequency=0.5Hz
p.start(1)
raw_input('Press return to stop:') # use input for Python 3
p.stop()
GPIO.cleanup()
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@
'Topic :: Home Automation',
'Topic :: System :: Hardware']

setup(name = 'OPi.GPIO',
with open("README.md", "r") as fh:
long_description = fh.read()

setup(name = 'OrangePi.GPIO',
version = '0.6.3',
author = 'Jeremie-C',
description = 'A module to control OrangePi GPIO channels',
long_description=long_description,
long_description_content_type="text/markdown",
license = 'MIT',
keywords = 'OrangePi GPIO',
url = 'https://github.com/Jeremie-C/OrangePi.GPIO',
Expand Down
4 changes: 3 additions & 1 deletion source/boards.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ const char* FUNCTIONS[] = {
"UART2_TX","UART2_RX","UART2_RTS","UART2_CTS", // 27-30
"UART3_TX","UART3_RX","UART3_RTS","UART3_CTS", // 31-34
"S_UART_TX","S_UART_RX", // 35-36
"PWM0","PWM1","S_PWM" // 37-39
"PWM0","PWM1","S_PWM", // 37-39
"IO DISABLED"
};

/* Get Alt Function Name */
Expand Down Expand Up @@ -227,6 +228,7 @@ int gpio_function_name(int gpio, int func, int board)
break;
case 5 : str = 2; break;
case 6 : str = 4; break;
case 7 : str = 40; break;
default : str = 3; break;
}
return str;
Expand Down
2 changes: 1 addition & 1 deletion source/boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ const int pin_to_gpio_pc[41];
const int pin_to_gpio_pc2[41];
const int pin_to_gpio_prime[41];

const char* FUNCTIONS[40];
const char* FUNCTIONS[41];

int gpio_function_name(int gpio, int func, int board);
29 changes: 15 additions & 14 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,29 @@ int is_valid_raw_port(int channel)

int get_gpio_number(int channel, unsigned int *gpio)
{
// check channel number is in range
// check channel number is in range
if ( (gpio_mode == BCM && (channel < 0 || channel > 27))
|| (gpio_mode == BOARD && (channel < 1 || channel > 40)) )
{
PyErr_SetString(PyExc_ValueError, "The channel sent is invalid");
return 4;
}
// Mode BCM
if (gpio_mode == BCM)
{
unsigned int i;
for (i=0; i < 41; i++)
{
if (*(phys_To_BCM + i) == channel) {
channel = i; // Set channel to BOARD PIN
break;
}
}
}
// Mode BOARD
if (gpio_mode == BOARD)
if (gpio_mode == BOARD || gpio_mode == BCM )
{
if (*(*pin_to_gpio + channel) == -1)
if (*(*pin_to_gpio + channel) == -1)
{
PyErr_SetString(PyExc_ValueError, "The channel sent is invalid");
return 5;
Expand All @@ -70,17 +82,6 @@ int get_gpio_number(int channel, unsigned int *gpio)
*gpio = *(*pin_to_gpio + channel); //pin_to_gpio is initialized in py_gpio.c, the last several lines
}
}
// Mode BCM
else if (gpio_mode == BCM)
{
unsigned int i;
for (i=0; i < 41; i++)
{
if (*(phys_To_BCM + i) == channel) {
*gpio = *(*pin_to_gpio + i);
}
}
}
// Mode SOC
else if (gpio_mode == MODE_SOC)
{
Expand Down
3 changes: 1 addition & 2 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ SOFTWARE.
#define ZEROPLUS 2 // H5 => same as 1 but different ALT function
#define ZEROPLUS2H5 2 // H5 => same as 1 but different ALT function
#define ZEROPLUS2H3 3
#define PC 4
#define PCPLUS 4
#define PCPCPLUS 4
#define ONE 4
#define LITE 4
#define PLUS2E 4
Expand Down
7 changes: 3 additions & 4 deletions source/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ void define_constants(PyObject *module)
bzeroh5 = Py_BuildValue("i", ZEROPLUS2H5);
PyModule_AddObject(module, "ZEROPLUS", bzeroh5);
PyModule_AddObject(module, "ZEROPLUS2H5", bzeroh5);

bzeroplus3 = Py_BuildValue("i", ZEROPLUS2H3);
PyModule_AddObject(module, "ZEROPLUS2H3", bzeroplus3);

bpc = Py_BuildValue("i", PC);
PyModule_AddObject(module, "PC", bpc);
PyModule_AddObject(module, "PCPLUS", bpc);
bpc = Py_BuildValue("i", PCPCPLUS);
PyModule_AddObject(module, "PCPCPLUS", bpc);
PyModule_AddObject(module, "PLUS2E", bpc);

bpc2 = Py_BuildValue("i", PC2);
Expand Down
5 changes: 3 additions & 2 deletions source/py_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar

func = gpio_function(gpio);
/* Prevent Break of SPI, TWI, UART,... */
if (func != 0 && func != 1) {
if (func != 0 && func != 1 && func != 7) {
fn = gpio_function_name(gpio, func, board_type);
PyErr_Format(PyExc_ValueError, "This channel is already in use by system as %s.", FUNCTIONS[fn]);
return NULL;
Expand Down Expand Up @@ -319,7 +319,8 @@ static PyObject *py_gpio_function(PyObject *self, PyObject *args)
case 3 :
case 4 :
case 5 :
case 6 : fn = f; break;
case 6 :
case 7 : fn = f; break;
default : fn = ALT_UNKNOWN; break;
}

Expand Down

0 comments on commit 1ee7587

Please sign in to comment.