C Language Development Kit/Functions/ComIdle

From OptiWiki

Jump to: navigation, search
Description Starts or exits a Bluetooth low power mode.
Syntax
int com_idle(int state, int arg);
Arguments
int state
State is the type of low-power mode that will be selected. See table and remarks for more information.
int arg
The arg parameter depends on the value for state. The following options are defined:
State Arg
PARK Enters park mode. Arg specifies the beacon period. The arg stands for beacon cycle the beacon cycle can range from 200-10000 slots.
EXIT_PARK Exits park mode, and returns to full speed mode. Arg is ignored.
SNIFF Enters sniff mode. Arg specifies the sniff period. The arg argument can range from 66-10000.
EXIT_SNIFF Exits sniff mode, and returns to full speed mode. Arg is ignored.
HOLD Enters hold mode. Arg specifies the hold period. The arg is the duration time slots and can range from 4-10000 slots.
EXIT_HOLD_CHECK Checks if the hold period has elapsed.
Returns All functions return OK, but the EXIT_HOLD_CHECK function returns -1 when the

terminal is still in hold mode.

Remarks Although described in the Bluetooth specification, not all Bluetooth devices support all the above modes. Some devices only initiate, but do not accept when the terminal tries to initiate.

Bluetooth connection modes from high to low power: Active or full speed mode. In Active mode the terminal actively participates on the channel. The Master (Server) schedules the traffic demands of the terminal. It supports regular data transmission from Master to and from terminal to keep the terminal synchronized to the channel. Active terminal listen to master of terminal packets.


Sniff Mode. In Sniff mode the terminal device activity is lowered. In sniff mode the terminal listens to the master at a reduced rate, this means reducing it's duty cycle. The sniff interval is programmable by the com_idle() function. com_idle( SNIFF, arg ). The arg argument can range from 66-10000. The arg sets the duty cycle when the terminal checks the connection. A slot is 625 µs. The Sniff mode has the highest duty cycle (read: Highest power consumption) of the 3 power saving modes (sniff, hold and park).


Hold Mode. In hold mode device activity is lowered for an amount of slots. A slot is 625 µs. The terminal can put itself into hold mode by the command com_idle( HOLD, arg ). The arg is the duration time slots and can range from 4-10000 slots. The Hold mode has the medium duty cycle (read: Medium power consumption) of the 3 power saving modes (sniff, hold and park).


Park mode. In park mode the terminal is still connected (synchronized) to the Master but does not produce traffic. The terminal in parked mode has given up it's bluetooth MAC (AM_ADDR) address. The terminal listens occasionally to the traffic of the master to re-synchronize and checks broadcast messages. The terminal can be put into park mode by the function com_idle( PARK, arg). The arg stands for beacon cycle the beacon cycle can range from 200-10000 slots. Every beacon cycle a slot can be received by the terminal. The Park mode has the lowest duty cycle (read: least power consumption) of the 3 power saving modes ( sniff, park and hold).

Example
//
// Example application to show some of the OPL9724, OPL9712
// bluetooth functions
//
// The application connect to bluetooth host.
// Then scan barcode and after that directly transmit
// barcode to the host.
//
 
#include <stdio.h>
#include "lib.h"
 
#define BCR_MAX    50
#define COM_USED   COM3 // SPP Connect port
 
void keybeep( void )
{
    sound( TSHORT, VMEDIUM, SHIGH, 0);
}
 
void okbeep( void )
{
    sound( TSTANDARD, VMEDIUM, SHIGH, SMEDIUM, SHIGH, 0);
}
 
void errbeep( void )
{
    sound( TLONG, VMEDIUM, SLOW, SMEDIUM, SHIGH, 0);
}
 
int waitkey( void )
{
    int c;
 
    while( ( c = getchar()) == EOF)
        idle();
    keybeep(); // make the beeping sound 
    return c;
}
 
int check_connection( void )
{
    int result;
 
    //
    // Check if the bluetooth device is still connected
    //
    for(;;)
    {
        result = blt_is_connected();
        switch( result )
        {
            case NG:
                //
                // Connection can not be determined at
                // the moment we have to wait a little while.
                //
                delay(5);
                break;
            default:
                return result; // return TRUE or FALSE
        }
    }
}
 
