ASX Version4.10.32

tuner/main.c

This is an example of how to use the ASX tuner control functions.

/* $Header: /home/eliot/asi/repo/cvsrepo/Repository/apps/asx/examples/tuner/main.c,v 1.9 2008/06/09 18:41:41 as-age Exp $ */
#include "stdio.h"
#include "stdlib.h"
#include "asx.h"
#include "asxstring.h"

int CheckError(ASX_HANDLE hObj, const int nLine, const int nExitOnError);
void PrintControlName(ASX_HANDLE hControl);
ASX_HANDLE hSystem;

int main(int argc, char* argv[])
{

    char *pszName;
    ASX_HANDLE hAdapter;
    ASX_HANDLE hMixer;
    ASX_HANDLE hTuner;
    ASX_HANDLE hPAD;
    ASX_ERROR asxError;
    int nAdapterToUse=0;
    int i,nLen;
    unsigned long lFreq;
    float fMin,fMax,fStep,fGain;
    float fLevel;
    enum asxTUNERBAND eBand;
    int nCount;
    char szString[64];
    int nRequiredStringSize;
    unsigned int nStatusBits,nStatusMask;

    // create the system
    ASX_System_Create(ASX_SYSTEM_TYPE_HPI,&hSystem);
    CheckError(hSystem, __LINE__,1);

    // get the adapter
    asxError = ASX_System_GetAdapter(hSystem,nAdapterToUse,&hAdapter);
    CheckError(hSystem, __LINE__,1);

    ASX_Adapter_GetName(hAdapter,0,0,&nLen);
    CheckError(hAdapter, __LINE__,1);
    pszName = (char *)malloc(nLen);
    ASX_Adapter_GetName(hAdapter,pszName,nLen,&nLen);
    CheckError(hAdapter, __LINE__,1);
    printf("Adapter [%d] is %s \n", nAdapterToUse,pszName);

    // get the mixer handle
    asxError = ASX_Adapter_GetMixer( hAdapter, &hMixer );
    CheckError(hAdapter, __LINE__,1);

    // get a tuner object (index 0)
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        asxNODE_TUNER_IN,0,
        0,0,
        asxCONTROL_TUNER,
        &hTuner);
    CheckError(hMixer, __LINE__,1);
    // print out some control details
    PrintControlName(hTuner);

    // get Tuner band
    asxError=ASX_Tuner_GetBand( hTuner, &eBand );
    CheckError(hTuner, __LINE__,1);
    if(!asxError)
    {
        ASXSTRING_EnumToString(eBand, szString, 64, &nRequiredStringSize);
        printf("Tuner band is currently %s\n",szString);
    }


    // List all the bands and print them out
    asxError=ASX_Tuner_EnumerateBand( hTuner, 0, &eBand, &nCount);

    printf("Total tuner bands available is %d\n",nCount);
    for(i=0;i<nCount;i++)
    {
        asxError=ASX_Tuner_EnumerateBand( hTuner, i, &eBand, &nCount);
        CheckError(hTuner, __LINE__,1);

        if(!asxError)
        {
            ASXSTRING_EnumToString(eBand, szString, 64, &nRequiredStringSize);
            printf("Tuner band [%d] is %s\n",i,szString);
        }
    }

    // setting Tuner to band FM
    asxError=ASX_Tuner_SetBand( hTuner, asxTUNERBAND_FM );
    CheckError(hTuner, __LINE__,1);

    asxError = ASX_Tuner_GetFrequency( hTuner, &lFreq);
    CheckError(hTuner, __LINE__,1);
    asxError = ASX_Tuner_SetFrequency( hTuner, lFreq);
    CheckError(hTuner, __LINE__,1);


    asxError = ASX_Tuner_GetGainRange( hTuner, &fMin,&fMax,&fStep);
    CheckError(hTuner, __LINE__,1);
    if(!asxError)
    {
        printf("Gain range, min=%f dB, Max = %f dB, Step size is %f dB\n",
                fMin,
                fMax,
                fStep);

        asxError = ASX_Tuner_GetGain( hTuner, &fGain);
        CheckError(hTuner, __LINE__, 0);
        asxError = ASX_Tuner_SetGain( hTuner, fGain);
        CheckError(hTuner, __LINE__, 0);
    }


    asxError = ASX_Tuner_GetRFLevel( hTuner, &fLevel);
    CheckError(hTuner, __LINE__, 0);
    printf("RF level is %f \n",fGain);

    // get the status (not all tuners have status field) ?
    asxError = ASX_Tuner_GetStatus( hTuner, &nStatusMask, &nStatusBits);
    CheckError(hTuner, __LINE__, 0);
    if(!asxError)
        printf("Tuner status mask 0x%08X, vale 0x%08X\n",nStatusMask,nStatusBits);


    // read RDS fields

    // get a PAD/RDS object (index 0)
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        asxNODE_TUNER_IN,0,
        0,0,
        asxCONTROL_PAD,
        &hPAD);
    CheckError(hMixer, __LINE__,0);
    if(!asxError)
    {
        char szBuffer[ASX_LONGLONG_STRING];
        int n;

        // print out some control details
        PrintControlName(hPAD);

        // channel name
        asxError = ASX_PAD_GetChannelName(hPAD,szBuffer,sizeof(szBuffer));
        CheckError(hMixer, __LINE__,0);
        if(asxError==asxERROR_NO_ERROR)
            printf("PAD (ChannelName) : %s\n",szBuffer);
        
        // RDS PI
        asxError = ASX_PAD_GetRdsPI(hPAD,&n);
        CheckError(hMixer, __LINE__,0);
        if(asxError==asxERROR_NO_ERROR)
            printf("PAD (RDS PI) : %d\n",n);

        // RDS PTY
        asxError = ASX_PAD_GetProgramType(hPAD,&n);
        CheckError(hMixer, __LINE__,0);
        if(asxError==asxERROR_NO_ERROR)
        {
            asxError=ASX_PAD_GetProgramTypeString(
                    hPAD,
                    asxTUNER_RDS_TYPE_RDS,
                    n,
                    szBuffer,
                    sizeof(szBuffer));

            printf("PAD (Program Type) : [%d] %s\n",n,szBuffer);
        }

        // artist
        asxError = ASX_PAD_GetArtist(hPAD,szBuffer,sizeof(szBuffer));
        CheckError(hMixer, __LINE__,0);
        if(asxError==asxERROR_NO_ERROR)
            printf("PAD (Artist) : %s\n",szBuffer);

        // title
        asxError = ASX_PAD_GetTitle(hPAD,szBuffer,sizeof(szBuffer));
        CheckError(hMixer, __LINE__,0);
        if(asxError==asxERROR_NO_ERROR)
            printf("PAD (Title) : %s\n",szBuffer);

        // comment
        asxError = ASX_PAD_GetComment(hPAD,szBuffer,sizeof(szBuffer));
        CheckError(hMixer, __LINE__,0);
        if(asxError==asxERROR_NO_ERROR)
            printf("PAD (Comment) : %s\n",szBuffer);

    }


    printf("DONE.\n");
    printf("Press ENTER to exit\n");
    getchar();
    ASX_System_Delete(hSystem);
    return 0;
}

