Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!zaphod.mps.ohio-state.edu!uwm.edu!csd4.csd.uwm.edu!trantow From: trantow@csd4.csd.uwm.edu (Jerry J Trantow) Newsgroups: comp.sys.amiga.programmer Subject: Re: General questions regarding Tasks Keywords: tasks help questions seeking Message-ID: <12470@uwm.edu> Date: 28 May 91 02:29:55 GMT References: <1991May24.160440.374@m.cs.uiuc.edu> Sender: news@uwm.edu Organization: University of Wisconsin - Milwaukee Lines: 210 In article <1991May24.160440.374@m.cs.uiuc.edu> schwager@m.cs.uiuc.edu (Michael Schwager) writes: > >Hi, >I'm looking for guidelines about using tasks within C programs. C code >examples would help. I've got Aztec C 5.0d, and their little beep.c >example only goes so far. For example, I wonder: Can I call other >-Mike Schwager | Machine: Amiga 500, 3 MB RAM/30 HD I would be very wary of the beep.c example if it is the original by Sam Dicker. I looked at that about 1 1/2 years ago and found several problems with it. I don't recall all of the problems, but here is my modified version of it. I used this as a starting point for a program using a task to animate a pointer and I have what I feel is a much better task example, but I'm still hoping it will show up in AC. (Submitted 1 year ago in July, accepted in December, and I'M STILL WAITING!) In the meantime, here is my modified beep.c. /***************************************************************************** * VT100-like Beep Sound Example 22 February 1986 * Copyright (c) 1986 Sam Dicker, Commodore/Amiga Inc. 25 Jan 90 Heavily hacked by JJT since Sam Dicker programs for shit * * Calling VTBeep() starts the sound which stops when it times out. * Calling it again, while it is playing, only restarts the timer. * Call KillVTBeep() to kill any beeps in progress before exiting your * main program. *****************************************************************************/ #include "functions.h" #include "exec/types.h" #include "exec/errors.h" #include "exec/memory.he "devices/timer.h" #include "devices/audio.h" #include "libraries/dos.h" #define BEEPNAME "JJTBeep" #define BEEPSIZE 10L #define BEEPFREQ 1000L #define COLORCLOCK 3579545L #define SIGB_BEEP 31L #define SIGF_BEEP (ULONG)(1L << 31L) #define SIGF_PORT (ULONG)(1L << replyPort->mp_SigBit) #define NOT_DONE (LONG)(FALSE) /* useful for CheckIO() */ #defONG CreatePort(); */ /* channel allocation map (try left channel; if unavailable try right) */ UBYTE allocationMap[] = { 1, 8, 2, 4 }; /* make beep sound */ LONG VTBeep() { struct Task *beepTCB; VOID beepTask(); Forbid(); if ((beepTCB = (struct Task *)FindTask(BEEPNAME))== NULL) { beepTCB = (struct Task *)CreateTask(BEEPNAME, 0L,(LONG)beepTask,500L); /* printf("Spawned\n"); */ } else { Signal((struct Task *)beepTCB,SIGF_BEEP); /* printf("Signalled\n"); */ } Permit(); return((LONG)(beepTCB != NULL)); } /* kill any beep sounds in progress. This is necessary before exiting the * main program; otherwise, if a beep is playing, when the beep times out * and the t may be gone */ LONG KillVTBeep() { struct Task *beepTCB; do { Forbid(); if ((beepTCB = (struct Task *)FindTask((char *)BEEPNAME)) != NULL) { Signal((struct Task *)beepTCB,(LONG)SIGBREAKF_CTRL_C); /* printf("Kill the task\n"); */ Delay(10L); } Permit(); } while (beepTCB != NULL); /* if it existed, look for it again (Another copy perhaps)*/ } /* beep sound child task */ VOID beepTask() { struct MsgPort *replyPort; struct timerequest *timerIOB; struct IOAudio *audioIOB; UBYTE *beepWave; struct Message *myIO; ULONG signals; geta4(); if ((LONG)AllocSignal(SIGB_BEEP) == SIGB_BEEP) { replyPort = (struct MsgPort *)CreatePort((char *)NULL,0L); if (replyPort != NULL) { timerIOB = (structt *)CreateExtIO((APTR)replyPort,(LONG)sizeof(struct timerequest)); if (timerIOB != NULL) { if ((LONG)OpenDevice((char *)TIMERNAME,(LONG)UNIT_VBLANK,(APTR)timerIOB,0L) == 0L) { timerIOB->tr_node.io_Command = TR_ADDREQUEST; beepWave = (UBYTE *)AllocMem((LONG)BEEPSIZE,(LONG)MEMF_CHIP|MEMF_CLEAR); if (beepWave != NULL) { beepWave[0] = 127; beepWave[1] =-128; audioIOB=(struct IOAudio *)CreateExtIO((struct MsgPort *)replyPort,(LONG)sizeof(struct IOAudio)); if (audioIOB != NULL) { audioIOB->ioa_Request.io_Message.mn_Node.ln_Pri =85; audioIOB->ioa_Data = allocationMap; audioIOB->ioa_Length = sizeof(allocationMap); /* allocate */ if ((LONG)OpenDevice(AUDIONAME, 0L, audioIOB, 0L) == 0L) { audioIOB->ioa_Request.io_Command = CMD_WRITE; audioIOB->ioa_Request.io_Flags = ADIOF_PERVOL; audioIOB->ioa_Data = beepWave; audioIOB->ioa_Length = BEEPSIZE; audioIOB->ioa_Period =COLORCLOCK / (BEEPSIZE * BEEPFREQ); audioIOB->ioa_Volume = 64; audioIOB->ioa_Cycles=0; timerIOB->tr_time.tv_secs = 0; timerIOB->tr_time.tv_micro = 1L; SendIO(timerIOB); /* initializes timer IO */ do { Forbid(); signals=(LONG)Wait((LONG)(SIGBREAKF_CTRL_C|SIGF_BE GetMsg(replyPort); /* doesn't matter what order they come off */ AbortIO(audioIOB); GetMsg(replyPort); } } else { if ((signals&SIGF_BEEP)!=0L) /* start or restart a beep */ { if (CheckIO(tim* these values get destroyed */ timerIOB->tr_time.tv_micro = 250000L; SendIO(timerIOB); /* start a new timer */ } else /* Signal came from the ReplyPort */ { /* timerIOB returned */ myIO=(struct Message *)CheckIO(timerIOB); if (myIO != NULL) { Remove(replyPort->mp_MsgList,myIO); AbortIO(audioIOB); } else /* audioIOB returned */ { AbortIO(timerIOB); GetMsg(replyPort); } } } Permit(); } while ((signals & SIGBREAKF_CTRL_C) == 0L); /* clean up */ /* closing the audio device kills the sound * and frees the channels */ CloseDevice(audioIOB); } DeleteExtIO(audioIOB); } FreeMem((UBYTE *)beepWave, BEEPSIZE); } CloseDevice(timerIOB); } DeleteExtIO(timerIOB); } DeletePort(replyPort); } } } main() { VTBeep(); VTBeep(); KillVTBeep(); } Jerry J. Trantow | I swear by my life and my love of it, 8967 N. Pelham Parkway | that I will never live for the sake of another man, Milwaukee, Wi 53217-1954 | nor ask another man to live for mine. (414) 228-4689 | Ayn Rand _____________________________________________________________________________