int ScanBarcode( char* string, int min_length, int max_length)
{
    static int fActive;
    static char barcode_buffer[ BCR_MAX ];
    int key;
    struct barcode code;
 
    code.min = min_length;
    code.max = max_length;
    code.text = barcode_buffer;
 
    if( !fActive )
    {
        com_idle( EXIT_SNIFF, 0 );
        fActive = TRUE;
        gotoxy(0,3);
        printf("Active...");
    }
    starttimer( 600 ); // wait 12 seconds before powersaving mode
    while( readbarcode(  ) != OK )
    {
        scannerpower( SINGLE, 300 );
        if( kbhit() )
        {
            resetkey();
            starttimer( 600 ); // wait 12 seconds before powersaving mode
            if( !fActive )
                com_idle( EXIT_SNIFF, 0 );
 
            fActive = TRUE;
            gotoxy(0,3);
            printf("Active...");
        }
 
        //
        // Check if the OPL is still connected to the PC
        //
        if( check_connection() == FALSE )
            return ERROR;
 
        if( scanneroff() && fActive && endtimer())
        {
            com_idle( SNIFF, 100 );
            fActive = FALSE;
            gotoxy(0,3);
            printf("Sniff... ");
        }
        idle();
    }
 
    strcpy( string, barcode_buffer );
    return( OK );
}
 
int transmit_barcode( char* barcode )
{
    char* ptr;
    static char transm_string[ 40 + 2 + 1 ];
 
    strcpy( transm_string, barcode );
    strcat( transm_string, "\r\n");
    ptr = transm_string;
    while( *ptr  )
    {
        if( putcom( *ptr ) == ERROR )
            return ERROR;
        ptr++;
    }
    return OK;
}
 
void main( void )
{
    static char barcode[ 40 + 2+1 ];
    static char msg[40+25];
    static int scanned_barcode;
 
    autopowerdown( ON, 1000 ); // 20 seconds
    systemsetting("SZ");       // 115200 baud
    systemsetting("7G");       // show charging indication LED
 
    scanned_barcode = FALSE;
    cursor( NOWRAP );
    setfont( LARGE_FONT, NULL );
 
    //
    // Set bluetooth name (max. 18 characters)
    //
    SetBltLocalName("Ex OPL-9724" );
 
    //
    // The main application loop.
    //
    for(;;)
    {
        printf("\f%s\nSPP con. to\n %s\npress a key",
                            GetBltLocalName(), GetBltAddress());
        waitkey();
 
        for(;;)
        {
            printf("\fConnecting...");
 
            //
            // Open the comport with SPP
            //
            if( comopen( COM_USED ) != 0 )
            {
                printf("\fError open\nSPP port\n\npress a key");
                waitkey();
                continue;
            }
            break;
        }
        printf("\fConnected... ");
        for(;;)
        {
            if( check_connection() == FALSE )
            {
                printf("\fconn. lost\nget closer\nto BT station\npress a key");
                waitkey();
                break;
            }
 
            if( scanned_barcode )
            {
                //
                // A barcode was previously scanned but could not be transmitted
                //
                if( transmit_barcode( barcode ) == OK )
                    scanned_barcode = FALSE;
                else
                    break; // connection failed
                printf("\fConnected... ");
            }
 
            memset( barcode, '\0', sizeof( barcode ));
            gotoxy(0,1);
            printf("Scan barcode.");
            //
            // Scan barcode
            //
            if( ScanBarcode( barcode, 1, 40 ) == ERROR)
                continue;
 
            okbeep();
            gotoxy(0,2);
            printf("%-14.14s",barcode );
            scanned_barcode = TRUE;
 
            //
            // Transmit the barcode
            //
            if( scanned_barcode && transmit_barcode( barcode ) == OK )
                scanned_barcode = FALSE;
        }
 
        //
        // Close the SPP port to make reconnection is possible
        //
        comclose( COM_USED );
    }
}


Views
Personal tools