Controlling the Pi TFT tactile buttons with C++

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
OldManIvan
 
Posts: 5
Joined: Fri Jun 20, 2014 9:45 am

Controlling the Pi TFT tactile buttons with C++

Post by OldManIvan »

Hi guys,

I'm trying to read the button status of the Pi TFT tactile buttons via C++, but it seems that I've hit a wall. The program seems to work, but no button signal is registered.
Since I've hit new land with GPIO-programming, I've followed this guide to write the following program:

Code: Select all

#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include "gpioOutput.h"

using namespace std;
string pinNumbers [4] = {"18", "22", "23", "27"};

gpioButton::gpioButton()
{
}

void gpioButton::initGPIO()
{
    string gpioDir;
    for(int c = 0; c < 4; c++)
    {
        gpioDir = "/sys/class/gpio/export";
        ofstream gpioExport(gpioDir.c_str());
        gpioExport << pinNumbers[c];
        gpioExport.close();
        gpioDir = "/sys/class/gpio/gpio" + pinNumbers[c] + "/direction";
        ofstream gpioDirection(gpioDir.c_str());
        gpioDirection << "in";
        gpioDirection.close();
    }
}

void gpioButton::unexportGPIO()
{
    string gpioDir;
    for(int c = 0; c < 4; c++)
    {
        gpioDir = "/sys/class/gpio/unexport";
        ofstream gpioExport(gpioDir.c_str());
        gpioExport << pinNumbers[c];
        gpioExport.close();
    }
}

bool gpioButton::getValue(int buttonNumber)
{
    string gpioDir = "/sys/class/gpio/gpio" + pinNumbers[buttonNumber] + "/value";
    ifstream gpioRead(gpioDir.c_str());
    string val;
    gpioRead >> val;
    gpioRead.close();
    if(val != "0")
    {
        cout << "yay";
        return true;
    }
    else
    {
        cout << "nay";
        return false;
    }
}
It's a bit rough around the edges, but I'll get to that once it's working. In essence, the initGPIO opens the pins 18, 27, 22 and 23 and sets the direction to input, whereas getValue reads the value of the selected pin and returns TRUE or FALSE depending on the value in /sys/class/gpio. (The yay/nay-bit is for debugging-purposes ;-) )
Since there seems to be no change on the GPIO-pins, I've tried measuring the voltage. Oddly enough, it merely toggles between 0V and 45mV, which seems pretty low. Would disabling the pull-up-resistors help? And if it would, how can I disable them via C++?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Controlling the Pi TFT tactile buttons with C++

Post by adafruit_support_rick »

The GPIO lines attached to the switches require pullups, otherwise they will float, which is what seems to be happening in your case. Pressing the switch shorts the GPIO pin to GND, so the pin state should normally be high, if you have the pullups enabled.

I suspect that you are not really enabling the pullups on the pins. You should check that part of your code.

OldManIvan
 
Posts: 5
Joined: Fri Jun 20, 2014 9:45 am

Re: Controlling the Pi TFT tactile buttons with C++

Post by OldManIvan »

I've tried activating pullup like instructed on this site which resulted in the following lines being added to my initGpio():

Code: Select all

gpioDir = "/sys/class/gpio/gpio" + pinNumbers[c]+"/drive";
ofstream gpioDrive(gpioDir.c_str());
gpioDrive << "pullup";
gpioDrive.close();
I've tried the pullup and strong settings, however, neither seem to work, and I'm slowly running out of ideas. The odd thing is that when browsing through /sys/class/gpio/gpio<number>/, the drive file that's supposed to contain the pull up setting is nowhere to be found.
To check if the buttons are working at all, I gave Adafruits rpi_power_switch-module a shot, and that one worked fine. Is there a way to get a look at the code of the module?

EDIT: For some reason, Firefox thought that it'd be a hot idea to submit the post while I was still writing.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Controlling the Pi TFT tactile buttons with C++

Post by adafruit_support_rick »

I think the rpi_power_switch code is here:
https://github.com/notro/fbtft_tools/tr ... wer_switch

User avatar
FishX
 
Posts: 7
Joined: Fri Jun 06, 2014 8:45 pm

Re: Controlling the Pi TFT tactile buttons with C++

Post by FishX »

Code: Select all

sudo apt-get update
sudo apt-get install libncurses5-dev

gcc loader.c -o loader -I/usr/include -L/usr/lib -lncurses -lmenu -lwiringPi

