gpio.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #ifndef HAND_BRIDGE_GPIO_H_
00019 #define HAND_BRIDGE_GPIO_H_
00020 
00021 #include "pigpio.h"
00022 
00023 class GPIO {
00024     bool initialized;
00025     uint32_t out_mask0;
00026     bool call(int (*func)(uint32_t), uint32_t mask, uint32_t pins){
00027         return initialized && (pins & mask) == pins && (pins == 0 || func(pins) == 0);
00028     }
00029 public:
00030     GPIO() : initialized(false), out_mask0(0) {}
00031     bool isInitialized() const { return initialized; }
00032     bool setInput(uint32_t pin) {
00033         return initialized && gpioSetMode(pin, PI_INPUT) == 0;
00034     }
00035     bool setOutput(uint32_t pin) {
00036         if(initialized && gpioSetMode(pin, PI_OUTPUT) == 0){
00037             if(pin < 32) out_mask0 |= (1<<pin);
00038             return true;
00039         }
00040         return false;
00041     }
00042     bool init(){
00043         return initialized = gpioInitialise() >= 0;
00044     }
00045     bool clearPins(uint32_t pins){
00046         return call(gpioWrite_Bits_0_31_Clear, out_mask0, pins);
00047     }
00048     bool setPins(uint32_t pins){
00049         return call(gpioWrite_Bits_0_31_Set, out_mask0, pins);
00050     }
00051     bool writePin(uint32_t pin, uint32_t level) {
00052         if(!initialized) return false;
00053         if(pin < 32 &&  ((1<<pin) & out_mask0) != (1<<pin)) return false;
00054 
00055         return gpioWrite(pin, level ? 1 : 0) == 0;
00056     }
00057     bool pwmPin(uint32_t pin, float level) {
00058         if(!initialized) return false;
00059         if(pin >= 32 ||  ((1<<pin) & out_mask0) != (1<<pin)) return false;
00060 
00061         int range;
00062         if ((range = gpioGetPWMrange(pin)) < 0) return false;
00063 
00064         if(level < 0 || level > 1.0) return false;
00065 
00066         return gpioPWM(pin, level* range) == 0;
00067     }
00068     uint32_t getState() {
00069         return initialized ? gpioRead_Bits_0_31(): 0;
00070     }
00071 };
00072 #endif // HAND_BRIDGE_GPIO_H_


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57