|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Arduino API Library is an API layer to quickly prototype ideas using the Arduino. |
| 5 | +Copyright (C) 2009 Akash Xavier <akashmanohar@gmail.com> |
| 6 | +
|
| 7 | +This program is free software: you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation, either version 3 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +This program is distributed in the hope that it will be useful, |
| 13 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +""" |
| 20 | + |
| 21 | +import serial, time |
| 22 | + |
| 23 | +class Arduino: |
| 24 | + |
| 25 | + __OUTPUT_PINS = -1 |
| 26 | + |
| 27 | + def __init__(self, port, baudrate=115200): |
| 28 | + self.serial = serial.Serial(port, baudrate) |
| 29 | + |
| 30 | + def __str__(self): |
| 31 | + return "Arduino is on port %s at %d baudrate" %(self.serial.port, self.serial.baudrate) |
| 32 | + |
| 33 | + def output(self, pinArray): |
| 34 | + self.__sendData(len(pinArray)) |
| 35 | + |
| 36 | + if(isinstance(pinArray, list) or isinstance(pinArray, tuple)): |
| 37 | + self.__OUTPUT_PINS = pinArray |
| 38 | + for each_pin in pinArray: |
| 39 | + self.__sendPin(each_pin) |
| 40 | + return True |
| 41 | + |
| 42 | + def setLow(self, pin): |
| 43 | + self.__sendData('0') |
| 44 | + self.__sendPin(pin) |
| 45 | + return True |
| 46 | + |
| 47 | + def setHigh(self, pin): |
| 48 | + self.__sendData('1') |
| 49 | + self.__sendPin(pin) |
| 50 | + return True |
| 51 | + |
| 52 | + def getState(self, pin): |
| 53 | + self.__sendData('2') |
| 54 | + self.__sendPin(pin) |
| 55 | + return self.__formatPinState(self.__getData()) |
| 56 | + |
| 57 | + def analogWrite(self, pin, value): |
| 58 | + self.__sendData('3') |
| 59 | + hex_value = hex(value)[2:] |
| 60 | + if(len(hex_value)==1): |
| 61 | + self.__sendData('0') |
| 62 | + else: |
| 63 | + self.__sendData(hex_value[0]) |
| 64 | + self.__sendData(hex_value[1]) |
| 65 | + return True |
| 66 | + |
| 67 | + def analogRead(self, pin, value): |
| 68 | + self.__sendData('4') |
| 69 | + return self.__getData() |
| 70 | + |
| 71 | + def turnOff(self): |
| 72 | + for each_pin in self.__OUTPUT_PINS: |
| 73 | + self.setLow(each_pin) |
| 74 | + return True |
| 75 | + |
| 76 | + def __sendPin(self, pin): |
| 77 | + pin_in_char = chr(pin+48) |
| 78 | + self.__sendData(pin_in_char) |
| 79 | + |
| 80 | + def __sendData(self, serial_data): |
| 81 | + while(self.__getData()!="what"): |
| 82 | + pass |
| 83 | + self.serial.write(str(serial_data)) |
| 84 | + |
| 85 | + def __getData(self): |
| 86 | + return self.serial.readline().replace("\r\n","") |
| 87 | + |
| 88 | + def __formatPinState(self, pinValue): |
| 89 | + if pinValue=='1': |
| 90 | + return True |
| 91 | + else: |
| 92 | + return False |
| 93 | + |
| 94 | + def close(self): |
| 95 | + self.serial.close() |
| 96 | + return True |
| 97 | + |
| 98 | + """ |
| 99 | + def __del__(self): |
| 100 | + #close serial connection once program ends |
| 101 | + #this fixes the problem of port getting locked or unrecoverable in some linux systems |
| 102 | + self.serial.close() |
| 103 | + """ |
| 104 | + |
0 commit comments