summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/mktone.c
blob: 427bc5a69646dcbbba8cf624af8edcbd24a54400 (plain)
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
/*
**  MKTONE.C
**
**  Original Copyright 1988-1991 by Bob Stout as part of
**  the MicroFirm Function Library (MFL)
**
**  This subset version is functionally identical to the
**  version originally published by the author in Tech Specialist
**  magazine and is hereby donated to the public domain.
*/

#include "uclock.h"
#include "sound.h"

static int usec_timeout(uclock_t start, uclock_t finish, uclock_t usecs)
{
      if (usecs >= (finish - start))
            return 0;
      else  return 1;
}

void dosound(int freq)
{
      unsigned i;

      outp(C8253, SETIMER);
      i = (unsigned)freq%256;
      outp(F8253, i);
      i = (unsigned)freq/256;
      outp(F8253, i);
}

void mktone(int freq, int update, unsigned delay)
{
      uclock_t start;

      if (0 == freq)
      {
            soundoff();
            return;
      }
      dosound(freq);
      if (update != UPDATE)
            soundon();
      if (delay == 0)
            return;
      start = usec_clock();
      while (!usec_timeout(start, usec_clock(), 1000L * (long)delay))
            ;
      if (update == TOGGLE)
            soundoff();
}