I have a few GPIO pins configured as an input as well as a button. The interrupt for the button works great (I need to add some debouncing). But sometimes when the button is pushed, or if I just touch the header pins with my fingers, the other gio interrupts are firing.
The gio pins are configured with pull down resistors and set to fire on a rising edge.
For simplicity I have removed most of the gio pins I'm using and just shown one, but I have the following configured for input.
PA4, PA7,PA10, PB6, PB8, PB9
// Enable GPIO Peripheral clock
RCC->AHB1ENR |= BLINK_RCC_MASKx(BLINK_PORT_NUMBER);
GPIO_InitTypeDef GPIO_InitStruct;
/*Configure GPIO pin : PC13 - Button input*/
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
//Init LED
/*Configure GPIO pin : PA5 - LD2 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
void EXTI9_5_IRQHandler(void)
{
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_7) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7);
//Do something
#ifdef DEBUG
trace_printf("PA7 interrupt. Tick count: %u Counts\n", xTaskGetTickCount());
#endif
}
void EXTI15_10_IRQHandler(void)
{
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_10) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_10);
//Do something
#ifdef DEBUG
trace_printf("PA10 interrupt. Tick count: %u Counts\n", xTaskGetTickCount());
#endif
}
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
trace_printf("User Button Pushed. Tick count: %u Counts\n", xTaskGetTickCount());
}
}
Related
How can I set the font name and size for a text_view? Or do I have to set the font information at the buffer or at the window? Do I have to create some sort of style-sheet?
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *view;
GtkTextBuffer *buffer;
gtk_init(&argc, &argv);
view = gtk_text_view_new();
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_default_size(GTK_WINDOW(window), 350, 300);
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
The following code works.
my.c
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *view;
GtkTextBuffer *buffer;
GtkWidget *window;
GdkDisplay *display;
GdkScreen *screen;
GtkCssProvider *provider;
GError *error;
gtk_init(&argc, &argv);
view = gtk_text_view_new();
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
provider = gtk_css_provider_new();
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
error = NULL;
gtk_css_provider_load_from_file (provider, g_file_new_for_path("my.css"), &error);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_default_size(GTK_WINDOW(window), 350, 300);
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
my.css
GtkTextView
{
font:Monospace 10;
}.
I know it is 2 years old question but in case someone encounter any issues.
Note, that Pango syntax for the font is deprecated in Gtk+3.
If you want to use GtkSourceView:
#include <gtksourceview/gtksource.h>
must be installed separately and compiled with
`pkg-config --cflags --libs gtksourceview-3.0`
or use regular GtkTextView
GtkCssProvider *cssProvider;
GtkSourceView *view;
GError *error = NULL;
GtkStyleContext *context;
/* new css provider */
cssProvider = gtk_css_provider_new();
view = GTK_SOURCE_VIEW(gtk_source_view_new ());
/* widget name for css syntax */
gtk_widget_set_name (GTK_WIDGET(view), "cssView");
/* load css file */
gtk_css_provider_load_from_path (cssProvider, "main.css", &error);
/* get GtkStyleContext from widget */
context = gtk_widget_get_style_context(GTK_WIDGET(view));
/* finally load style provider */
gtk_style_context_add_provider(context,
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
and "main.css" file in the same directory
#cssView {
font: 15px "Monospace";
color: #ff0000; /*in case you need red color*/
}
Actually in Gtk+3 you can add and remove CSS classes in very easy way:
in one callback you need to call:
gtk_style_context_add_class(context, "redFonts");
then in another one:
gtk_style_context_remove_class(context, "redFonts");
your "main.css" should be like:
#tab1Content {
font: 15px "Monospace";
}
.redFonts{
color: #ff0000;
}
I am using stm32f4xx with HAL library and I have configured some ports like this:
#define Led1 GPIO_PIN_1
#define Led1_Port GPIOC
#define Led2 GPIO_PIN_2
#define Led2_PoRT GPIOE
GPIO_InitStruct.Pin = Led1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Led1_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = Led2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Led2_Port, &GPIO_InitStruct);
I want to move the code above (not the defines of course) to a function which can be then called and setup the ports in the exact same way as above:
#define Led1 GPIO_PIN_1
#define Led1_Port GPIOC
#define Led2 GPIO_PIN_2
#define Led2_PoRT GPIOE
void GPIOConfig(*Pin,GPIO_TypeDef *Port)
{
GPIO_InitStruct.Pin = &Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(&Port, &GPIO_InitStruct);
}
// Calling the configuration function
GPIOConfig(Led1,Led1_Port);
GPIOConfig(Led2,Led2_Port);
For some reason it doesn't work.
I think your init function should be more like this as Pin is just a number and not a pointer. Also you are also passing Port in as a pointer which is what you want to pass to HAL_GPIO_Init (and not the address of the pointer):
void GPIOConfig(uint32_t Pin,GPIO_TypeDef *Port)
{
GPIO_InitStruct.Pin = Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Port, &GPIO_InitStruct);
}
And then should called like:
GPIOConfig( Led1, Led1_Port );
Everybody, Hello.
I'm new to gtk+ and want to ask a newbie question.
I want make a software of gtk2.10 in arm. My code is followed.
GtkWidget *windowmain = NULL;
GtkWidget *thebox = NULL;
GtkWidget *boxmain = NULL;
GtkWidget *boxtreeviewlist = NULL;
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
boxmain = createmain();
thebox = boxmain;
boxtreeviewlist = createtreeviewlist();
g_object_ref_sink(boxmain);
g_object_ref_sink(boxtreeviewlist);
windowmain = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(windowmain), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(windowmain), WINDOW_DEFAULT_WIDTH_SIZE, WINDOW_DEFAULT_HEIGTH_SIZE);
g_signal_connect(G_OBJECT(windowmain), "delete_event", G_CALLBACK(on_delete_event), NULL);
gtk_container_add(GTK_CONTAINER(windowmain), boxmain);
gtk_widget_show_all(windowmain);
gtk_main();
return TRUE;
}
GtkWidget* createmain()
{
GtkWidget *box = NULL;
GtkWidget *framestatus = NULL;
GtkWidget *framebutton = NULL;
GtkWidget *scroll = NULL;
GtkWidget *textview = NULL;
box = gtk_vbox_new(FALSE, 10);
framestatus = create_frame_status(TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), framestatus, FALSE, TRUE, 0);
scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 0);
textview = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(scroll), textview);
framebutton = main_create_button_box(GTK_BUTTONBOX_EDGE, "");
gtk_box_pack_start(GTK_BOX(box), framebutton, FALSE, TRUE, 0);
return box;
}
GtkWidget* createtreeviewlist()
{
GtkWidget *box = NULL;
GtkWidget *framebutton = NULL;
GtkCellRenderer* renderer = NULL;
GtkTreeViewColumn* column = NULL;
box = gtk_vbox_new(FALSE, 0);
list_store = gtk_list_store_new(STANDARD_LIST_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store));
gtk_box_pack_start(GTK_BOX(box), treeview, TRUE, TRUE, 0);
framebutton = treeviewlist_create_button_box(GTK_BUTTONBOX_EDGE, "");
gtk_box_pack_start(GTK_BOX(box), framebutton, FALSE, TRUE, 0);
return box;
}
void show_box(GtkWidget *box)
{
gtk_container_remove(GTK_CONTAINER(windowmain), thebox);
gtk_container_add(GTK_CONTAINER(windowmain), box);
thebox = box;
gtk_widget_show_all(windowmain);
}
In the Ubuntu10.0.04, it's work well. Use X11.
But in the arm, when show boxmain, in other words,use (show_box) to switch boxtreeviewlist to boxmain, in the windowmain I can look image of boxtreeviewlist. Use Directfb.
Can anybody help me? Thanks.
You never add boxtreeviewlist to a container. Use a GtkVBox or GtkHBox as intermediate container to add two items to a single parent.
Then you can just use gtk_widget_set_visible() to change visibility of the two items.
I have a jailbroken Iphone 4 running iOS 5.0.1 and I'm trying to send serial data through the Dock Connector to my Arduino. I have successful sent data to the Arduino using Minicom but I can't seem to get it working in an app. This is my code and the error is at the bottom.
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#import "ViewController.h"
static struct termios gOriginalTTYAttrs;
static int OpenSerialPort(void);
#implementation ViewController
#synthesize dataLabel, startData;
#synthesize timer;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(IBAction)startSerial:(id)sender{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(targetMethod:) userInfo:nil repeats:YES];
}
static int OpenSerialPort()
{
int fileDescriptor = -1;
int handshake;
struct termios options;
// Open the serial port read/write, with no controlling terminal, and don't wait for a connection.
// The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking.
// See open(2) ("man 2 open") for details.
fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NDELAY);
NSLog(#"%d",fileDescriptor);
if (fileDescriptor == -1)
{
printf("Error opening serial port %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
// unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
// processes.
// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.
if (ioctl(fileDescriptor, TIOCEXCL) == -1)
{
printf("Error setting TIOCEXCL on %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
// See fcntl(2) ("man 2 fcntl") for details.
if (fcntl(fileDescriptor, F_SETFL, 0) == -1)
{
printf("Error clearing O_NONBLOCK %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Get the current options and save them so we can restore the default settings later.
if (tcgetattr(fileDescriptor, &gOriginalTTYAttrs) == -1)
{
printf("Error getting tty attributes %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// The serial port attributes such as timeouts and baud rate are set by modifying the termios
// structure and then calling tcsetattr() to cause the changes to take effect. Note that the
// changes will not become effective without the tcsetattr() call.
// See tcsetattr(4) ("man 4 tcsetattr") for details.
options = gOriginalTTYAttrs;
// Print the current input and output baud rates.
// See tcsetattr(4) ("man 4 tcsetattr") for details.
printf("Current input baud rate is %d\n", (int) cfgetispeed(&options));
printf("Current output baud rate is %d\n", (int) cfgetospeed(&options));
// Set raw input (non-canonical) mode, with reads blocking until either a single character
// has been received or a one second timeout expires.
// See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.
cfmakeraw(&options);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 10;
// The baud rate, word length, and handshake options can be set as follows:
cfsetspeed(&options, B19200); // Set 19200 baud
options.c_cflag |= (CS8); // RTS flow control of input
printf("Input baud rate changed to %d\n", (int) cfgetispeed(&options));
printf("Output baud rate changed to %d\n", (int) cfgetospeed(&options));
// Cause the new options to take effect immediately.
if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1)
{
printf("Error setting tty attributes %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Success
return fileDescriptor;
// Failure "/dev/tty.iap"
error:
if (fileDescriptor != -1)
{
close(fileDescriptor);
}
return -1;
}
-(void) targetMethod: (NSTimer *) theTimer {
//dataLabel.text = #"timer running";
int fd;
char somechar[8];
fd=OpenSerialPort(); // Open tty.iap with no hardware control, 8 bit, BLOCKING and at 19200 baud
NSLog(#"%d",fd);
dataLabel.text = [NSString stringWithFormat:#"%d",fd];
if(fd>-1)
{
// dataLabel.text = #"got port";
write(fd,"*",1); // Write handshaking message over serial
///////////////////////////////////////////////////////////////////////////////////////////////////
// After this, our device or our PC program should be strobing serial ground to gain access to the Iphone Serial Line
//////////////////////////////////////////////////////////////////////////////////////////////////
read(fd,&somechar[0],1); // Read 1 byte over serial. This will block (wait) untill the byte has been received
if(somechar[0]=='*') // Check if this byte is a "handshaking" message
{
dataLabel.text = #"handshake accepted";
printf("Serial connection established!\n"); // If it is, we have established a connection to the device and can freely read/write over serial!
while(true) // Do this forever or untill someone presses CTRL+C
{
dataLabel.text = #"in the loop";
read(fd,&somechar[0],1); // Read a character over serial!
dataLabel.text = [NSString stringWithFormat:#"%#",somechar[0]]; // Write the character to the Terminal!!
}
}
}
}
#end
But the error i'm getting is:
2012-03-01 19:58:03.507 Iphone-Serial[2470:707] -1
Error opening serial port /dev/tty.iap - Resource busy(16).
Should I restart my phone? or somehow close the serial port?
Thanks,
Andrew
Your app must have root permission to access tty.iap. Simply ssh to your device(as root) and chmod 777 YourApp.app may fix the problem. Just start with simpler code, get response and extend more and more.
I have a pop-up window (created using the WINDOW_POPUP type) which contains some widgets on it, including a text entry. The problem is that the entry doesn't get the focus when I click on it, so I can't type anything. Is there any flag I have to set to allow the window to get the keyboard focus?
You can not use WINDOW_POPUP for gtk-windows that require the focus. Instead you should use a GtkWindow with type GTK_WINDOW_TOPLEVEL and call the next functions (or methods)
GtkWindow *result = g_object_new(GTK_TYPE_WINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
gtk_widget_set_can_focus(result, TRUE);
gtk_window_set_decorated(GTK_WINDOW(result), FALSE);
gtk_window_set_type_hint(GTK_WINDOW(result), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
gtk_window_set_transient_for(GTK_WINDOW(result), main_top_level_window);
This worked for me ... unfortunately the icon in the window-list blinks short when this 'popup' is destroyed
Despite the previous answers and the GTK Reference, it is possible to grab the keyboard focus when using a GTK_WINDOW_POPUP. You need to connect to the "show" event...
GtkWindow *w = gtk_window_new(GTK_WINDOW_POPUP);
g_signal_connect(G_OBJECT(w), "show", G_CALLBACK(on_window_show), NULL);
... with a callback that tries to grab the keyboard:
static void on_window_show(GtkWidget *w, gpointer user_data) {
/* grabbing might not succeed immediately... */
while (gdk_keyboard_grab(w->window, FALSE, GDK_CURRENT_TIME) != GDK_GRAB_SUCCESS) {
/* ...wait a while and try again */
sleep(0.1);
}
}
That works for me pretty well.
#include <gtk/gtk.h>
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
gtk_main_quit ();
return TRUE;
}
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *windowpopup;
GtkWidget *button;
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
windowpopup = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_transient_for(GTK_WINDOW(windowpopup),GTK_WINDOW(window));
gtk_window_set_destroy_with_parent(GTK_WINDOW(windowpopup),TRUE);
gtk_widget_show (windowpopup);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
/* Creates a new button with the label "Hello World". */
button = gtk_button_new_with_label ("Hello World");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (window));
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}