📄 settings.c
字号:
/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id: settings.c,v 1.189 2004/01/26 17:05:21 hardeeps Exp $ * * Copyright (C) 2002 by wavey@wavey.org * RTC config saving code (C) 2002 by hessu@hes.iki.fi * * 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 <stdio.h>#include "config.h"#include "kernel.h"#include "thread.h"#include "settings.h"#include "disk.h"#include "panic.h"#include "debug.h"#include "button.h"#include "usb.h"#include "backlight.h"#include "lcd.h"#include "mpeg.h"#include "mp3_playback.h"#include "string.h"#include "ata.h"#include "fat.h"#include "power.h"#include "backlight.h"#include "powermgmt.h"#include "status.h"#include "atoi.h"#include "screens.h"#include "ctype.h"#include "file.h"#include "errno.h"#include "system.h"#include "misc.h"#ifdef HAVE_LCD_BITMAP#include "icons.h"#include "font.h"#include "peakmeter.h"#include "hwcompat.h"#endif#include "lang.h"#include "language.h"#include "wps-display.h"#include "powermgmt.h"#include "bookmark.h"#include "sprintf.h"#include "keyboard.h"#include "version.h"#ifdef HAVE_MAS3507Dvoid dac_line_in(bool enable);#endifstruct user_settings global_settings;char rockboxdir[] = ROCKBOX_DIR; /* config/font/data file directory */char rec_base_directory[] = REC_BASE_DIR;#define CONFIG_BLOCK_VERSION 9#define CONFIG_BLOCK_SIZE 512#define RTC_BLOCK_SIZE 44#ifdef HAVE_LCD_BITMAP#define MAX_LINES 10#else#define MAX_LINES 2#endif/********************************************Config block as saved on the battery-packed RTC user RAM memory blockof 44 bytes, starting at offset 0x14 of the RTC memory space.offset abs0x00 0x14 "Roc" header signature: 0x52 0x6f 0x630x03 0x17 <version byte: 0x0>0x04 0x18 <volume byte>0x05 0x19 <balance byte>0x06 0x1a <bass byte>0x07 0x1b <treble byte>0x08 0x1c <loudness byte>0x09 0x1d <bass boost byte>0x0a 0x1e <contrast (bit 0-5), invert bit (bit 6), show_icons (bit 7)>0x0b 0x1f <backlight_on_when_charging, invert_cursor, backlight_timeout>0x0c 0x20 <poweroff timer byte>0x0d 0x21 <resume settings byte>0x0e 0x22 <shuffle,dirfilter,sort_case,discharge,statusbar,show_hidden, scroll bar>0x0f 0x23 <volume type, battery type, timeformat, scroll speed>0x10 0x24 <ff/rewind min step, acceleration rate>0x11 0x25 <AVC, channel config>0x12 0x26 <(short) Resume playlist index, or -1 if no playlist resume>0x14 0x28 <(short) Resume playlist first index>0x16 0x2a <(int) Byte offset into resume file>0x1a 0x2e <time until disk spindown>0x1b 0x2f <browse current, play selected, recursive dir insert>0x1c 0x30 <peak meter hold timeout (bit 0-4), flip_display (bit 6) rec_editable (bit 7)>0x1d 0x31 <(int) Resume shuffle seed, or -1 if no shuffle>0x21 0x35 <repeat mode (bit 0-1), rec. channels (bit 2), mic gain (bit 4-7)>0x22 0x36 <rec. quality (bit 0-2), source (bit 3-4), frequency (bit 5-7)>0x23 0x37 <rec. left gain (bit 0-3)>0x24 0x38 <rec. right gain (bit 0-3)>0x25 0x39 <disk poweroff flag (bit 0), MP3 buffer margin (bit 1-3), Trickle charge flag (bit 4)>0x26 0x40 <runtime low byte>0x27 0x41 <runtime high byte>0x28 0x42 <topruntime low byte>0x29 0x43 <topruntime high byte>0x2a <checksum 2 bytes: xor of 0x0-0x29>Config memory is reset to 0xff and initialized with 'factory defaults' ifa valid header & checksum is not found. Config version number is onlyincreased when information is _relocated_ or space is _reused_ so that oldversions can read and modify configuration changed by new versions. Newversions should check for the value of '0xff' in each config memorylocation used, and reset the setting in question with a factory default ifneeded. Memory locations not used by a given version should not bemodified unless the header & checksum test fails.Because 0xff mean that the byte is unused, care must be taken so thata used byte can't have the value 0xff. Either use only 7 bits, or make surethat the value will never be 0xff.Rest of config block, only saved to disk:0xA8 (char)jump scroll mode (only for player)0xA9 (char)jump scroll delay (only for player)0xAA Max number of files in playlist (1000-20000)0xAC Max number of files in dir (50-10000)0xAE fade on pause/unpause/stop setting (bit 0) caption backlight (bit 1) car adapter mode (bit 2) line_in (Player only) (bit 3) playlist viewer icons (bit 4) playlist viewer indices (bit 5) playlist viewer track display (bit 6)0xAF <most-recent-bookmarks, auto-bookmark, autoload>0xB0 peak meter clip hold timeout (bit 0-4), peak meter performance (bit 7)0xB1 peak meter release step size, peak_meter_dbfs (bit 7)0xB2 peak meter min either in -db or in percent0xB3 peak meter max either in -db or in percent0xB4 battery capacity0xB5 scroll step in pixels0xB6 scroll start and endpoint delay0xB7 bidir scroll setting (bidi if 0-200% longer than screen width)0xB8 (char[20]) WPS file0xCC (char[20]) Lang file0xE0 (char[20]) Font file0xF4 Prerecording time (bit 0-4), Recording directory option (bit 5-6)0xF5-0xFF <unused>*************************************/#include "rtc.h"long lasttime = 0;static unsigned char config_block[CONFIG_BLOCK_SIZE];/* * Calculates the checksum for the config block and returns it */static unsigned short calculate_config_checksum(unsigned char* buf){ unsigned int i; unsigned char cksum[2]; cksum[0] = cksum[1] = 0; for (i=0; i < RTC_BLOCK_SIZE - 2; i+=2 ) { cksum[0] ^= buf[i]; cksum[1] ^= buf[i+1]; } return (cksum[0] << 8) | cksum[1];}/* * initialize the config block buffer */static void init_config_buffer( void ){ DEBUGF( "init_config_buffer()\n" ); /* reset to 0xff - all unused */ memset(config_block, 0xff, CONFIG_BLOCK_SIZE); /* insert header */ config_block[0] = 'R'; config_block[1] = 'o'; config_block[2] = 'c'; config_block[3] = CONFIG_BLOCK_VERSION;}/* * save the config block buffer to disk or RTC RAM */static int save_config_buffer( void ){ unsigned short chksum;#ifdef HAVE_RTC unsigned int i;#endif DEBUGF( "save_config_buffer()\n" ); /* update the checksum in the end of the block before saving */ chksum = calculate_config_checksum(config_block); config_block[ RTC_BLOCK_SIZE - 2 ] = chksum >> 8; config_block[ RTC_BLOCK_SIZE - 1 ] = chksum & 0xff;#ifdef HAVE_RTC /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so that it would write a number of bytes at a time since the RTC chip supports that, but this will have to do for now 8-) */ for (i=0; i < RTC_BLOCK_SIZE; i++ ) { int r = rtc_write(0x14+i, config_block[i]); if (r) { DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n", 14+i, r ); return r; } }#endif if (fat_startsector() != 0) ata_delayed_write( 61, config_block); else return -1; return 0;}/* * load the config block buffer from disk or RTC RAM */static int load_config_buffer( void ){ unsigned short chksum; bool correct = false;#ifdef HAVE_RTC unsigned int i; unsigned char rtc_block[RTC_BLOCK_SIZE];#endif DEBUGF( "load_config_buffer()\n" ); if (fat_startsector() != 0) { ata_read_sectors( 61, 1, config_block); /* calculate the checksum, check it and the header */ chksum = calculate_config_checksum(config_block); if (config_block[0] == 'R' && config_block[1] == 'o' && config_block[2] == 'c' && config_block[3] == CONFIG_BLOCK_VERSION && (chksum >> 8) == config_block[RTC_BLOCK_SIZE - 2] && (chksum & 0xff) == config_block[RTC_BLOCK_SIZE - 1]) { DEBUGF( "load_config_buffer: header & checksum test ok\n" ); correct = true; } }#ifdef HAVE_RTC /* read rtc block */ for (i=0; i < RTC_BLOCK_SIZE; i++ ) rtc_block[i] = rtc_read(0x14+i); chksum = calculate_config_checksum(rtc_block); /* if rtc block is ok, use that */ if (rtc_block[0] == 'R' && rtc_block[1] == 'o' && rtc_block[2] == 'c' && rtc_block[3] == CONFIG_BLOCK_VERSION && (chksum >> 8) == rtc_block[RTC_BLOCK_SIZE - 2] && (chksum & 0xff) == rtc_block[RTC_BLOCK_SIZE - 1]) { memcpy(config_block, rtc_block, RTC_BLOCK_SIZE); correct = true; }#endif if ( !correct ) { /* if checksum is not valid, clear the config buffer */ DEBUGF( "load_config_buffer: header & checksum test failed\n" ); init_config_buffer(); return -1; } return 0;}/* * persist all runtime user settings to disk or RTC RAM */int settings_save( void ){ DEBUGF( "settings_save()\n" ); /* update the config block buffer with current settings and save the block in the RTC */ config_block[0x4] = (unsigned char)global_settings.volume; config_block[0x5] = (char)global_settings.balance; config_block[0x6] = (unsigned char)global_settings.bass; config_block[0x7] = (unsigned char)global_settings.treble; config_block[0x8] = (unsigned char)global_settings.loudness; config_block[0x9] = (unsigned char)global_settings.bass_boost; config_block[0xa] = (unsigned char) ((global_settings.contrast & 0x3f) | (global_settings.invert ? 0x40 : 0) | (global_settings.show_icons ? 0x80 : 0) ); config_block[0xb] = (unsigned char) ((global_settings.backlight_on_when_charging?0x40:0) | (global_settings.invert_cursor ? 0x20 : 0) | (global_settings.backlight_timeout & 0x1f)); config_block[0xc] = (unsigned char)global_settings.poweroff; config_block[0xd] = (unsigned char)global_settings.resume; config_block[0xe] = (unsigned char) ((global_settings.playlist_shuffle & 1) | ((global_settings.dirfilter & 1) << 1) | ((global_settings.sort_case & 1) << 2) | ((global_settings.discharge & 1) << 3) | ((global_settings.statusbar & 1) << 4) | ((global_settings.dirfilter & 2) << 4) | ((global_settings.scrollbar & 1) << 6)); config_block[0xf] = (unsigned char) ((global_settings.volume_type & 1) | ((global_settings.battery_type & 1) << 1) | ((global_settings.timeformat & 1) << 2) | ( global_settings.scroll_speed << 3)); config_block[0x10] = (unsigned char) ((global_settings.ff_rewind_min_step & 15) << 4 | (global_settings.ff_rewind_accel & 15)); config_block[0x11] = (unsigned char) ((global_settings.avc & 0x03) | ((global_settings.channel_config & 0x07) << 2)); *((short*)(&config_block[0x12])) = global_settings.resume_index; *((short*)(&config_block[0x14])) = global_settings.resume_first_index; memcpy(&config_block[0x16], &global_settings.resume_offset, 4); DEBUGF( "+Resume index %X offset %X\n", global_settings.resume_index, global_settings.resume_offset ); config_block[0x1a] = (unsigned char)global_settings.disk_spindown;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -