This repository has been archived on 2018-05-28. You can view files and clone it, but cannot push or open issues or pull requests.
BBB-Simple-ACS/client/lib/gpio.hpp

101 lines
2.7 KiB
C++

/*
* @file GPIO.h
* @author Derek Molloy
* @version 0.1
*
* Created on: 29 Apr 2014
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring BeagleBone"
* See: www.exploringbeaglebone.com
* Licensed under the EUPL V.1.1
*
* This Software is provided to You under the terms of the European
* Union Public License (the "EUPL") version 1.1 as published by the
* European Union. Any use of this Software, other than as authorized
* under this License is strictly prohibited (to the extent such use
* is covered by a right of the copyright holder of this Software).
*
* This Software is provided under the License on an "AS IS" basis and
* without warranties of any kind concerning the Software, including
* without limitation merchantability, fitness for a particular purpose,
* absence of defects or errors, accuracy, and non-infringement of
* intellectual property rights other than copyright. This disclaimer
* of warranty is an essential part of the License and a condition for
* the grant of any rights to this Software.
*
* For more details, see http://www.derekmolloy.ie/
*/
#pragma once
#include <string>
#include <fstream>
class gpio
{
public:
enum direction
{
input, output
};
enum value
{
low = 0, high = 1
};
enum edge
{
none, rising, falling, both
};
private:
static constexpr const char* base_path = "/sys/class/gpio/";
/// The GPIO number of the object
int number_;
/// The name of the GPIO
std::string name_;
/// The full path to the GPIO
std::string path_;
/// Write a string to a file.
static int write(const std::string& path, const std::string& filename, const std::string& value);
/// Write an integer to a file.
static int write(const std::string& path, const std::string& filename, int value);
/// Read a string from a file.
static std::string read(const std::string& path, const std::string& filename);
public:
explicit gpio(int number);
int get_number() const
{
return number_;
}
// General Input and Output Settings
int set_direction(direction);
direction get_direction() const;
int set_value(value);
int toggle_output();
value get_value() const;
int set_active_low(bool is_low = true);
int set_active_high(); //default
// Advanced OUTPUT: Faster write by keeping the stream alive (~20X)
int stream_open();
int stream_write(value);
int stream_close();
// Advanced INPUT: Detect input edges; threaded and non-threaded
int set_edge_type(edge);
edge get_edge_type() const;
~gpio();
private:
int export_gpio();
int unexport_gpio();
std::ofstream stream_;
};