/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: $
 *
 * Copyright (C) 2006 Antoine Cellerier <dionoea @t videolan d.t org>
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "configfile.h"
#include "button.h"
#include "lcd.h"

PLUGIN_HEADER

/**
 * Plugin entry point
 */

enum plugin_status plugin_start( struct plugin_api* api, void* parameter )
{
    /* plugin init */
    (void)parameter;
    unsigned long i=0,j,k;
    unsigned long freq = 0x100;
    unsigned long en = 1;
    unsigned long vol = 0x80;
    char str[80];

    unsigned long *piezo = (unsigned long *)0x7000a000;

#define FREQ_MIN 0
#define FREQ_MAX 0x2000

#define VOL_MIN 0
#define VOL_MAX 0x100

#define PIEZO_INIT    (*(volatile unsigned long*)(0x70000010))

//4 addresses control the piezo ?!?
#define PIEZO (*(volatile unsigned long*)(0x7000a000))
//#define PIEZO (*(volatile unsigned long*)(0x7000a040))
//#define PIEZO (*(volatile unsigned long*)(0x7000a080))
//#define PIEZO (*(volatile unsigned long*)(0x7000a0c0))

//#define PIEZO_ENABLE 0x80000000
//#define PIEZO_FREQ   0x00001fff
//#define PIEZO_WAVE   0x00ff0000

    PIEZO_INIT &= ~0xc;
    DEV_EN |= 0x20000;

    api->lcd_setfont( FONT_SYSFIXED );

    while( true )
    {
        unsigned long command = (en<<31)|(vol<<16)|freq;
        PIEZO = command;

        api->lcd_puts( 0, 0, "ipod piezo test" );
        api->snprintf( str, 40, "%c freq: %04x", i==0?'>':' ', freq );
        api->lcd_puts( 0, 2, str );
        api->snprintf( str, 40, "%c enabled: %d", i==1?'>':' ', en );
        api->lcd_puts( 0, 3, str );
        api->snprintf( str, 40, "%c vol/wave form?: %02x", i==2?'>':' ', vol );
        api->lcd_puts( 0, 4, str );
        api->snprintf( str, 40, "command: %08x", command );
        api->lcd_puts( 0, 6, str );
        k = 0;
        for( j = 0; j < 250 && k < 22; j++ )
        {
            if( piezo[j] )
            {
                api->snprintf( str, 80, "%08x -> %08x", piezo+j, piezo[j] );
                api->lcd_puts( 0, 8+k, str );
                k++;
            }
        }
        api->lcd_update();

        switch( api->button_get( true ) )
        {
            case BUTTON_LEFT:
                switch( i )
                {
                    case 0:
                        freq = (freq+FREQ_MAX-1)%FREQ_MAX;
                        break;
                    case 1:
                        en = (en+1)%2;
                        break;
                    case 2:
                        vol = (vol+VOL_MAX-1)%VOL_MAX;
                        break;
                }
                break;
            case BUTTON_LEFT|BUTTON_REPEAT:
                switch( i )
                {
                    case 0:
                        freq = (freq+FREQ_MAX-0xf)%FREQ_MAX;
                        break;
                    case 1:
                        en = (en+1)%2;
                        break;
                    case 2:
                        vol = (vol+VOL_MAX-0x8)%VOL_MAX;
                        break;
                }
                break;
            case BUTTON_RIGHT:
                switch( i )
                {
                    case 0:
                        freq = (freq+1)%FREQ_MAX;
                        break;
                    case 1:
                        en = (en+1)%2;
                        break;
                    case 2:
                        vol = (vol+1)%VOL_MAX;
                        break;
                }
                break;
            case BUTTON_RIGHT|BUTTON_REPEAT:
                switch( i )
                {
                    case 0:
                        freq = (freq+0xf)%FREQ_MAX;
                        break;
                    case 1:
                        en = (en+1)%2;
                        break;
                    case 2:
                        vol = (vol+0x8)%VOL_MAX;
                        break;
                }
                break;

            case BUTTON_MENU:
                i = (i+2)%3;
                break;
            case BUTTON_PLAY:
                i = (i+1)%3;
                break;

            case BUTTON_SELECT:
                goto stop;
        }
    }

    stop:
    PIEZO = 0x0;

    return 0;
}
