🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: nl.comp.programmeren
Article #706

Re: GTK programming en parallelle poort sturing

#706
From: Dave Mertens
Date: Thu, 18 Nov 1999 00:00
396 lines
12803 bytes
Gefeliciteerd, je bent de voorronde van de 'langste posting van het'
doorgekomen. Shit...  11 KB

Bart Vandewoestyne <Bart.Vandewoestyne@skynet.be> wrote in message
news:38346CD5.4FB482B2@skynet.be...
> Ik zit met volgend probleem:
>
> Ik wil een stappenmotorsturing schrijven met GTK en heb momenteel al een
> 'basis-grafische omgeving' met menu's en submenus.  Aan ��n van die
> submenu's wil ik volgende functie toekennen:
>
> void turn_left_half_step(int *status)
> {
>  set_left_half_step(status);
>  turn(status);
>  unset_left_half_step(status);
> }
>
> *status is een pointer naar een integer waarde die de status van de
> parallelle poort bijhoudt. In de functie set_left_half_step(status) zet
> ik de nodige bits om de motor te doen draaien in half step mode in de
> linker draaizin.  Daarna via turn(status) laat ik de clock een paar
> clockpulsen genereren zodat de motor een aantal clockpulsen 'draait' en
> daarna ga maak ik de parallelle poort weer 'proper' via
> unset_left_half_step(status).
> Nu wil ik deze functie toekennen aan een submenu dat ik via GTK al heb
> gecre�eerd.  Dit gebeurt normaal via gtk_signal_connect:
>
> gtk_signal_connect (GTK_OBJECT (menuMotorTurnLeftFull), "activate",
>   GTK_SIGNAL_FUNC (turn_left_full_step), ????);
>
> Mijn probleem is nu het volgende: hoe geef ik hier nu in godsnaam de
> variabelen (int *status) mee aan de functie turn_left_full_step?  Mijn
> functie heeft variabelen nodig en als ik schrijf:
>
> gtk_signal_connect (GTK_OBJECT (menuMotorTurnLeftFull), "activate",
>   GTK_SIGNAL_FUNC (turn_left_full_step), portstatus);
>
> dan lukt dit al geen kanten :-(
> portstatus is in het main programma als volgt gedefinieerd:
>
> int *portstatus;  // Can I use an int here or does it have to be gint
> ???
>
> Hoe geef ik nu in godsnaam deze pointer mee aan de functie
> turn_left_full_step(int *status) die ge-gtk_signal_connected is aan een
> submenu ???
>
> Ben je verplicht om je in GTK aan de types van GTK te houden (zoals
> gint, gpointer etc...) of zou mijn
> gewone int ook moeten lukken ?  Of moet ik proberen te werken met
> functies die geen argumenten vereisen (void) dus... ?
>
> I definately need help here...
>
> Hieronder volgt de source van mijn programma tot nu toe (alle commentaar
> wat betreft programmeerstijl is welkom!  Zie jij betere algoritmes of
> kan ik zaken verkorten door via functies zaken te veralgemenen, zeg het
> dan!:
>
>
> de file motor.h:
> #include <gtk/gtk.h>
> #include <asm/io.h>
> #include <unistd.h>
> #include <stdio.h>
>
> #define BASEPORT 0x378
> #define PULSE_WIDTH 100000  // in microseconds ???
>
> #define LEFT 1
> #define RIGHT 0
> #define CLOCK 2
> #define HALF_STEP 4
> #define FULL_STEP 0
> #define RESET 8
> #define ENABLE 16
> #define CONTROL 32
>
> void initialize(int *status);
>
> void set_left_half_step(int *status);
> void unset_left_half_step(int *status);
> void set_right_half_step(int *status);
> void unset_right_half_step(int *status);
> void set_left_full_step(int *status);
> void unset_left_full_step(int *status);
> void set_right_full_step(int *status);
> void unset_right_full_step(int *status);
>
> void turn(int *status);
>
> void turn_left_half_step(int *status);
> void turn_left_full_step(int *status);
> void turn_right_half_step(int *status);
> void turn_right_full_step(int *status);
>
> gint CloseAppWindow (GtkWidget *widget, int *data);
>
> void NewFile (void);
>
>
> -->>> de file motor.c:
>
> #include "motor.h"
>
> /*
>  * motor.c
>  *
>  * Program to control steppermotor by Bart Vandewoestyne
>  * Compile as user with:
>  * gcc -Wall -O1 -g motor.c control.c -o motor `gtk-config --cflags`
> `gtk-config --libs`
>  *
>  * To be able to run as user do as root:
>  * # chown root ~user/place_to_be/motor
>  * # chmod ug+s ~user/place_to_be/motor
>  */
>
> gint CloseAppWindow (GtkWidget *widget, int *data)
> {
>  gtk_main_quit();
>
>  /* --- Ok to close --- */
>  return (FALSE);
> }
>
> void NewFile (void)
> {
>  g_print("New File\n");
> }
>
> int main (int argc, char *argv[])
> {
>  GtkWidget *window;
>  GtkWidget *vbox_main;
>  GtkWidget *menubar;
>  GtkWidget *menuFile;
>  GtkWidget *menuFileSub;
>  GtkWidget *menuFileOpen;
>  GtkWidget *menuFileNew;
>  GtkWidget *menuFileSave;
>  GtkWidget *menuFileSaveAs;
>  GtkWidget *menuL297;
>  GtkWidget *menuMotor;
>  GtkWidget *menuMotorSub;
>  GtkWidget *menuMotorTurnLeftHalf;
>  GtkWidget *menuMotorTurnLeftFull;
>  GtkWidget *menuMotorTurnRightHalf;
>  GtkWidget *menuMotorTurnRightFull;
>  GtkWidget *menuHelp;
>
>  int *portstatus;  // Can I use an int here or does it have to be gint
> ???
>
>  if (ioperm(BASEPORT, 3, 1)) { perror("ioperm"); exit(1);}
>
>  initialize(portstatus);
>
>  g_print("%d\n", *portstatus);  // to be able to see the portstatus
> after
>   // initializing
>
>  /* --- GTK initialization --- */
>  gtk_init (&argc, &argv);
>
>  /* Create the top-level window --- */
>  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
>
>  /* --- Set the title --- */
>  gtk_window_set_title (GTK_WINDOW (window), "Steppermotorcontroller by
> Bart Vandewoestyne");
>
>  /* --- Set the size --- */
>  gtk_widget_set_usize (window, 400, 300);
>
>  /* --- Connect the delete_event to the main window */
>  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
>      GTK_SIGNAL_FUNC (CloseAppWindow), NULL);
>
>  /* --- Give the window a border --- */
>  gtk_container_border_width (GTK_CONTAINER (window), 0);
>
>  /* --- Create the application_s vertical packing box --- */
>  vbox_main = gtk_vbox_new (FALSE, 0);
>
>  /* --- Add the packing box to the main window --- */
>  gtk_container_add (GTK_CONTAINER (window), vbox_main);
>
>  /* --- Create a menu bar for the application --- */
>  menubar = gtk_menu_bar_new();
>
>  /* --- Put the menu bar in the application packing box --- */
>  gtk_box_pack_start (GTK_BOX(vbox_main), menubar, FALSE, TRUE, 0);
>
>  /* --- Create a menu item for "File" --- */
>  menuFile = gtk_menu_item_new_with_label ("File");
>
>  /* --- Add File to the menu bar --- */
>  gtk_menu_bar_append (GTK_MENU_BAR (menubar), menuFile);
>
>  /* --- Create Submenu for menuFile --- */
>  menuFileSub = gtk_menu_new ();
>
>  /* --- Mark the submenu under the "File" menuitem --- */
>  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuFile), menuFileSub);
>
>  menuFileNew = gtk_menu_item_new_with_label ("New");
>  gtk_menu_append (GTK_MENU (menuFileSub), menuFileNew);
>  gtk_signal_connect (GTK_OBJECT (menuFileNew), "activate",
> GTK_SIGNAL_FUNC(NewFile), NULL);
>  gtk_widget_show (menuFileNew);
>
>  menuFileOpen = gtk_menu_item_new_with_label ("Open");
>  gtk_menu_append (GTK_MENU (menuFileSub), menuFileOpen);
>  gtk_widget_show (menuFileOpen);
>
>  menuFileSave = gtk_menu_item_new_with_label ("Save");
>  gtk_menu_append (GTK_MENU (menuFileSub), menuFileSave);
>  gtk_widget_show (menuFileSave);
>
>  menuFileSaveAs = gtk_menu_item_new_with_label ("Save As...");
>  gtk_menu_append (GTK_MENU (menuFileSub), menuFileSaveAs);
>  gtk_widget_show (menuFileSaveAs);
>
>  /* -- Create a menu item for "L297 settings" --- */
>  menuL297 = gtk_menu_item_new_with_label ("L297 Settings");
>
>  /* --- Add L297 to the menu bar --- */
>  gtk_menu_bar_append (GTK_MENU_BAR (menubar), menuL297);
>
>  menuMotor = gtk_menu_item_new_with_label ("Motor");
>  gtk_menu_bar_append (GTK_MENU_BAR (menubar), menuMotor);
>
>  menuMotorSub = gtk_menu_new ();
>  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuMotor), menuMotorSub);
>
>  menuMotorTurnLeftHalf = gtk_menu_item_new_with_label ("Turn Left Half
> Step");
>  gtk_menu_append (GTK_MENU (menuMotorSub),menuMotorTurnLeftHalf);
>  gtk_signal_connect (GTK_OBJECT (menuMotorTurnLeftHalf), "activate",
>   GTK_SIGNAL_FUNC (turn_left_half_step), portstatus);
>  gtk_widget_show (menuMotorTurnLeftHalf);
>
>  menuMotorTurnLeftFull = gtk_menu_item_new_with_label ("Turn Left Full
> Step");
>  gtk_menu_append (GTK_MENU (menuMotorSub),menuMotorTurnLeftFull);
>  gtk_signal_connect (GTK_OBJECT (menuMotorTurnLeftFull), "activate",
>   GTK_SIGNAL_FUNC (turn_left_full_step), portstatus);
>  gtk_widget_show (menuMotorTurnLeftFull);
>
>  menuMotorTurnRightHalf = gtk_menu_item_new_with_label ("Turn Right Half
> Step");
>  gtk_menu_append (GTK_MENU (menuMotorSub),menuMotorTurnRightHalf);
>  gtk_signal_connect (GTK_OBJECT (menuMotorTurnRightHalf), "activate",
>   GTK_SIGNAL_FUNC (turn_right_half_step), portstatus);
>  gtk_widget_show (menuMotorTurnRightHalf);
>
>  menuMotorTurnRightFull = gtk_menu_item_new_with_label ("Turn Right Full
> Step");
>  gtk_menu_append (GTK_MENU (menuMotorSub),menuMotorTurnRightFull);
>  gtk_signal_connect (GTK_OBJECT (menuMotorTurnRightFull), "activate",
>   GTK_SIGNAL_FUNC (turn_right_full_step), portstatus);
>  gtk_widget_show (menuMotorTurnRightFull);
>
>  menuHelp = gtk_menu_item_new_with_label ("Help");
>  gtk_menu_bar_append (GTK_MENU_BAR (menubar), menuHelp);
>
>  /* --- Make the widget visible --- */
>  gtk_widget_show (menuFile);
>  gtk_widget_show (menuL297);
>  gtk_widget_show (menuMotor);
>  gtk_widget_show (menuHelp);
>
>  /* --- Make the menu bar visible --- */
>  gtk_widget_show (menubar);
>
>  /* --- Make the packing box visible --- */
>  gtk_widget_show (vbox_main);
>
>
>  /* --- Make the main window visible, now the button
>   *     will be made visible.
>   */
>  gtk_widget_show (window);
>
>  gtk_main();
>  return(0);
> }
>
> --->>> de file control.c:
>
> #include "motor.h"
>
> void initialize(int *status)
> {
>  outb(0, BASEPORT);
>  outb(0x0B, BASEPORT+2);
>  *status=0;
> }
>
> void set_left_half_step(int *status)
> {
>  *status=*status+LEFT+HALF_STEP+ENABLE+RESET;
> }
>
> void unset_left_half_step(int *status)
> {
>  *status=*status-LEFT-HALF_STEP-ENABLE-RESET;
> }
>
> void set_right_half_step(int *status)
> {
>  *status=*status+RIGHT+HALF_STEP+ENABLE+RESET;
> }
>
> void unset_right_half_step(int *status)
> {
>  *status=*status-RIGHT-HALF_STEP-ENABLE-RESET;
> }
>
> void set_left_full_step(int *status)
> {
>  *status=*status+LEFT+FULL_STEP+ENABLE+RESET;
> }
>
> void unset_left_full_step(int *status)
> {
>  *status=*status-LEFT-FULL_STEP-ENABLE-RESET;
> }
>
> void set_right_full_step(int *status)
> {
>  *status=*status+RIGHT+FULL_STEP+ENABLE+RESET;
> }
>
> void unset_right_full_step(int *status)
> {
>  *status=*status-RIGHT-FULL_STEP-ENABLE-RESET;
> }
>
> void turn(int *status)
> {
>  int i=0;
>
>  g_print("%d\n", *status);
>  for (i=0; i<10; i++) {
>    *status = *status+CLOCK;
>    outb(*status, BASEPORT);
>    usleep(PULSE_WIDTH);
>    *status = *status-CLOCK;
>    outb(*status, BASEPORT);
>    usleep(PULSE_WIDTH); }
> }
>
> void turn_left_half_step(int *status)
> {
>  set_left_half_step(status);
>  turn(status);
>  unset_left_half_step(status);
> }
>
> void turn_left_full_step(int *status)
> {
>  set_left_full_step(status);
>  turn(status);
>  unset_left_full_step(status);
> }
>
> void turn_right_half_step(int *status)
> {
>  set_right_half_step(status);
>  turn(status);
>  unset_right_half_step(status);
> }
>
> void turn_right_full_step(int *status)
> {
>  set_right_full_step(status);
>  turn(status);
>  unset_right_full_step(status);
> }
>
>
>
> --
> Bart Vandewoestyne http://hello.to/MC303
> Hugo Verrieststraat 48 Bart.Vandewoestyne@skynet.be
> 8550 ZWEVEGEM ICQ# 21275340
> BELGIUM - EUROPE nick: MC303
> Phone: +32(0)56/75.48.11
> -------------------------------------------------------------------
> If carpenters made buildings the way programmers make programs, the
> first woodpecker to come along would destroy all of civilization.
> - Weinberg's Second Law -
>
Technical Details
Message-ID: <38347716$0$11594@news2.zeelandnet.nl>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38346CD5.4FB482B2@skynet.be>

Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII
X-Google-Thread: 100a19,10906ecdbe863be8
X-Google-Attributes: gid100a19,public
X-Google-Thread: 107324,10906ecdbe863be8
X-Google-Attributes: gid107324,public
X-Google-Thread: fcea4,10906ecdbe863be8
X-Google-Attributes: gidfcea4,public
X-Google-Thread: fb7d6,10906ecdbe863be8
X-Google-Attributes: gidfb7d6,public
From: "Dave Mertens" <davemertens@zeelandnet.nl>
Subject: Re: GTK programming en parallelle poort sturing
Date: 1999/11/18
Message-ID: <38347716$0$11594@news2.zeelandnet.nl>
X-Deja-AN: 550290873
References: <38346CD5.4FB482B2@skynet.be>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
X-Trace: dreaderd.zeelandnet 942962454 11594 212.92.75.38
X-MSMail-Priority: Normal
Newsgroups: nl.comp.programmeren,be.comp.os.linux,nl.comp.os.linux,be.comp.programming