/*树莓派开机通过GPIO按钮选择执行程序,Version1.0
作者:FishX @ bbs.ickey.cn @weibo.com 鱼叉wb
欢迎转载,转载请注明出处*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <curses.h>
#include <menu.h>
#define BTN_UP 4
#define BTN_DOWN 3
#define BTN_OK 2
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

/*菜单项及其描述*/
char *choices[] = {"1.Network Toolbox","2.Camera","3.Shutdown","4.Back to console"};
char *choice_descp[] = {"","","",""};

/*初始化GPIO按钮*/
void gpiosetup (void){
	if (geteuid() != 0){
		fprintf(stderr, "nAlert: Need to be root to run (sudo?)n") ;
		exit (0);
	}
	if (wiringPiSetup() == -1)
		exit (1) ;
	fflush (stdout) ;
	//BTN_UP 4 AdaPanel 23,BTN_DOWN 3 AdaPanel 22,BTN_ENTER AdaPanel 21 or 27
	//把GPIO设置为上拉模式
	pullUpDnControl (BTN_UP,PUD_UP);
	pullUpDnControl (BTN_DOWN,PUD_UP);
	pullUpDnControl (BTN_OK,PUD_UP);
}

/*主程序入口*/
void main(){
	//初始化GPIO按钮
	gpiosetup();
	//构建菜单
	ITEM **my_items;
	int c;
	MENU *my_menu;
	int n_choices, i;
	ITEM *cur_item;
	initscr();
	cbreak();
	noecho();
	keypad(stdscr, TRUE);
	n_choices = ARRAY_SIZE(choices);
	my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
	for(i = 0; i < n_choices; ++i)
		my_items[i] = new_item(choices[i],choice_descp[i]);
	my_items[n_choices] = (ITEM *)NULL;
	my_menu = new_menu((ITEM **)my_items);
	post_menu(my_menu);
	refresh();
	//开始检测按键
	int ret = 0;
	while(ret==0){
		refresh();
		if (digitalRead (BTN_UP) == LOW){
			menu_driver(my_menu, REQ_UP_ITEM);
			delay(200);
			continue;
		}
		if (digitalRead (BTN_DOWN) == LOW){
			menu_driver(my_menu, REQ_DOWN_ITEM);
			delay(200);
			continue;
		}
		if (digitalRead (BTN_OK) == LOW){
			switch(item_index(current_item(my_menu)))
				{	
					case 0://执行第一个程序
						def_prog_mode();
						endwin();
						system ("cd //home//pi//xpi// && sudo python tools.py");
						reset_prog_mode();
						refresh();
						break;
					case 1://执行第二个程序
						def_prog_mode();
						endwin();
						system ("cd //home//pi//ada// && sudo python cam.py");
						reset_prog_mode();
						refresh();
						break;
					case 2://关机
						free_item(my_items[0]);
						free_item(my_items[1]);
						free_menu(my_menu);
						endwin();
						refresh();
						system ("sudo shutdown -h now");
						break;
					case 3://退出返回到控制台
						ret = 1;
						break;
				}
			delay(200);
			continue;
		}
		mvprintw(LINES-1,0,"[%s]       ", item_name(current_item(my_menu)));
	}
	free_item(my_items[0]);
	free_item(my_items[1]);
	free_menu(my_menu);
	endwin();
	fflush (stdout) ;
}




User avatar
FishX
 
Posts: 7
Joined: Fri Jun 06, 2014 8:45 pm

Re: Controlling the Pi TFT tactile buttons with C++

Post by FishX »

Effect:
First Button:Up
Second Button:Down
Third Button:OK
Fourth Button:halt
Image

sudo nano /etc/rc.local
add this line before exit 0;
sudo /home/pi/loader &
It will run after Pi's boot

see my video:
http://v.youku.com/v_show/id_XNzE1MDg5MzY0.html

OldManIvan
 
Posts: 5
Joined: Fri Jun 20, 2014 9:45 am

Re: Controlling the Pi TFT tactile buttons with C++

Post by OldManIvan »

Thanks for your replies. It seems like there's no avoiding wiringPi.
Indeed, the pull up resistor was the problem. Unfortunatly for me, you can't toggle the pull up using the sysfs method. As I'm trying to avoid custom libraries, I've chosen the hardware solution, meaning I've soldered a 330 Ohm resistor between the GPIO- and 3.3V-pins.
It ain't a pretty solution but so far, it seems to work.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”