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 <fcntl.h>
|
||||
#include <stdio.h>
|
||||
@@ -42,30 +18,36 @@
|
||||
* power led - gpiol.10 --> gpio362
|
||||
*/
|
||||
|
||||
#define GPIO_LED "/sys/class/gpio/gpio10"
|
||||
#define LED "10"
|
||||
#define GPIO_LED "/sys/class/gpio/gpio10"
|
||||
#define LED "10"
|
||||
|
||||
#define GPIO_BTN1 "/sys/class/gpio/gpio0"
|
||||
#define BTN1 "0"
|
||||
#define BTN1 "0"
|
||||
#define GPIO_BTN2 "/sys/class/gpio/gpio2"
|
||||
#define BTN2 "2"
|
||||
#define BTN2 "2"
|
||||
#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)
|
||||
{
|
||||
printf("Starting Button 1 Test...\n");
|
||||
|
||||
int btn1 = open_btn(GPIO_BTN1, BTN1);
|
||||
if (btn1 < 0) {
|
||||
perror("Failed to open button");
|
||||
return 1;
|
||||
// Open all button with the right flags
|
||||
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");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Create epoll instance
|
||||
// Create epoll instance to control all button files
|
||||
int epfd = epoll_create1(0);
|
||||
if (epfd < 0) {
|
||||
perror("Failed to create epoll");
|
||||
@@ -73,25 +55,29 @@ int main(void)
|
||||
}
|
||||
|
||||
// 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!
|
||||
ev.events = EPOLLPRI | EPOLLERR | EPOLLET;
|
||||
ev.data.fd = btn1;
|
||||
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, btn1, &ev) < 0) {
|
||||
perror("Failed to add btn1 to epoll");
|
||||
return 1;
|
||||
for(int i=0; i<NBR_BTN; i++) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Dummy read to clear initial state before waiting
|
||||
// Dummy read to clear initial state before waiting
|
||||
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) {
|
||||
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!
|
||||
int n = epoll_wait(epfd, events, 1, -1);
|
||||
@@ -102,21 +88,27 @@ int main(void)
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (events[i].data.fd == btn1) {
|
||||
// Read the new value. pread uses offset 0 so we don't need lseek()
|
||||
pread(btn1, buf, sizeof(buf), 0);
|
||||
for (int j = 0; j < NBR_BTN; j++) {
|
||||
if (events[i].data.fd == btn[j]) {
|
||||
|
||||
// Print the result. '1' or '0' depends on your hardware pull-up/down resistors
|
||||
if (buf[0] == '1') {
|
||||
printf("Button 1 State: HIGH (1)\n");
|
||||
} else {
|
||||
printf("Button 1 State: LOW (0)\n");
|
||||
// Read the new value. pread uses offset 0 so we don't need lseek()
|
||||
pread(btn[j], buf, sizeof(buf), 0);
|
||||
|
||||
// Print the result. '1' or '0' depends on your hardware pull-up/down resistors
|
||||
if (buf[0] == '1') {
|
||||
printf("Button %d State: HIGH (1)\n", j+1);
|
||||
} else {
|
||||
printf("Button %d State: LOW (0)\n", j+1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(btn1);
|
||||
for (int i=0; i<NBR_BTN; i++) {
|
||||
close(btn[i]);
|
||||
}
|
||||
close(epfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#define GPIO_EXPORT "/sys/class/gpio/export"
|
||||
#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)
|
||||
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
||||
@@ -41,7 +41,7 @@ static int open_led(char* gpio_path, char* pin) {
|
||||
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);
|
||||
write(f, pin, strlen(pin));
|
||||
close(f);
|
||||
|
||||
Reference in New Issue
Block a user