void PrintControlName(ASX_HANDLE hControl)
{
    char *pszName;
    int nLen;
    enum asxCONTROL eControl;

    ASX_Control_GetType(hControl, &eControl);
    ASXSTRING_EnumToString(eControl,0,0,&nLen);
    pszName=(char *)malloc(nLen);
    ASXSTRING_EnumToString(eControl,pszName,nLen,&nLen);
    printf("Control : %s\n",pszName);

    free(pszName);
}


int CheckError(ASX_HANDLE hObj, const int nLine, const int nExitOnError)
{
    int nError;
    int asxSubSystemErrorCode=0;
    char *pszAsxErrorString;
    char *pszAsxSubSystemErrorString;
    int nLen1,nLen2;

    ASX_Error_GetLast( hObj, &nError, &asxSubSystemErrorCode);
    if(!nError)
        return 0;
    ASX_Error_GetLastString( hObj, 0,0,&nLen1,0,0,&nLen2);
    pszAsxErrorString = (char *)malloc(nLen1);
    pszAsxSubSystemErrorString = (char *)malloc(nLen2);
    ASX_Error_GetLastString( hObj, pszAsxErrorString,nLen1,&nLen1,pszAsxSubSystemErrorString,nLen2,&nLen2);
    printf("Error: #%d, %s - Subsystem Error: #%ld, %s \n",
        nError,
        pszAsxErrorString,
        asxSubSystemErrorCode,
        pszAsxSubSystemErrorString );
    printf("When called from source %s line %d\n",__FILE__,nLine);
    if(nExitOnError)
    {
        printf("Press ENTER to exit\n");
        getchar();
        free(pszAsxErrorString);
        free(pszAsxSubSystemErrorString);
        ASX_System_Delete(hSystem);
        exit(1);
    }
    return 1;
}