1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// gcc interface.c -o test `pkg-config --cflags --libs gtk+-3.0`
#include <stdlib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>
void OnScrollbarChange(GtkWidget *pWidget, gpointer data)
{
gchar* sLabel;
gfloat iValue;
/* Récupération de la valeur de la scrollbar */
iValue = gtk_range_get_value(GTK_RANGE(pWidget));
/* Création du nouveau label */
sLabel = g_strdup_printf("%f", iValue);
/* Modification du label */
gtk_label_set_text(GTK_LABEL(data), sLabel);
/* Liberation memoire */
g_free(sLabel);
}
void connection(void)
{
int fd;
char c;
struct termios termios_p;
/* Ouverture de la liaison serie */
if ( (fd=open("/dev/ttyUSB0",O_RDWR)) == -1 ) {
perror("open");
exit(-1);
}
/* Lecture des parametres courants */
tcgetattr(fd,&termios_p);
/* On ignore les BREAK et les caracteres avec erreurs de parite */
termios_p.c_iflag = IGNBRK | IGNPAR;
/* Pas de mode de sortie particulier */
termios_p.c_oflag = 0;
/* Liaison a 9600 bps avec 7 bits de donnees et une parite paire */
termios_p.c_cflag = B9600 | CS7 | PARENB;
/* Mode non-canonique avec echo */
termios_p.c_lflag = ECHO;
/* Caracteres immediatement disponibles */
termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
/* Sauvegarde des nouveaux parametres */
tcsetattr(fd,TCSANOW,&termios_p);
/* Boucle d'écriture */
while ( 1 ) {
write(fd,"Tapez Ctrl-C pour quitter\n",26);
if ( c == 0x03 ) /* Ctrl-C */
break;
printf("%03u %02x %c\n",c&0xff,c&0xff,c);
}
/* Fermeture */
close(fd);
/* Bye... */
exit(0);
}
int main(int argc,char **argv)
{
GtkWidget* pWindow;
GtkWidget *pMainVBox;
GtkWidget *pFrame;
GtkWidget *pLabel;
GtkWidget *pScale;
GtkWidget *pScale2;
GtkWidget *pEntry;
GtkWidget *pButton;
GtkWidget *pSpin;
GtkWidget *pToggleBtn;
gchar *sLabel;
GtkWidget *Adjust;
int x;
int y;
long harmonique;
gtk_init(&argc,&argv);
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(pWindow), "moppa");
gtk_window_set_default_size(GTK_WINDOW(pWindow), 200, 400);
gtk_container_set_border_width(GTK_CONTAINER(pWindow), 4);
pMainVBox = gtk_box_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(pWindow), pMainVBox);
pButton = gtk_button_new_with_label("Connection");
gtk_box_pack_start(GTK_BOX(pMainVBox), pButton, FALSE, TRUE, 0);
pLabel = gtk_label_new(NULL);
gtk_box_pack_start(GTK_BOX(pMainVBox), pLabel, FALSE, FALSE, 0);
/* Connexion du signal "clicked" pour ouvrir la boite de dialogue */
g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(connection), NULL);
pFrame = gtk_frame_new("Harmonique en Hz");
pSpin = gtk_spin_button_new_with_range(0, 1000000000000, 0.0001);
gtk_container_add(GTK_CONTAINER(pFrame), pSpin);
gtk_box_pack_start(GTK_BOX(pMainVBox), pFrame, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(pSpin), "value-changed",
G_CALLBACK(OnScrollbarChange), (GtkWidget*)pLabel);
pEntry = gtk_entry_new();
// gtk_box_pack_start(GTK_BOX(pVBox), pEntry, TRUE, FALSE, 0);
// pEntry = gtk_entry_new_with_max_length(8);
pFrame = gtk_frame_new("X");
/* Création du widget GtkHScale */
pScale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL,x , 360, 0.5);
gtk_container_add(GTK_CONTAINER(pFrame), pScale);
gtk_box_pack_start(GTK_BOX(pMainVBox), pFrame, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(pScale), "value-changed",
G_CALLBACK(OnScrollbarChange), (GtkWidget*)pLabel);
pFrame = gtk_frame_new("Y");
pScale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL,y , 200, 0.5);
/* Position du label en dessous */
gtk_scale_set_value_pos(GTK_SCALE(pScale2), GTK_POS_BOTTOM);
gtk_container_add(GTK_CONTAINER(pFrame), pScale);
gtk_box_pack_start(GTK_BOX(pMainVBox), pFrame, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(pScale), "value-changed",
G_CALLBACK(OnScrollbarChange), (GtkWidget*)pLabel);
gtk_widget_show_all(pWindow);
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return EXIT_SUCCESS;
} |
Partager