home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / usbd0906.zip / usb_20020906.zip / usbcalls / samples / fmradio / radio.cpp < prev    next >
C/C++ Source or Header  |  2001-11-13  |  6KB  |  203 lines

  1. /***************************/
  2. /*                         */ 
  3. /*  USB FM Radio SDK       */
  4. /*  by Dr. Ebony Huang     */
  5. /*  ysh@pidc.gov.tw        */ 
  6. /*  OS/2 implementation    */ 
  7. /*  by Markus Montkowski   */ 
  8. /***************************/
  9.  
  10. /* After the installation of USB FM Radio, one can locate a file 
  11.    called radio.dll in the Windows folder. The Radio.dll provides 
  12.    some functions which can be used to control and communicate with 
  13.    Radio hardware. The details of the functions in radio.dll 
  14.    is explained as follows. You are welcome to write your own application 
  15.    to control the Radio hardware. If you want to share your application 
  16.    with others, please send to me via email:ysh@pidc.gov.tw We will 
  17.    put your app. in the website www.fmbox.com.tw .
  18. */
  19. #define INCL_DOS
  20. #include <os2.h>
  21. #include "..\..\usbcalls.h"
  22.  
  23.  
  24. /* The Org Windows DLL also doesn't implement the open and close
  25.    Function and does open/close in each function.
  26. */
  27.  
  28. /*
  29.  Open the USB Radio hardware.
  30.  Return
  31.    0 : Failure, can not find Radio hardware.
  32.    1 : Success, the hardware is correctly connected to PC and is opened.
  33. */
  34. int _System open_radio(void)
  35. {
  36.   return 1;
  37. }
  38.  
  39.  
  40.  
  41. /*
  42.  Close the USB Radio hardware.
  43.  Return 
  44.    0       : Failure
  45.    Nonzero : Success   
  46.  In general, before going to control the Radio hardware, 
  47.  one has to call the open_radio to see if the hardware exists. 
  48.  Before close the application, one has to call the close_radio 
  49.  to tell PC that you don't want to control the radio anymore.
  50. */
  51. int _System close_radio(void)
  52. {
  53.   return 1;
  54. }
  55.  
  56. /*
  57.   Turning Radio on/off (mute or not)
  58.   f:1 Turn on the radio, active
  59.   f:0 Turn off the radio, inactive.
  60.  Return 
  61.    0       : Failure
  62.    Nonzero : Success   
  63. */
  64. int _System turn_power(int f)
  65. {
  66.   APIRET rc;
  67.   USBHANDLE Handle;
  68.   UCHAR ucData[8];
  69.  
  70.   rc = UsbOpen( &Handle,
  71.                 0x04b4,
  72.                 0x1002,
  73.                 USB_ANY_PRODUCTVERSION,
  74.                 USB_OPEN_FIRST_UNUSED);
  75.   if(!rc)
  76.   {
  77.     rc = UsbCtrlMessage( Handle,
  78.                          0xC0, 0x02,
  79.                          f!=0?1:0, 0x00,
  80.                          1,(UCHAR*)&ucData,
  81.                          0);
  82.     UsbClose(Handle);
  83.   }
  84.   return((rc==0)?1:0);
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.   Setting radio frequency. The Japanese Radio hardware is different 
  91.   from other area's Radio hardware. Japanese Radio hardware covers 
  92.   the radio freq. 76.00~91.00 MHz, others cover 88.00~108.00 MHz. 
  93.   The minimum freq step is 0.05 MHz. Since we use the integer ch(channel)  
  94.   as the parameter to set radio frequency,  the ch, therefore, 
  95.   is defined as ch=Freq*20. This means that when one to set the 100.1 MHz,  
  96.   one should put ch to be 100.1*20 and the japan as 0 as the parameter 
  97.   in the set_freq function
  98.   For the japanese radio hardware, one has to set japan to be 1 when 
  99.   invoking the set_freq function.
  100.  
  101.  Return 
  102.    0       : Failure
  103.    Nonzero : Success   
  104. */
  105. int _System set_freq(int ch,int japan)
  106. {
  107.   double dFreq;
  108.   ULONG ulFreq;
  109.   APIRET rc;
  110.   USBHANDLE Handle;
  111.   UCHAR ucData[8];
  112.  
  113.   dFreq = ch * 0.05;
  114.   if(japan)
  115.   {
  116.     ulFreq = ((dFreq-10.7)*80);
  117.   }
  118.   else
  119.   {
  120.     ulFreq = ((dFreq+10.7)*80);
  121.   }
  122.   rc = UsbOpen( &Handle,
  123.                 0x04b4,
  124.                 0x1002,
  125.                 USB_ANY_PRODUCTVERSION,
  126.                 USB_OPEN_FIRST_UNUSED);
  127.   if(!rc)
  128.   {
  129.     rc = UsbCtrlMessage( Handle,
  130.                          0xC0, 0x01,
  131.                          ulFreq>>8,ulFreq,
  132.                          1,(UCHAR*)&ucData,
  133.                          0);
  134.     UsbClose(Handle);
  135.   }
  136.   return(rc==0?1:0);
  137. }
  138.  
  139.  
  140.  
  141.  
  142. /*
  143.   Get the Radio status.
  144.  
  145.  Return 
  146.    0       : non stereo
  147.    1       : Radio is in stereo mode, this also means the radio is tuned 
  148.              to some station.
  149.  
  150. */
  151. int _System get_stereo(void)
  152. {
  153.   APIRET rc;
  154.   USBHANDLE Handle;
  155.   UCHAR ucData[8];
  156.  
  157.   rc = UsbOpen( &Handle,
  158.                 0x04b4,
  159.                 0x1002,
  160.                 USB_ANY_PRODUCTVERSION,
  161.                 USB_OPEN_FIRST_UNUSED);
  162.   if(!rc)
  163.   {
  164.     rc = UsbCtrlMessage( Handle,
  165.                          0xC0, 0x00,
  166.                          0, 0x00,//0x24,
  167.                          1,(UCHAR*)&ucData,
  168.                          0);
  169.     UsbClose(Handle);
  170.   }
  171.   return(rc==0?((ucData[0]&0x01)==0x00)?1:0:-1);
  172. }
  173.  
  174.  
  175. /*
  176.    The above five basic functions can let the programmer to write his/her own
  177.    special application to control the Radio hardware. 
  178.    But we do not provide the searching radio station function in the radio.dll. 
  179.    The users can combine the set_freq and get_stereo functions to do the 
  180.    seraching station function. One thing should be noted: 
  181.    after calling the set_freq, one should delay for a while,say 100ms, 
  182.    before reading the radio status.(get_stereo). 
  183.    If the radio is not tuned, the get_stereo will retrun 0, 
  184.    otherwise it will return a nonzero value.
  185.  
  186.                           100.1 MHz
  187.     searching up     100.0   100.2
  188.     ---->----------------****----------
  189.                     There is a station covers 100.0~100.2 MHz 
  190.                        
  191. The above approach cannot locate the best radio station because it will
  192. stop immediately when the get_stereo return 1. 
  193. Like the above figure,the seraching function will stop at 100.00 MHz 
  194. instead of the best station 100.1 MHz. Therefore, during the searching 
  195. mode, when one finds the radio is in the stereo mode, one has to keep 
  196. tuning to find out what radio frequency is not tuned(here is 100.2MHz), 
  197. after the above two steps, we can conclude the searching radio station 
  198. should stop at 100.1MHz=(100.0+100.2)/2.
  199.  
  200.  
  201. Ebony Huang
  202. */
  203.