feat(lab03): setup epoll on 3 button
This commit is contained in:
@@ -1,27 +1,3 @@
|
|||||||
/**
|
|
||||||
* Copyright 2018 University of Applied Sciences Western Switzerland / Fribourg
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* Project: HEIA-FR / HES-SO MSE - MA-CSEL1 Laboratory
|
|
||||||
*
|
|
||||||
* Abstract: System programming - file system
|
|
||||||
*
|
|
||||||
* Purpose: NanoPi silly status led control system
|
|
||||||
*
|
|
||||||
* Autĥor: Daniel Gachet
|
|
||||||
* Date: 07.11.2018
|
|
||||||
*/
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -52,20 +28,26 @@
|
|||||||
#define GPIO_BTN3 "/sys/class/gpio/gpio3"
|
#define GPIO_BTN3 "/sys/class/gpio/gpio3"
|
||||||
#define BTN3 "3"
|
#define BTN3 "3"
|
||||||
|
|
||||||
|
#define NBR_BTN 3
|
||||||
|
|
||||||
|
// constant
|
||||||
|
const char* GPIO_BTN[NBR_BTN] = {GPIO_BTN1, GPIO_BTN2, GPIO_BTN3};
|
||||||
|
const char* BTN[NBR_BTN] = {BTN1, BTN2, BTN3};
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
printf("Starting Button 1 Test...\n");
|
|
||||||
|
|
||||||
int btn1 = open_btn(GPIO_BTN1, BTN1);
|
// Open all button with the right flags
|
||||||
if (btn1 < 0) {
|
int btn[NBR_BTN] = {0};
|
||||||
|
for(int i=0; i<NBR_BTN; i++) {
|
||||||
|
btn[i] = open_btn(GPIO_BTN[i], BTN[i]);
|
||||||
|
if (btn[i] < 0) {
|
||||||
perror("Failed to open button");
|
perror("Failed to open button");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1. Create epoll instance
|
// Create epoll instance to control all button files
|
||||||
int epfd = epoll_create1(0);
|
int epfd = epoll_create1(0);
|
||||||
if (epfd < 0) {
|
if (epfd < 0) {
|
||||||
perror("Failed to create epoll");
|
perror("Failed to create epoll");
|
||||||
@@ -73,25 +55,29 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Add button to epoll
|
// 2. Add button to epoll
|
||||||
struct epoll_event ev;
|
struct epoll_event ev[NBR_BTN];
|
||||||
// CRITICAL: Sysfs GPIOs use EPOLLPRI (priority data) and EPOLLERR, not EPOLLIN!
|
// CRITICAL: Sysfs GPIOs use EPOLLPRI (priority data) and EPOLLERR, not EPOLLIN!
|
||||||
ev.events = EPOLLPRI | EPOLLERR | EPOLLET;
|
|
||||||
ev.data.fd = btn1;
|
|
||||||
|
|
||||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, btn1, &ev) < 0) {
|
for(int i=0; i<NBR_BTN; i++) {
|
||||||
perror("Failed to add btn1 to epoll");
|
ev[i].events = EPOLLPRI | EPOLLERR | EPOLLET;
|
||||||
|
ev[i].data.fd = btn[i];
|
||||||
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, btn[i], &ev[i]) < 0) {
|
||||||
|
perror("Failed to add button to epoll");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 3. Dummy read to clear initial state before waiting
|
// Dummy read to clear initial state before waiting
|
||||||
char buf[2];
|
char buf[2];
|
||||||
pread(btn1, buf, sizeof(buf), 0);
|
for(int i=0; i<NBR_BTN; i++) {
|
||||||
|
pread(btn[i], buf, sizeof(buf), 0);
|
||||||
|
}
|
||||||
|
|
||||||
printf("Waiting for button presses (CPU should be at 0%%)...\n");
|
printf("Waiting for button presses...\n");
|
||||||
|
|
||||||
// 4. Event loop
|
// Event main loop
|
||||||
while (1) {
|
while (1) {
|
||||||
struct epoll_event events[1]; // We only care about 1 event for now
|
struct epoll_event events[NBR_BTN]; // We only care about 1 event for now
|
||||||
|
|
||||||
// Timeout is -1: Block infinitely until an event occurs!
|
// Timeout is -1: Block infinitely until an event occurs!
|
||||||
int n = epoll_wait(epfd, events, 1, -1);
|
int n = epoll_wait(epfd, events, 1, -1);
|
||||||
@@ -102,21 +88,27 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
if (events[i].data.fd == btn1) {
|
for (int j = 0; j < NBR_BTN; j++) {
|
||||||
|
if (events[i].data.fd == btn[j]) {
|
||||||
|
|
||||||
// Read the new value. pread uses offset 0 so we don't need lseek()
|
// Read the new value. pread uses offset 0 so we don't need lseek()
|
||||||
pread(btn1, buf, sizeof(buf), 0);
|
pread(btn[j], buf, sizeof(buf), 0);
|
||||||
|
|
||||||
// Print the result. '1' or '0' depends on your hardware pull-up/down resistors
|
// Print the result. '1' or '0' depends on your hardware pull-up/down resistors
|
||||||
if (buf[0] == '1') {
|
if (buf[0] == '1') {
|
||||||
printf("Button 1 State: HIGH (1)\n");
|
printf("Button %d State: HIGH (1)\n", j+1);
|
||||||
} else {
|
} else {
|
||||||
printf("Button 1 State: LOW (0)\n");
|
printf("Button %d State: LOW (0)\n", j+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(btn1);
|
for (int i=0; i<NBR_BTN; i++) {
|
||||||
|
close(btn[i]);
|
||||||
|
}
|
||||||
close(epfd);
|
close(epfd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
#define GPIO_EXPORT "/sys/class/gpio/export"
|
#define GPIO_EXPORT "/sys/class/gpio/export"
|
||||||
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
||||||
|
|
||||||
static int open_led(char* gpio_path, char* pin) {
|
static int open_led(const char* gpio_path, const char* pin) {
|
||||||
|
|
||||||
// unexport pin out of sysfs (reinitialization)
|
// unexport pin out of sysfs (reinitialization)
|
||||||
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
||||||
@@ -41,7 +41,7 @@ static int open_led(char* gpio_path, char* pin) {
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int open_btn(char* gpio_path, char* pin) {
|
static int open_btn(const char* gpio_path, const char* pin) {
|
||||||
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
||||||
write(f, pin, strlen(pin));
|
write(f, pin, strlen(pin));
|
||||||
close(f);
|
close(f);
|
||||||
|
|||||||
Reference in New Issue
Block a user