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
|
#include <stdlib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>
void fonctionharmonique(GtkSpinButton *spin_button, GtkWidget *pTextview)
{
gchar* sLabel;
gfloat Value;
/* Récupération du buffer du GtkTextView */
GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (pTextview));
/* Effacement complet du buffer */
GtkTextIter start ,end;
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_delete (buffer, &start, &end);
/* Récupération de la valeur de la scrollbar */
Value = (float)gtk_spin_button_get_value (spin_button);
float harmonique;
/* Création des différents labels */
gint factor;
for (factor=1; factor <= 1048576; factor*=2, harmonique=Value*factor)
{
sLabel = g_strdup_printf("%f Hz %f hz\n", harmonique, harmonique*1.5);
/* insertion du texte la position du curseur */
gtk_text_buffer_insert_at_cursor (buffer, sLabel, -1);
g_free (sLabel);
}
}
void connection(GtkWidget *pWidget, gpointer data)
{
int fd;
char c;
gfloat iValue;
iValue = gtk_range_get_value(GTK_RANGE(pWidget));
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 */
char buf[10] = "";
sprintf(buf, "%f", iValue);
write(fd,&buf,sizeof(buf));
printf("%s a bien etait envoyer\n", buf); // confirmation
/* Fermeture */
close(fd);
exit(0);
}
int main(int argc,char **argv)
{
GtkWidget* pWindow;
GtkWidget *pMainVBox;
GtkWidget *pFrame;
GtkWidget *pLabel;
GtkWidget *pScale;
GtkWidget *pButton;
GtkWidget *pSpin;
gtk_init(&argc,&argv);
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(pWindow), "Moppa 3.0");
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("test bouton 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);
GtkWidget *pScroll = gtk_scrolled_window_new (NULL, NULL);
gtk_box_pack_start(GTK_BOX(pMainVBox), pScroll, FALSE, TRUE, 0);
gtk_widget_set_size_request (pScroll, 200, 200);
GtkWidget *pTextview = gtk_text_view_new ();
gtk_container_add (GTK_CONTAINER(pScroll), pTextview);
/* 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, 20000000, 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(fonctionharmonique), pTextview);
pFrame = gtk_frame_new("X");
/* Création du widget GtkHScale */
pScale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL,-361 , -1, 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(connection), (GtkWidget*)pLabel);
pFrame = gtk_frame_new("Y");
pScale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL,1 , 201, 0.5);
/* Position du label en dessous */
gtk_scale_set_value_pos(GTK_SCALE(pScale), 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(connection), (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