├── .gitignore ├── .idea └── .gitignore ├── Matrix.GPIO ├── .gitignore ├── Matrix │ ├── .gitignore │ └── __init__.py ├── common.c ├── common.h ├── constants.c ├── constants.h ├── gpio.c ├── libfahw-gpio.h ├── setup.py └── test │ ├── matrix_input.py │ ├── matrix_led.py │ └── matrix_output.py ├── Matrix.I2C ├── .gitignore ├── I2C.c ├── Matrix │ ├── .gitignore │ └── __init__.py ├── setup.py └── test │ └── matrix_compass.py ├── Matrix.pcf8591 ├── .gitignore ├── Matrix │ ├── .gitignore │ └── __init__.py ├── common.c ├── common.h ├── libfahw-pcf8591.h ├── pcf8591.c ├── setup.py └── test │ └── matrix_adc.py ├── matrix-3_axis_digital_accelerometer └── matrix_accelerometer.py ├── matrix-analog_to_digital_converter └── matrix_adc.py └── modules ├── .gitignore ├── adxl34x-i2c.ko ├── adxl34x.ko ├── bmp085.ko ├── dht11.ko ├── fbtft_device.ko ├── matrix_ads7846.ko ├── matrix_gpio_int.ko ├── matrix_hcsr04.ko ├── matrix_ir_recv.ko ├── matrix_pwm.ko ├── matrix_rotary_encoder.ko ├── pcf8591.ko ├── rtc-ds1307.ko ├── w1-gpio-board.ko └── w1-gpio.ko /.gitignore: -------------------------------------------------------------------------------- 1 | .tags* 2 | *.cproject 3 | *.project 4 | /.pydevproject 5 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | /.name 2 | -------------------------------------------------------------------------------- /Matrix.GPIO/.gitignore: -------------------------------------------------------------------------------- 1 | .tags* 2 | /build 3 | -------------------------------------------------------------------------------- /Matrix.GPIO/Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /Matrix.GPIO/Matrix/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/Matrix.GPIO/Matrix/__init__.py -------------------------------------------------------------------------------- /Matrix.GPIO/common.c: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "common.h" 3 | 4 | int writeValueToFile(char* fileName, char* buff) { 5 | FILE *fp = fopen(fileName,"w"); 6 | if (fp == NULL) { 7 | PyErr_SetString(PyExc_RuntimeError, "Unable to open file"); 8 | return -1; 9 | } else { 10 | fwrite ( buff, strlen(buff), 1, fp ); 11 | } 12 | fclose(fp); 13 | return 0; 14 | } 15 | 16 | int writeIntValueToFile(char* fileName, int value) { 17 | char buff[50]; 18 | sprintf(buff, "%d", value); 19 | return writeValueToFile(fileName, buff); 20 | } 21 | 22 | int readValueFromFile(char* fileName, char* buff, int len) { 23 | int ret = -1; 24 | FILE *fp = fopen(fileName,"r"); 25 | if (fp == NULL) { 26 | PyErr_SetString(PyExc_RuntimeError, "Unable to open file"); 27 | return -1; 28 | } else { 29 | if (fread(buff, sizeof(char), len, fp)>0) { 30 | ret = 0; 31 | } 32 | } 33 | fclose(fp); 34 | return ret; 35 | } 36 | 37 | int readIntValueFromFile(char* fileName) { 38 | char buff[255]; 39 | memset(buff, 0, sizeof(buff)); 40 | int ret = readValueFromFile(fileName, buff, sizeof(buff)-1); 41 | if (ret == 0) { 42 | return atoi(buff); 43 | } 44 | return ret; 45 | } -------------------------------------------------------------------------------- /Matrix.GPIO/common.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMMON_H_ 2 | #define _COMMON_H_ 3 | 4 | #define __DEBUG 5 | 6 | #ifdef __DEBUG 7 | #define DEBUG(format, args...) \ 8 | printf("FAHW-Lib: " format, ## args) 9 | #else 10 | #define DEBUG(format, args...) 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | extern int writeValueToFile(char* fileName, char* buff); 27 | extern int writeIntValueToFile(char* fileName, int value); 28 | extern int readValueFromFile(char* fileName, char* buff, int len); 29 | extern int readIntValueFromFile(char* fileName); 30 | 31 | #define FILE_PATH_LENGTH (128) 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /Matrix.GPIO/constants.c: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "libfahw-gpio.h" 3 | #include "constants.h" 4 | 5 | void define_constants(PyObject *module) 6 | { 7 | high = Py_BuildValue("i", HIGH); 8 | PyModule_AddObject(module, "HIGH", high); 9 | 10 | low = Py_BuildValue("i", LOW); 11 | PyModule_AddObject(module, "LOW", low); 12 | 13 | output = Py_BuildValue("i", OUTPUT); 14 | PyModule_AddObject(module, "OUT", output); 15 | 16 | input = Py_BuildValue("i", INPUT); 17 | PyModule_AddObject(module, "IN", input); 18 | 19 | board = Py_BuildValue("i", BOARD); 20 | PyModule_AddObject(module, "BOARD", board); 21 | 22 | kernel = Py_BuildValue("i", KERNEL); 23 | PyModule_AddObject(module, "KERNEL", kernel); 24 | 25 | } -------------------------------------------------------------------------------- /Matrix.GPIO/constants.h: -------------------------------------------------------------------------------- 1 | PyObject *high; 2 | PyObject *low; 3 | PyObject *input; 4 | PyObject *output; 5 | PyObject *board; 6 | PyObject *kernel; 7 | 8 | void define_constants(PyObject *module); -------------------------------------------------------------------------------- /Matrix.GPIO/gpio.c: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "common.h" 3 | #include "constants.h" 4 | #include "libfahw-gpio.h" 5 | 6 | static int gpio_mode = MODE_UNKNOWN; 7 | static int pinGPIO[GPIO_MAX_NUM+1] = {-1, -1, -1, 99, -1, 98, -1, 60, 117, -1, 113, 8 | 61, 58, 62, -1, 63, 78, -1, 59, 95, -1, 9 | 96, 97, 93, 94, -1, 77, 103, 102, 72, -1, 10 | 73, 92, 74, -1, 76, 71, 75, 162, -1, 163, 11 | }; 12 | static int is_exported[GPIO_MAX_NUM+1]; 13 | 14 | static int chantoGPIO(int channel) 15 | { 16 | int i; 17 | int gpio; 18 | 19 | if (gpio_mode == BOARD) { 20 | if (channel < GPIO_MIN_NUM || channel > GPIO_MAX_NUM) 21 | return -1; 22 | gpio = pinGPIO[channel]; 23 | } else if (gpio_mode == KERNEL) { 24 | gpio = channel; 25 | for (i=GPIO_MIN_NUM; i<=GPIO_MAX_NUM; i++) { 26 | if (gpio == pinGPIO[i]) 27 | break; 28 | } 29 | if (i > GPIO_MAX_NUM || gpio == -1) { 30 | return -1; 31 | } 32 | } else { 33 | return -1; 34 | } 35 | return gpio; 36 | } 37 | 38 | static int exportGPIOPin(int gpio) 39 | { 40 | return writeIntValueToFile("/sys/class/gpio/export", gpio); 41 | } 42 | 43 | static int unexportGPIOPin(int gpio) 44 | { 45 | return writeIntValueToFile("/sys/class/gpio/unexport", gpio); 46 | } 47 | 48 | static int getGPIOValue(int gpio) 49 | { 50 | char fileName[255] = {0}; 51 | sprintf(fileName, "/sys/class/gpio/gpio%d/%s", gpio, "value"); 52 | return readIntValueFromFile(fileName); 53 | } 54 | 55 | static int setGPIOValue(int gpio, int value) 56 | { 57 | char fileName[255] = {0}; 58 | sprintf(fileName, "/sys/class/gpio/gpio%d/%s", gpio, "value"); 59 | return writeIntValueToFile(fileName, value); 60 | } 61 | 62 | static int setGPIODirection(int gpio, int direction) 63 | { 64 | char directionStr[10]; 65 | char fileName[255] = {0}; 66 | 67 | sprintf(fileName, "/sys/class/gpio/gpio%d/%s", gpio, "direction"); 68 | if (direction == INPUT) { 69 | strcpy(directionStr, "in"); 70 | } else if (direction == OUTPUT) { 71 | strcpy(directionStr, "out"); 72 | } else { 73 | PyErr_SetString(PyExc_ValueError, "Direction must be INPUT or OUTPUT"); 74 | return -1; 75 | } 76 | return writeValueToFile(fileName, directionStr); 77 | } 78 | 79 | #if 0 80 | static int getGPIODirection(int gpio) 81 | { 82 | char buf[255] = {0}; 83 | int direction; 84 | int ret; 85 | char fileName[255] = {0}; 86 | 87 | sprintf(fileName, "/sys/class/gpio/gpio%d/%s", gpio, "direction"); 88 | ret = readValueFromFile(fileName, buf, sizeof(buf)-1); 89 | if (ret >= 0) { 90 | if (strncasecmp(buf, "out", 3)==0) { 91 | direction = OUTPUT; 92 | } else if (strncasecmp(buf, "in", 2)==0) { 93 | direction = INPUT; 94 | } else { 95 | PyErr_SetString(PyExc_ValueError, "Direction must be INPUT or OUTPUT"); 96 | return -1; 97 | } 98 | return direction; 99 | } 100 | return ret; 101 | } 102 | #endif 103 | 104 | static PyObject *py_gpio_setmode(PyObject *self, PyObject *args) 105 | { 106 | int new_mode; 107 | 108 | if (!PyArg_ParseTuple(args, "i", &new_mode)) 109 | return NULL; 110 | gpio_mode = new_mode; 111 | if (gpio_mode != KERNEL && gpio_mode != BOARD) { 112 | PyErr_SetString(PyExc_ValueError, "Pin mode must be KERNEL or BOARD"); 113 | return NULL; 114 | } 115 | Py_RETURN_NONE; 116 | } 117 | 118 | static PyObject *py_gpio_getmode(PyObject *self, PyObject *args) 119 | { 120 | PyObject *mode; 121 | mode = Py_BuildValue("i", gpio_mode); 122 | return mode; 123 | } 124 | 125 | static PyObject *py_gpio_setup(PyObject *self, PyObject *args) 126 | { 127 | int channel = -1; 128 | int gpio = -1; 129 | int direction = -1; 130 | int i; 131 | 132 | if (!PyArg_ParseTuple(args, "ii", &channel, &direction)) { 133 | return NULL; 134 | } 135 | if (direction != INPUT && direction != OUTPUT) { 136 | PyErr_SetString(PyExc_ValueError, "Pin direction must be IN or OUT"); 137 | return NULL; 138 | } 139 | if ((gpio = chantoGPIO(channel)) < 0) { 140 | PyErr_SetString(PyExc_RuntimeError, "Invalid Pin, it may be 5V 3.3V GND or occupied by kernel"); 141 | return NULL; 142 | } 143 | if (exportGPIOPin(gpio) < 0) { 144 | PyErr_SetString(PyExc_RuntimeError, "Fail to export gpio"); 145 | return NULL; 146 | } 147 | for (i=GPIO_MIN_NUM; i<=GPIO_MAX_NUM; i++) { 148 | if (gpio == pinGPIO[i]) { 149 | is_exported[i] = 1; 150 | break; 151 | } 152 | } 153 | if (setGPIODirection(gpio, direction) < 0) { 154 | PyErr_SetString(PyExc_RuntimeError, "Fail to set gpio direction"); 155 | return NULL; 156 | } 157 | Py_RETURN_NONE; 158 | } 159 | 160 | static PyObject *py_gpio_cleanup(PyObject *self, PyObject *args) 161 | { 162 | int i; 163 | for (i=GPIO_MIN_NUM; i<=GPIO_MAX_NUM; i++) { 164 | if (is_exported[i] == 1) { 165 | unexportGPIOPin(pinGPIO[i]); 166 | is_exported[i] = 0; 167 | } 168 | } 169 | Py_RETURN_NONE; 170 | } 171 | static PyObject *py_gpio_output(PyObject *self, PyObject *args) 172 | { 173 | int channel = -1; 174 | int gpio = -1; 175 | int value = -1; 176 | 177 | if (!PyArg_ParseTuple(args, "ii", &channel, &value)) 178 | return NULL; 179 | if ((gpio = chantoGPIO(channel)) < 0) { 180 | PyErr_SetString(PyExc_RuntimeError, "Invalid Pin, it may be 5V 3.3V GND or occupied by kernel"); 181 | return NULL; 182 | } 183 | if (setGPIOValue(gpio, value) < 0) { 184 | PyErr_SetString(PyExc_RuntimeError, "Fail to set gpio value"); 185 | return NULL; 186 | } 187 | 188 | Py_RETURN_NONE; 189 | } 190 | 191 | static PyObject *py_gpio_input(PyObject *self, PyObject *args) 192 | { 193 | int channel = -1; 194 | int gpio = -1; 195 | PyObject *value = NULL; 196 | 197 | if (!PyArg_ParseTuple(args, "i", &channel)) 198 | return NULL; 199 | if ((gpio = chantoGPIO(channel)) < 0) { 200 | PyErr_SetString(PyExc_RuntimeError, "Invalid Pin, it may be 5V 3.3V GND or occupied by kernel"); 201 | return NULL; 202 | } 203 | switch (getGPIOValue(gpio)) { 204 | case 0: 205 | value = Py_BuildValue("i", LOW); 206 | break; 207 | case 1: 208 | value = Py_BuildValue("i", HIGH); 209 | break; 210 | default: 211 | break; 212 | } 213 | return value; 214 | } 215 | 216 | PyDoc_STRVAR(module_doc, "provides GPIO function for Matrix accessory using Python"); 217 | static PyMethodDef matrix_gpio_methods[] = { 218 | {"setmode", py_gpio_setmode, METH_VARARGS, "Set pin index mode as sys index or board index"}, 219 | {"getmode", py_gpio_getmode, METH_VARARGS, "Get pin index mode"}, 220 | {"setup", py_gpio_setup, METH_VARARGS, "Export gpio and set gpio direction as ouput or input"}, 221 | {"cleanup", py_gpio_cleanup, METH_VARARGS, "Unexport gpio"}, 222 | {"output", py_gpio_output, METH_VARARGS, "Set gpio value as high or low"}, 223 | {"input", py_gpio_input, METH_VARARGS, "Get gpio value"}, 224 | {NULL, NULL, 0, NULL} 225 | }; 226 | 227 | PyMODINIT_FUNC initGPIO(void) 228 | { 229 | PyObject *m; 230 | 231 | if ((m = Py_InitModule3("Matrix.GPIO", matrix_gpio_methods, module_doc)) == NULL) 232 | return; 233 | define_constants(m); 234 | memset(is_exported, 0, sizeof(is_exported)/sizeof(is_exported[0])); 235 | return; 236 | } 237 | -------------------------------------------------------------------------------- /Matrix.GPIO/libfahw-gpio.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_GPIO_H__ 2 | #define __FRIENDLYARM_HARDWARE_GPIO_H__ 3 | 4 | // Direction 5 | #define INPUT (1) 6 | #define OUTPUT (0) 7 | 8 | // Value 9 | #define HIGH (1) 10 | #define LOW (0) 11 | 12 | #define MODE_UNKNOWN (-1) 13 | #define BOARD (10) 14 | #define KERNEL (11) 15 | 16 | #define GPIO_MIN_NUM (1) 17 | #define GPIO_MAX_NUM (40) 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Matrix.GPIO/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | MOD = 'Matrix.GPIO' 4 | setup(name = MOD, 5 | version = '0.0.1', 6 | url = 'http://www.friendlyarm.com', 7 | author = 'FriendlyARM', 8 | author_email = 'support@friendlyarm.com', 9 | description = 'A module to control Matrix related boards GPIO', 10 | keywords = 'matirx gpio', 11 | packages = ['Matrix'], 12 | ext_modules = [Extension(MOD, sources=['gpio.c', 'common.c', 'constants.c'])]) 13 | -------------------------------------------------------------------------------- /Matrix.GPIO/test/matrix_input.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import time 5 | import Matrix.GPIO as GPIO 6 | 7 | def main(): 8 | argc = len(sys.argv) 9 | if argc != 2: 10 | print "usage:%s pin" % sys.argv[0] 11 | return 12 | pin = int(sys.argv[1]) 13 | try: 14 | GPIO.setmode(GPIO.BOARD) 15 | GPIO.setup(pin, GPIO.IN) 16 | print "Get pin%d value %d" % (pin, GPIO.input(pin)) 17 | except Exception, e: 18 | print e 19 | finally: 20 | GPIO.cleanup() 21 | 22 | if __name__ == '__main__': 23 | main() -------------------------------------------------------------------------------- /Matrix.GPIO/test/matrix_led.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import time 4 | import Matrix.GPIO as GPIO 5 | 6 | def main(): 7 | try: 8 | led = 7 9 | GPIO.setmode(GPIO.BOARD) 10 | GPIO.setup(led, GPIO.OUT) 11 | print "Light on" 12 | GPIO.output(led, GPIO.HIGH) 13 | time.sleep(1) 14 | print "Light off" 15 | GPIO.output(led, GPIO.LOW) 16 | except Exception, e: 17 | print e 18 | finally: 19 | GPIO.cleanup() 20 | 21 | if __name__ == '__main__': 22 | main() -------------------------------------------------------------------------------- /Matrix.GPIO/test/matrix_output.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import time 5 | import Matrix.GPIO as GPIO 6 | 7 | def main(): 8 | argc = len(sys.argv) 9 | if argc != 2: 10 | print "usage:%s pin" % sys.argv[0] 11 | return 12 | pin = int(sys.argv[1]) 13 | try: 14 | GPIO.setmode(GPIO.BOARD) 15 | GPIO.setup(pin, GPIO.OUT) 16 | print "Set pin%d HIGH" % pin 17 | GPIO.output(pin, GPIO.HIGH) 18 | time.sleep(1) 19 | print "Set pin%d LOW" % pin 20 | GPIO.output(pin, GPIO.LOW) 21 | except Exception, e: 22 | print e 23 | finally: 24 | GPIO.cleanup() 25 | 26 | if __name__ == '__main__': 27 | main() -------------------------------------------------------------------------------- /Matrix.I2C/.gitignore: -------------------------------------------------------------------------------- 1 | .tags* 2 | /build 3 | -------------------------------------------------------------------------------- /Matrix.I2C/I2C.c: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static PyObject *py_open_channel(PyObject *self, PyObject *args) 11 | { 12 | int fd = -1; 13 | char i2c_dev[32] = {0}; 14 | int index; 15 | 16 | if (!PyArg_ParseTuple(args, "i", &index)) 17 | return NULL; 18 | if (index < 0) { 19 | PyErr_SetString(PyExc_ValueError, "I2C channel must be int type and >=0"); 20 | return NULL; 21 | } 22 | sprintf(i2c_dev, "/dev/i2c-%d", index); 23 | fd = open(i2c_dev, O_RDWR); 24 | if (fd < 0) { 25 | PyErr_SetString(PyExc_RuntimeError, "Fail to open I2C channel"); 26 | return NULL; 27 | } 28 | return Py_BuildValue("i", fd); 29 | } 30 | 31 | static PyObject *py_set_slave(PyObject *self, PyObject *args) 32 | { 33 | int fd = -1; 34 | int slave = -1; 35 | 36 | if (!PyArg_ParseTuple(args, "ii", &fd, &slave)) 37 | return NULL; 38 | if (slave < 0 || slave > 0x7f) { 39 | PyErr_SetString(PyExc_ValueError, "I2C slave address must be 7 bit"); 40 | return NULL; 41 | } 42 | if (ioctl(fd, I2C_SLAVE, slave) < 0) { 43 | PyErr_SetString(PyExc_RuntimeError, "Fail to ioctl I2C_SLAVE"); 44 | return NULL; 45 | } 46 | Py_RETURN_NONE; 47 | } 48 | 49 | static PyObject *py_set_timeout(PyObject *self, PyObject *args) 50 | { 51 | int fd = -1; 52 | int timeout = -1; 53 | 54 | if (!PyArg_ParseTuple(args, "ii", &fd, &timeout)) 55 | return NULL; 56 | if (ioctl(fd, I2C_TIMEOUT, timeout) < 0) { 57 | PyErr_SetString(PyExc_RuntimeError, "Fail TO ioctl I2C_TIMEOUT"); 58 | return NULL; 59 | } 60 | Py_RETURN_NONE; 61 | } 62 | 63 | static PyObject *py_set_retries(PyObject *self, PyObject *args) 64 | { 65 | int fd = -1; 66 | int retries = -1; 67 | 68 | if (!PyArg_ParseTuple(args, "ii", &fd, &retries)) 69 | return NULL; 70 | if (ioctl(fd, I2C_RETRIES, retries) < 0) { 71 | PyErr_SetString(PyExc_RuntimeError, "Fail ioctl I2C_RETRIES"); 72 | return NULL; 73 | } 74 | Py_RETURN_NONE; 75 | } 76 | 77 | static PyObject *py_read_byte(PyObject *self, PyObject *args) 78 | { 79 | int data = -1; 80 | int wait_ms = -1; 81 | int fd = -1; 82 | 83 | if (!PyArg_ParseTuple(args, "ii", &fd, &wait_ms)) 84 | return NULL; 85 | ioctl(fd, BLKFLSBUF); 86 | usleep(1000 * wait_ms); 87 | if ((data = i2c_smbus_read_byte(fd)) == -1) { 88 | PyErr_SetString(PyExc_RuntimeError, "Fail to i2c_smbus_read_byte"); 89 | return NULL; 90 | } 91 | return Py_BuildValue("i", data); 92 | } 93 | 94 | static PyObject *py_write_byte(PyObject *self, PyObject *args) 95 | { 96 | int fd = -1; 97 | int data = -1; 98 | int wait_ms = -1; 99 | if (!PyArg_ParseTuple(args, "iii", &fd, &data, &wait_ms)) 100 | return NULL; 101 | if (data < 0 || data > 0xff) { 102 | PyErr_SetString(PyExc_ValueError, "Data must be 8 bit"); 103 | return NULL; 104 | } 105 | if (i2c_smbus_write_byte(fd, (unsigned char)data) < 0) { 106 | PyErr_SetString(PyExc_RuntimeError, "Fail to i2c_smbus_write_byte"); 107 | return NULL; 108 | } 109 | usleep(1000 * wait_ms); 110 | Py_RETURN_NONE; 111 | } 112 | 113 | static PyObject *py_read_bytefrom(PyObject *self, PyObject *args) 114 | { 115 | int fd = -1; 116 | int addr = -1; 117 | int data = -1; 118 | int wait_ms = -1; 119 | if (!PyArg_ParseTuple(args, "iii", &fd, &addr, &wait_ms)) 120 | return NULL; 121 | if (addr < 0 || addr > 0xff) { 122 | PyErr_SetString(PyExc_ValueError, "Address must be 8 bit"); 123 | return NULL; 124 | } 125 | ioctl(fd, BLKFLSBUF); 126 | if (i2c_smbus_write_byte(fd, (unsigned char)addr) < 0) { 127 | PyErr_SetString(PyExc_RuntimeError, "Fail to i2c_smbus_write_byte"); 128 | return NULL; 129 | } 130 | usleep(1000 * wait_ms); 131 | data = i2c_smbus_read_byte(fd); 132 | return Py_BuildValue("i", data); 133 | Py_RETURN_NONE; 134 | } 135 | 136 | static PyObject *py_write_byteto(PyObject *self, PyObject *args) 137 | { 138 | int addr = -1; 139 | int data = -1; 140 | int wait_ms = -1; 141 | int fd = -1; 142 | 143 | if (!PyArg_ParseTuple(args, "iiii", &fd, &addr, &data, &wait_ms)) 144 | return NULL; 145 | if (data < 0 || data > 0xff || addr < 0 || addr > 0xff) { 146 | PyErr_SetString(PyExc_ValueError, "Address and data must be 8 bit"); 147 | return NULL; 148 | } 149 | if (i2c_smbus_write_byte_data(fd, (unsigned char)addr, (unsigned char)data) < 0) { 150 | PyErr_SetString(PyExc_RuntimeError, "Fail to i2c_smbus_write_byte_data"); 151 | return NULL; 152 | } 153 | usleep(1000 * wait_ms); 154 | Py_RETURN_NONE; 155 | } 156 | 157 | static PyObject *py_close_channel(PyObject *self, PyObject *args) 158 | { 159 | int fd = -1; 160 | if (!PyArg_ParseTuple(args, "i", &fd)) 161 | return NULL; 162 | close(fd); 163 | Py_RETURN_NONE; 164 | } 165 | PyDoc_STRVAR(module_doc, "provides I2C control function for Matrix accessory using Python"); 166 | PyMethodDef matrix_I2C_methods[] = { 167 | {"open_channel", py_open_channel, METH_VARARGS, "open i2c channel"}, 168 | {"set_slave", py_set_slave, METH_VARARGS, "set i2c device slave address"}, 169 | {"set_timeout", py_set_timeout, METH_VARARGS, "set i2c timeout"}, 170 | {"set_retries", py_set_retries, METH_VARARGS, "set i2c retries"}, 171 | {"read_byte", py_read_byte, METH_VARARGS, "i2c read byte"}, 172 | {"write_byte", py_write_byte, METH_VARARGS, "i2c write byte"}, 173 | {"read_bytefrom", py_read_bytefrom, METH_VARARGS, "i2c read byte from address"}, 174 | {"write_byteto", py_write_byteto, METH_VARARGS, "i2c write byte to address"}, 175 | {"close_channel", py_close_channel, METH_VARARGS, "close i2c channel"}, 176 | {NULL, NULL, 0, NULL} 177 | }; 178 | 179 | PyMODINIT_FUNC initI2C(void) 180 | { 181 | PyObject *m; 182 | 183 | if ((m = Py_InitModule3("Matrix.I2C", matrix_I2C_methods, module_doc)) == NULL) 184 | return; 185 | return; 186 | } 187 | -------------------------------------------------------------------------------- /Matrix.I2C/Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /Matrix.I2C/Matrix/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/Matrix.I2C/Matrix/__init__.py -------------------------------------------------------------------------------- /Matrix.I2C/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | MOD = 'Matrix.I2C' 4 | setup(name = MOD, 5 | version = '0.0.1', 6 | url = 'http://www.friendlyarm.com', 7 | author = 'FriendlyARM', 8 | author_email = 'support@friendlyarm.com', 9 | description = 'A module to control Matrix I2C', 10 | keywords = 'matirx I2C', 11 | packages = ['Matrix'], 12 | ext_modules = [Extension(MOD, sources=['I2C.c'])]) 13 | -------------------------------------------------------------------------------- /Matrix.I2C/test/matrix_compass.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import time 5 | import math 6 | import Matrix.I2C as I2C 7 | 8 | WADDR = 0x3C 9 | RADDR = 0x3D 10 | HMC5883_ADDRESS = 0x1E 11 | def hmc5883_read(fd): 12 | try: 13 | I2C.write_byte(fd, RADDR, 6) # read command 14 | x1 = I2C.read_bytefrom(fd, 0x03, 6) 15 | x2 = I2C.read_bytefrom(fd, 0x04, 6) 16 | x = x1 << 8 | x2 # Combine MSB and LSB of X Data output register 17 | 18 | z1 = I2C.read_bytefrom(fd, 0x05, 6) 19 | z2 = I2C.read_bytefrom(fd, 0x06, 6) 20 | z = z1 << 8 | z2 # Combine MSB and LSB of Z Data output register 21 | 22 | y1 = I2C.read_bytefrom(fd, 0x07, 6) 23 | y2 = I2C.read_bytefrom(fd, 0x08, 6) 24 | except IOError, e: 25 | print e 26 | return None 27 | y = y1 << 8 | y2 # Combine MSB and LSB of Y Data output register 28 | if x > 0x8000: 29 | x = -(0xFFFF - x + 1) 30 | if z > 0x8000: 31 | z = -(0xFFFF - z + 1) 32 | if y > 0x8000: 33 | y = -(0xFFFF - y + 1) 34 | 35 | angle = math.atan2(y, x) * (180 / 3.14159265) + 180 36 | if angle >= 0 and angle <= 360: 37 | return int(angle) 38 | else: 39 | return None 40 | 41 | def hmc5883_init(index): 42 | fd = -1 43 | try: 44 | fd = I2C.open_channel(index) 45 | except Exception, e: 46 | print e 47 | return None 48 | try: 49 | I2C.set_slave(fd, HMC5883_ADDRESS) 50 | I2C.write_byte(fd, WADDR, 6) 51 | I2C.write_byteto(fd, 0x00, 0x70, 6) 52 | I2C.write_byte(fd, WADDR, 6) 53 | I2C.write_byteto(fd, 0x02, 0x00, 6) 54 | I2C.write_byte(fd, RADDR, 6) 55 | except Exception, e: 56 | print e 57 | time.sleep(0.1) 58 | return fd 59 | 60 | def hmc5883_deinit(fd): 61 | I2C.close_channel(fd) 62 | 63 | def main(): 64 | fd = hmc5883_init(0) 65 | if not fd: 66 | print "error: fail to init hmc5883" 67 | return 68 | i = 10 69 | while i > 0: 70 | angle = hmc5883_read(fd) 71 | if angle: 72 | print "The angle is:%d" % angle 73 | else: 74 | print "invalid angle" 75 | time.sleep(1) 76 | i = i - 1 77 | hmc5883_deinit(fd) 78 | 79 | if __name__ == '__main__': 80 | main() 81 | -------------------------------------------------------------------------------- /Matrix.pcf8591/.gitignore: -------------------------------------------------------------------------------- 1 | .tags* 2 | /build 3 | -------------------------------------------------------------------------------- /Matrix.pcf8591/Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /Matrix.pcf8591/Matrix/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/Matrix.pcf8591/Matrix/__init__.py -------------------------------------------------------------------------------- /Matrix.pcf8591/common.c: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "common.h" 3 | 4 | int writeValueToFile(char* fileName, char* buff) { 5 | FILE *fp = fopen(fileName,"w"); 6 | if (fp == NULL) { 7 | PyErr_SetString(PyExc_RuntimeError, "Unable to open file"); 8 | return -1; 9 | } else { 10 | fwrite ( buff, strlen(buff), 1, fp ); 11 | } 12 | fclose(fp); 13 | return 0; 14 | } 15 | 16 | int writeIntValueToFile(char* fileName, int value) { 17 | char buff[50]; 18 | sprintf(buff, "%d", value); 19 | return writeValueToFile(fileName, buff); 20 | } 21 | 22 | int readValueFromFile(char* fileName, char* buff, int len) { 23 | int ret = -1; 24 | FILE *fp = fopen(fileName,"r"); 25 | if (fp == NULL) { 26 | PyErr_SetString(PyExc_RuntimeError, "Unable to open file"); 27 | return -1; 28 | } else { 29 | if (fread(buff, sizeof(char), len, fp)>0) { 30 | ret = 0; 31 | } 32 | } 33 | fclose(fp); 34 | return ret; 35 | } 36 | 37 | int readIntValueFromFile(char* fileName) { 38 | char buff[255]; 39 | memset(buff, 0, sizeof(buff)); 40 | int ret = readValueFromFile(fileName, buff, sizeof(buff)-1); 41 | if (ret == 0) { 42 | return atoi(buff); 43 | } 44 | return ret; 45 | } -------------------------------------------------------------------------------- /Matrix.pcf8591/common.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMMON_H_ 2 | #define _COMMON_H_ 3 | 4 | #define __DEBUG 5 | 6 | #ifdef __DEBUG 7 | #define DEBUG(format, args...) \ 8 | printf("FAHW-Lib: " format, ## args) 9 | #else 10 | #define DEBUG(format, args...) 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | extern int writeValueToFile(char* fileName, char* buff); 27 | extern int writeIntValueToFile(char* fileName, int value); 28 | extern int readValueFromFile(char* fileName, char* buff, int len); 29 | extern int readIntValueFromFile(char* fileName); 30 | 31 | #define FILE_PATH_LENGTH (128) 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /Matrix.pcf8591/libfahw-pcf8591.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_PCF8591_H__ 2 | #define __FRIENDLYARM_HARDWARE_PCF8591_H__ 3 | 4 | #define PCF8591_SYS_PATH "/sys/bus/i2c/drivers/pcf8591/" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Matrix.pcf8591/pcf8591.c: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "common.h" 3 | #include "libfahw-pcf8591.h" 4 | 5 | #include 6 | #include 7 | 8 | static int pcf8591Read(int channel, int *value) 9 | { 10 | int existFlag = 0; 11 | DIR *d; 12 | struct dirent *de; 13 | char pcfFile[FILE_PATH_LENGTH]; 14 | 15 | if (!(d = opendir(PCF8591_SYS_PATH))) { 16 | PyErr_SetString(PyExc_RuntimeError, "Fail to opendir pcf8591 directory"); 17 | return -1; 18 | } 19 | while ((de = readdir(d))) { 20 | if (de->d_name[0] == '.') 21 | continue; 22 | 23 | sprintf(pcfFile, "%s%s/in%d_input", PCF8591_SYS_PATH, de->d_name, channel); 24 | if (access(pcfFile, F_OK) != -1) { 25 | existFlag = 1; 26 | break; 27 | } 28 | } 29 | closedir(d); 30 | 31 | if (existFlag == 0) { 32 | PyErr_SetString(PyExc_RuntimeError, "Fail to access pcf8591 sys file"); 33 | return -1; 34 | } 35 | if ((*value = readIntValueFromFile(pcfFile)) != -1) { 36 | return 0; 37 | } else { 38 | PyErr_SetString(PyExc_RuntimeError, "Invalid pcf8591 data"); 39 | return -1; 40 | } 41 | } 42 | 43 | static PyObject *py_pcf8591_read(PyObject *self, PyObject *args) 44 | { 45 | int data = -1; 46 | int channel = -1; 47 | PyObject *value = NULL; 48 | 49 | if (!PyArg_ParseTuple(args, "i", &channel)) 50 | return NULL; 51 | if (channel < 0 || channel > 3) { 52 | PyErr_SetString(PyExc_ValueError, "Pcf8591 only has channel 0 1 2 3"); 53 | return NULL; 54 | } 55 | if (pcf8591Read(channel, &data) == 0) { 56 | value = Py_BuildValue("i", data); 57 | return value; 58 | } else { 59 | return NULL; 60 | } 61 | } 62 | 63 | PyDoc_STRVAR(module_doc, "provides pcf8591 control function for Matrix accessory using Python"); 64 | PyMethodDef matrix_pcf8591_methods[] = { 65 | {"read_channel", py_pcf8591_read, METH_VARARGS, "Read pcf8591"}, 66 | {NULL, NULL, 0, NULL} 67 | }; 68 | 69 | PyMODINIT_FUNC initpcf8591(void) 70 | { 71 | PyObject *m; 72 | 73 | if ((m = Py_InitModule3("Matrix.pcf8591", matrix_pcf8591_methods, module_doc)) == NULL) 74 | return; 75 | return; 76 | } 77 | -------------------------------------------------------------------------------- /Matrix.pcf8591/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | MOD = 'Matrix.pcf8591' 4 | setup(name = MOD, 5 | version = '0.0.1', 6 | url = 'http://www.friendlyarm.com', 7 | author = 'FriendlyARM', 8 | author_email = 'support@friendlyarm.com', 9 | description = 'A module to control Matrix pcf8591', 10 | keywords = 'matirx pcf8591', 11 | packages = ['Matrix'], 12 | ext_modules = [Extension(MOD, sources=['pcf8591.c', 'common.c'])]) 13 | -------------------------------------------------------------------------------- /Matrix.pcf8591/test/matrix_adc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import time 5 | import Matrix.pcf8591 as pcf8591 6 | 7 | def main(): 8 | i = 10 9 | channel = 0 10 | while i > 0: 11 | try: 12 | value = pcf8591.read_channel(channel) 13 | print "Channel%d value=%d" % (channel, value) 14 | except Exception, e: 15 | print e 16 | return 17 | time.sleep(1) 18 | i = i -1 19 | 20 | if __name__ == '__main__': 21 | main() -------------------------------------------------------------------------------- /matrix-3_axis_digital_accelerometer/matrix_accelerometer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import sys 5 | import time 6 | 7 | def read_position(): 8 | sys_dir = '/sys/bus/i2c/drivers/adxl34x' 9 | found = False 10 | try: 11 | for item in os.listdir(sys_dir): 12 | position = '%s/%s/position' % (sys_dir, item) 13 | if os.path.exists(position): 14 | found = True 15 | break; 16 | if not found: 17 | print 'error: adxl34x position not found' % channel 18 | return -1 19 | except OSError, e: 20 | print e 21 | return -1 22 | 23 | try: 24 | fd = open(position, 'r') 25 | except IOError, e: 26 | print 'error: fail to open %s' % position 27 | return -1 28 | else: 29 | value = fd.read() 30 | fd.close() 31 | return value 32 | 33 | def main(): 34 | i = 10 35 | while i > 0: 36 | value = read_position() 37 | if value: 38 | print 'Get position: ', value, 39 | else: 40 | print 'Fail to get position' 41 | break 42 | time.sleep(1) 43 | i = i - 1 44 | 45 | if __name__ == '__main__': 46 | main() -------------------------------------------------------------------------------- /matrix-analog_to_digital_converter/matrix_adc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import sys 5 | import time 6 | 7 | def read_channel(channel): 8 | sys_dir = '/sys/bus/i2c/drivers/pcf8591' 9 | found = False 10 | try: 11 | for item in os.listdir(sys_dir): 12 | inx_input = '%s/%s/in%d_input' % (sys_dir, item, channel) 13 | if os.path.exists(inx_input): 14 | found = True 15 | break; 16 | if not found: 17 | print 'error: pcf8591 channel%d not found' % channel 18 | return -1 19 | except OSError, e: 20 | print e 21 | return -1 22 | 23 | try: 24 | fd = open(inx_input, 'r') 25 | except IOError, e: 26 | print 'error: fail to open %s' % inx_input 27 | return -1 28 | else: 29 | value = fd.read() 30 | fd.close() 31 | return int(value) 32 | 33 | def main(): 34 | argc = len(sys.argv) 35 | if argc == 2: 36 | channel = int(sys.argv[1]) 37 | else: 38 | channel = 0 39 | i = 10 40 | while i > 0: 41 | value = read_channel(channel) 42 | if value >= 0: 43 | print 'Channel%d value=%d' % (channel, value) 44 | else: 45 | print 'Fail to read Channel%d' % channel 46 | break 47 | time.sleep(1) 48 | i = i - 1 49 | 50 | if __name__ == '__main__': 51 | main() -------------------------------------------------------------------------------- /modules/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /bcmdhd.ko 3 | /scsi_wait_scan.ko 4 | -------------------------------------------------------------------------------- /modules/adxl34x-i2c.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/adxl34x-i2c.ko -------------------------------------------------------------------------------- /modules/adxl34x.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/adxl34x.ko -------------------------------------------------------------------------------- /modules/bmp085.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/bmp085.ko -------------------------------------------------------------------------------- /modules/dht11.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/dht11.ko -------------------------------------------------------------------------------- /modules/fbtft_device.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/fbtft_device.ko -------------------------------------------------------------------------------- /modules/matrix_ads7846.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/matrix_ads7846.ko -------------------------------------------------------------------------------- /modules/matrix_gpio_int.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/matrix_gpio_int.ko -------------------------------------------------------------------------------- /modules/matrix_hcsr04.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/matrix_hcsr04.ko -------------------------------------------------------------------------------- /modules/matrix_ir_recv.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/matrix_ir_recv.ko -------------------------------------------------------------------------------- /modules/matrix_pwm.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/matrix_pwm.ko -------------------------------------------------------------------------------- /modules/matrix_rotary_encoder.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/matrix_rotary_encoder.ko -------------------------------------------------------------------------------- /modules/pcf8591.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/pcf8591.ko -------------------------------------------------------------------------------- /modules/rtc-ds1307.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/rtc-ds1307.ko -------------------------------------------------------------------------------- /modules/w1-gpio-board.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/w1-gpio-board.ko -------------------------------------------------------------------------------- /modules/w1-gpio.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix-python/e9908b2597f5dde613c795d2d1cf284cfb3e04a3/modules/w1-gpio.ko --------------------------------------------------------------------------------