home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
devices
/
trackdiskdevice
/
example1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-27
|
5KB
|
178 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Trackdisk Device Tulevagen 22 */
/* File: Example1.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-27 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This program will use the Trackdisk Device to turn on */
/* and off the internal diskdrive's motor. */
#include <exec/types.h> /* STRPTR */
#include <exec/ports.h> /* struct Message */
#include <exec/nodes.h> /* NT_MESSAGE */
#include <devices/trackdisk.h> /* Trackdisk Device */
/* Diskdrive: */
#define DF0 0
#define DF1 1
#define DF2 2
#define DF3 3
/* Motor status: */
#define OFF 0
#define ON 1
/* Declare a pointer to our reply port: */
struct MsgPort *replymp;
/* Declare a pointer to a IOStdReq structure: */
/* (We do not use the extended IOExtTD structure.) */
struct IOStdReq *req;
/* Have we opened the Trackdisk Device? */
BOOL error = TRUE;
/* Declare our functions: */
void main();
void clean_up( STRPTR text );
BYTE DiskMotor( BYTE status );
void main()
{
/* Was the motor already on or not? */
BYTE motor_was_on;
/* Get a reply port: */
replymp = (struct MsgPort *)
CreatePort( NULL, 0 );
if( !replymp )
clean_up( "Could not create the reply port!" );
/* Create an IOStdReq structure: */
req = (struct IOStdReq *)
CreateStdIO( replymp );
if( !req )
clean_up( "Could not create the IO!" );
/* Open the Trackdisk Device: */
error = OpenDevice( TD_NAME, DF0, req, 0 );
if( error )
clean_up( "Could not open the Trackdisk Device!" );
/* Turn the motor on: */
motor_was_on = DiskMotor( ON );
/* Only if the motor was off before we turned it on should */
/* we turn it off again. If the motor was already on we */
/* should not turn it off since that means that some other */
/* task wants it to be on. */
if( !motor_was_on )
DiskMotor( OFF );
/* Clean up and quit: */
clean_up( "The End!" );
}
/* Close and return everything that has been */
/* opened and allocated before we quit: */
void clean_up( STRPTR text )
{
/* Close the Trackdisk Device: */
if( !error )
CloseDevice( req );
/* Delete the IOStdReq structure: */
if( req )
DeleteStdIO( req, sizeof( struct IOStdReq ) );
/* Remove the replyport: */
if( replymp )
DeletePort( replymp);
/* Print the message: */
printf( "%s\n", text );
/* Quit: */
exit( 0 );
}
/* DiskMotor() will turn on/off the diskdrive's motor. */
/* */
/* Synopsis: oldstatus = DiskMotor( new_status ); */
/* */
/* oldstatus: (BYTE) DiskMotor() returns 1 if the motor */
/* was already on, else 0 (motor was off). */
/* */
/* newstatus: (BYTE) Set this field to 1 (ON) if you */
/* want to turn the motor on, else set it to */
/* 0 (OFF) and the motor will be turned off. */
/* Note! If you turned the motor on when the */
/* motor was off you must turn it off before */
/* your program terminates. However, if it */
/* was already on you should not turn it */
/* off. */
BYTE DiskMotor( BYTE status )
{
/* Will the user what we will do: */
printf( "Motor will be turned: %s\n", status ? "On" : "Off" );
/* Set our request: */
req->io_Command = TD_MOTOR; /* Turn motor on/off. */
req->io_Length = status; /* 0 = off, 1 = on. */
/* Do our request, and return when done: */
DoIO( req );
/* DoIO() will return when it has done our request, */
/* so it will therefore not send us any message to */
/* our reply port. If we had used SendIO() instead */
/* (returns immediately) we would had to wait for a */
/* message to arrive at our reply port. */
/* Check if the motor was on or not before we changed it: */
printf( "Motor was: %s\n", req->io_Actual ? "On" : "Off" );
/* Return 1 if the motor was already on, 0 if not: */
return( (BYTE) req->io_Actual );
}