<commit_before># -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet.abstract import FileDescriptor
from twisted.internet import fdesc
from twisted.internet import reactor
import os
import select


class GPIOPin(FileDescriptor):
    """A Calvin sysfs based gpio pin object"""
    def __init__(self, trigger, pin, direction, delay=0.2):
        """
        Export gpio pin and set direction and read delay
        """
        super(GPIOPin, self).__init__()
        self.trigger = trigger
        self.pin = pin
        self.direction = direction
        self.delay = delay
        self.data = None
        self.last_read = None

        # export pin
        if not os.path.exists("/sys/class/gpio/gpio" + str(self.pin)):
            fp = open("/sys/class/gpio/export", "w")
            fp.write(str(self.pin))
            fp.close()

        # set direction
        fp = open("/sys/class/gpio/gpio" + str(self.pin) + "/direction", "w")
        fp.write(self.direction)
        fp.close()

        if self.direction == "out":
            self.fp = open("/sys/class/gpio/gpio" + str(self.pin) + "/value", "w")
            self.startWriting()
        elif self.direction == "in":
            self.fp = open("/sys/class/gpio/gpio" + str(self.pin) + "/value", "r")
            self.delayedCall = reactor.callLater(self.delay, self.doRead)
        else:
            raise Exception("Pin direction must be in or out")

        fdesc.setNonBlocking(self)
        self.connected = True  # Required by FileDescriptor class

    def fileno(self):
        return self.fp.fileno()

    def writeSomeData(self, data):
        return fdesc.writeToFD(self.fp.fileno(), data)

    def _closeWriteConnection(self):
        self.connected = False
        self.fp.close()
        if os.path.exists("/sys/class/gpio/gpio" + str(self.pin)):
            fp = open("/sys/class/gpio/unexport", "w")
            fp.write(str(self.pin))
            fp.close()

    def dataRead(self, data):
        self.data = data
        self.fp.seek(0)
        self.delayedCall = reactor.callLater(self.delay, self.doRead)

    def doRead(self):
        if self.direction == "in":
            self.trigger()
            return fdesc.readFromFD(self.fp.fileno(), self.dataRead)

    def set_state(self, state):
        """
        Set state of pin
        """
        if self.direction == "out":
            self.fp.write(state)
            self.fp.seek(0)
        else:
            raise Exception("Pin direction must be out")

    def has_changed(self):
        """
        True if data has changed since last call to get_state()
        """
        return self.data != self.last_read

    def get_state(self):
        """
        Get state of pin
        """
        if self.direction == "in":
            self.last_read = self.data
            return self.data
        else:
            raise Exception("Pin direction must be in")

    def close(self):
        """
        Close file and unexport pin
        """
        self.loseConnection()
<commit_msg>gpiopin: Return 1/0 for state high/low<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet.abstract import FileDescriptor
from twisted.internet import fdesc
from twisted.internet import reactor
import os


class GPIOPin(FileDescriptor):
    """A Calvin sysfs based gpio pin object"""
    def __init__(self, trigger, pin, direction, delay=0.2):
        """
        Export gpio pin and set direction and read delay
        """
        super(GPIOPin, self).__init__()
        self.trigger = trigger
        self.pin = pin
        self.direction = direction
        self.delay = delay
        self.data = 0
        self.last_read = None

        # export pin
        if not os.path.exists("/sys/class/gpio/gpio" + str(self.pin)):
            fp = open("/sys/class/gpio/export", "w")
            fp.write(str(self.pin))
            fp.close()

        # set direction
        fp = open("/sys/class/gpio/gpio" + str(self.pin) + "/direction", "w")
        fp.write(self.direction)
        fp.close()

        if self.direction == "out":
            self.fp = open("/sys/class/gpio/gpio" + str(self.pin) + "/value", "w")
            self.startWriting()
        elif self.direction == "in":
            self.fp = open("/sys/class/gpio/gpio" + str(self.pin) + "/value", "r")
            self.delayedCall = reactor.callLater(self.delay, self.doRead)
        else:
            raise Exception("Pin direction must be in or out")

        fdesc.setNonBlocking(self)
        self.connected = True  # Required by FileDescriptor class

    def fileno(self):
        return self.fp.fileno()

    def writeSomeData(self, data):
        return fdesc.writeToFD(self.fp.fileno(), data)

    def _closeWriteConnection(self):
        self.connected = False
        self.fp.close()
        if os.path.exists("/sys/class/gpio/gpio" + str(self.pin)):
            fp = open("/sys/class/gpio/unexport", "w")
            fp.write(str(self.pin))
            fp.close()

    def dataRead(self, data):
        if "1" in data:
            self.data = 1
        else:
            self.data = 0
        self.fp.seek(0)
        self.delayedCall = reactor.callLater(self.delay, self.doRead)

    def doRead(self):
        if self.direction == "in":
            self.trigger()
            return fdesc.readFromFD(self.fp.fileno(), self.dataRead)

    def set_state(self, state):
        """
        Set state of pin
        """
        if self.direction == "out":
            self.fp.write(state)
            self.fp.seek(0)
        else:
            raise Exception("Pin direction must be out")

    def has_changed(self):
        """
        True if data has changed since last call to get_state()
        """
        return self.data != self.last_read

    def get_state(self):
        """
        Get state of pin, 1/0 for state high/low
        """
        if self.direction == "in":
            self.last_read = self.data
            return self.data
        else:
            raise Exception("Pin direction must be in")

    def close(self):
        """
        Close file and unexport pin
        """
        self.loseConnection()

{'fraction_non_alphanumeric': 0.1173238082154142, 'fraction_numerical': 0.004670971287264734, 'mean_word_length': 2.073026593499367, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7657081', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2060, 'n_words': 612}