@@ -170,33 +170,34 @@ export const LedDot: React.FC<{ bot: UnifiedBot }> = ({ bot }) => (
170170 />
171171) ;
172172
173- // Battery, on the same scale and thresholds the CLI uses ( swarmit's
174- // battery_level_color and its `V (%)` cell). The pack is a 3.0 V supercap, not
175- // a 4.2 V LiPo: reading it on a LiPo scale showed a healthy 2.3 V bot as
176- // nearly flat. Full voltage and the thresholds are hardware facts , so they
177- // live here once and every battery readout goes through them .
178- export const VOLTAGE_FULL_V = 2.9 ;
179- export const VOLTAGE_WARNING_V = 1.5 ;
180- const VOLTAGE_SCALE_V = 3.0 ; // 100% reference
181-
182- /** Percent 0..100, matching the CLI's `int(battery / 3000 * 100)`. */
183- export function batteryPct ( volts : number ) : number {
184- return Math . max ( 0 , Math . min ( 100 , Math . trunc ( ( volts / VOLTAGE_SCALE_V ) * 100 ) ) ) ;
173+ // Battery. The percentage and the band are computed by swarmit, per robot:
174+ // the v3 pack is a 3.0 V supercapacitor that browns out at 0.6 V, and reading
175+ // it on a naive voltage ratio showed a healthy 2.3 V bot as nearly flat. Those
176+ // numbers are robot facts and a v2 pack differs , so this renders what it is
177+ // told rather than keeping a second scale here .
178+ //
179+ // The fallbacks below only cover a bot swarmit does not know (a control-plane
180+ // only bot) and are deliberately crude - a bar with no band rather than a
181+ // confident wrong number.
182+ export function batteryPct ( bot : { batteryPct : number | null ; battery : number } ) : number {
183+ if ( bot . batteryPct !== null ) return Math . max ( 0 , Math . min ( 100 , bot . batteryPct ) ) ;
184+ return Math . max ( 0 , Math . min ( 100 , Math . trunc ( ( bot . battery / 3.0 ) * 100 ) ) ) ;
185185}
186186
187- /** Bar colour, mirroring swarmit's cyan / green / red bands. */
188- export function batteryColor ( volts : number ) : string {
189- if ( volts > VOLTAGE_FULL_V ) return "var(--s-Full )" ;
190- if ( volts > VOLTAGE_WARNING_V ) return "var(--s-Running)" ;
191- return "var(--s-Stopping )" ;
187+ export function batteryColor ( bot : { batteryLevel : string | null } ) : string {
188+ if ( bot . batteryLevel === "full" ) return "var(--s-Full)" ;
189+ if ( bot . batteryLevel === "low" ) return "var(--s-Stopping )" ;
190+ if ( bot . batteryLevel === "ok" ) return "var(--s-Running)" ;
191+ return "var(--muted )" ; // unknown to swarmit: no band to show
192192}
193193
194- export const BatteryCell : React . FC < { volts : number ; width ?: number ; fill ?: boolean } > = ( {
195- volts ,
194+ export const BatteryCell : React . FC < { bot : UnifiedBot ; width ?: number ; fill ?: boolean } > = ( {
195+ bot ,
196196 width = 54 ,
197197 fill = false ,
198198} ) => {
199- const pct = batteryPct ( volts ) ;
199+ const volts = bot . battery ;
200+ const pct = batteryPct ( bot ) ;
200201 return (
201202 < div style = { { display : "flex" , alignItems : "center" , gap : 8 , ...( fill ? { width : "100%" } : { } ) } } >
202203 < div
@@ -212,7 +213,7 @@ export const BatteryCell: React.FC<{ volts: number; width?: number; fill?: boole
212213 style = { {
213214 width : `${ pct } %` ,
214215 height : "100%" ,
215- background : batteryColor ( volts ) ,
216+ background : batteryColor ( bot ) ,
216217 } }
217218 />
218219 </ div >
0 commit comments