Code Samples

Home Up

bullet

Graphical Battery Indicator for the Banner State

bullet

This graphical battery indicator provides a easy to read indication of the battery voltage when switching between WristApps with the mode button.  The battery display shows full when voltage >=3.0 volts and empty when voltage is <2.5 volts.  I chose 3.0 volts for full because that is the voltage of the CR2025 battery when it is new.  I chose 2.5 volts for empty because that is the minimum voltage required by the EEPROM memory chip.

bullet

This code highlights two undocumented techniques.  The first is accessing the CPU's SVD (supply voltage detector) to gauge the battery voltage.  The code I'm using for this appears to have been originally written by Philip Hamer.  Note that this graphical version is only about 1/2 the size because we are only looking at a few of the possible voltage levels and the graphical display is simpler than text display.  The second technique is direct addressing of the LCD memory using information from Yahoo group member "Ralf".  By addressing the LCD memory directly, we can set half a column of pixels at once (instead of one pixel at a time using the API).  This makes it very easy to draw the battery.

bullet

Here's the code directly from SciCalc version 3a:

bullet
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; File Name :   sciban.asm
; Purpose   :   SciCal Application Banner State Manager
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;DO NOT MODIFY THE FOLLOWING SUBROUTINE DEFINITION;;;;;;;;;;;;;;;;;

                IF @DEF('SUBROUTINE')
                    UNDEF SUBROUTINE
                ENDIF
                DEFINE  SUBROUTINE      "'sciban'"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

                GLOBAL  sciwaBannerStateManager

sciwaBannerStateManager:
					;first, do the normal stuff
                CAR coreCommonBannerStateHandler
					;then, show battery level
;WristApp: BATTERY CHECK
;================================================================================
;
;by Philip Hamer  (Modified to give graphical display by Raymond Allen)
;Version: 0.1
;
;Description:
;------------
;This WristApp prints out the voltage level of your watch.
;
;Source code:
;------------
		; Turn on 1/4 HZ sampling
                ld		HL, #SVDReg
                and		[HL], #~bSVDON
                or		[HL], #bSVDSP
                
                ; We need to wait 16 clock cycles for reading of battery power
                ; 1 bus cycle = 2 clock cycles
                ; nop = 2 bus cycles = 4 clock cycles
                nop
                nop
                nop
                nop
                
                ; Mask out top 4 bits to get voltage level
                ld		A, [HL]
                and		A, #(bSVD0|bSVD1|bSVD2|bSVD3)
                
                ; Turn off 1/4 HZ sampling
                and		[HL], #~bSVDSP
                
				;LD A,#5		;FORCE BATTERY LEVEL  !!!!  REMOVE ME  !!!!!!!!!!

                ; Check voltage and draw battery
				LD H,#03Eh		;battery full line
				;LD IX,#0FA02h	;starting address for display on Line2
				LD IX,#0FA08h	;display on Line2
DrawBatteryBottom:
				LD [IX],H
				INC IX
				LD [IX],H
				INC IX				
					;there are 15 possible levels, but I'll just consider 4..8  (8=3.0V)
					;3.0 (8) will show as full, 2.33 (4) will show empty
				LD L,#3			;level 5=2.50V
CheckBatteryLoop:
				INC L

				LD B,L
				CP A,B
				JR GT,DrawBattery
				LD H,#022h		;battery empty line

DrawBattery:
				LD B,#4		;draw 4 of same thing
DrawBatteryLoop:		
				LD [IX],H
				INC IX
				DJR NZ,DrawBatteryLoop
				
				CP L,#7
				JR LT,CheckBatteryLoop			
						;done

DrawBatteryTop:
				LD H,#03Eh		;battery full line
				LD [IX],H
				INC IX
				LD [IX],H
				INC IX
				LD H,#01Ch		;battery top
				LD [IX],H	
				INC IX
				LD [IX],H

BanReturn:
				RET			;all done