re: Alex beep code example threads

  • From: Alex Hall <mehgcap@xxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 08 Jun 2008 21:43:35 -0400

Thanks for the example!! I will try it to find out if I have the boost libraries. I never knew there was a function to beep the sound card, I will look into that. I want this to work whether I have my speakers on or not, mainly for my dorm where my only speakers are headphones I am not always wearing. I can have them both beep, though... I will keep tweeking it, as well as put it onto my pc in my compiler so I can see just how it works; reading it in braille on my braillenote is not quite the same thing. I always thought braille would be easier for coding, but I am now so used to speech that using braille is often slower and more confusing. Anyway, thanks again and I will let you know if I do not have the boost library.


Have a great day,
Alex

----- Original Message -----
From: "Ken Perry" <whistler@xxxxxxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Date sent: Sun, 8 Jun 2008 18:21:20 -0700
Subject: Alex beep code example threads



Alex I want to first say if I was doing this I would make sure to
not use
just beep and I would code in a call to the wave function so you
could use a
sound card. I guess if the person didn't have a sound card you
could check
for it and then use the pc speaker. With that said I liked your
little
project and though I would show you how it would be done with the
boost
thread and date libraries. Now if you don't have boost installed
I can tell
you how to do that if you want to. Here is the code that I made
though and
it works.

#include "boost/date_time/posix_time/posix_time.hpp"
#include <boost/thread.hpp
#include <boost/bind.hpp
#include <iostream
#include <windows.h
#include <string

using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace std;

enum PERIOD
{
 HOUR,
 QUARTER,
 HALF,
 THREEQUARTER,
};

bool
 beeps = true;

void
chime (PERIOD when)
{
 int
chimes = 0; // used to repeat chimes for the
minutes
 long LastChime;//used to not repeat when it has already chimed
 //get the current time from the clock -- one second resolution
 ptime now = second_clock::local_time ();

//set the start last_time so that it doesn't ring instantly
 if (when == HOUR)
   LastChime = now.time_of_day ().hours ();
 else
   LastChime = now.time_of_day ().minutes ();

 while (1)
   {
     Sleep (1000);
     now = second_clock::local_time ();

     if (beeps == true)
        {
          switch (when)
            {
            case HOUR:
              if (LastChime != now.time_of_day ().hours ())
                {
for (long i = 0; i < now.time_of_day ().hours ();
i++)
                    {
                      Beep (244, 500);
                      Sleep (500);
                    }
                  LastChime = now.time_of_day ().hours ();
                }
              break;
            case QUARTER:
              if ((LastChime != now.time_of_day ().minutes ()) &&
                  (now.time_of_day ().minutes () == 15))
                chimes = 1;
              break;
            case HALF:
              if ((LastChime != now.time_of_day ().minutes ()) &&
                  (now.time_of_day ().minutes () == 30))
                chimes = 2;
              break;
            case THREEQUARTER:
              if ((LastChime != now.time_of_day ().minutes ()) &&
                  (now.time_of_day ().minutes () == 45))
                chimes = 3;
              break;
            };                  //end switch

          if (chimes > 0)
            {
              for (; chimes > 0; chimes--)
                {
                  Beep (488, 500);
                  Sleep (500);
                }
              LastChime = now.time_of_day ().minutes ();
            }
        }                       //end if beeps true
   }                            //end while

}

void
input ()
{
 string
   cmd;
 while (1)
   {
     cin >> cmd;

     if (cmd[0] == 'b')
        {
          beeps = true;
          cout << "Beeps turned on." << endl;
        }
     else if (cmd[0] == 's')
        {
          beeps = false;
          cout << "Beeps turned off." << endl;
        }
     else if (cmd[0] == 'q')
        {
          cout << "Turning off beeper." << endl;
          exit (0);
        }

   }
}

int
main ()
{
 boost::thread_group Tg;
 Tg.create_thread (boost::bind (chime, HOUR));
 Tg.create_thread (boost::bind (chime, QUARTER));
 Tg.create_thread (boost::bind (chime, HALF));
 Tg.create_thread (boost::bind (chime, THREEQUARTER));
 Tg.create_thread (boost::bind (input));

 Tg.join_all ();
 std::cout << "Done! =)" << std::endl;
 std::cin.get ();
 return 0;
}

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: