Skip to content

Commit

Permalink
[units] fix display of deg in messages (no conversion)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautierhattenberger committed Jul 25, 2012
1 parent 67da4ed commit 8212e54
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions sw/ground_segment/tmtc/messages.ml
Expand Up @@ -67,7 +67,7 @@ let one_page = fun sender class_name (notebook:GPack.notebook) bind m ->
let literal_values = values_of_field f in
let alt_value =
try
let coeff = float_of_string (Pprz.alt_unit_coef_of_xml f)
let coeff = float_of_string (Pprz.alt_unit_coef_of_xml ~auto:"display" f)
and unit = Xml.attrib f "alt_unit" in
fun value -> sprintf "%s (%f%s)" value (coeff*.float_of_string value) unit
with
Expand All @@ -86,7 +86,7 @@ let one_page = fun sender class_name (notebook:GPack.notebook) bind m ->
(* box dragger *)
field_label#drag#source_set dnd_targets ~modi:[`BUTTON1] ~actions:[`COPY];
let data_get = fun _ (sel:GObj.selection_context) ~info ~time ->
let scale = Pprz.alt_unit_coef_of_xml f in
let scale = Pprz.alt_unit_coef_of_xml ~auto:"display" f in
let field_descr =
if Pprz.is_array_type type_ then
match GToolbox.input_string ~title:"Index of value to drag" ~text:"0" "Index in the array ?" with
Expand Down
15 changes: 10 additions & 5 deletions sw/lib/ocaml/pprz.ml
Expand Up @@ -162,7 +162,7 @@ exception Unit_conversion_error of string
exception Unknown_conversion of string * string
exception No_automatic_conversion of string * string

let scale_of_units = fun from_unit to_unit ->
let scale_of_units = fun ?auto from_unit to_unit ->
if (from_unit = to_unit) then
1.0
else
Expand All @@ -173,7 +173,9 @@ let scale_of_units = fun from_unit to_unit ->
(* will raise Xml.No_attribute if not a valid attribute *)
let f = Xml.attrib u "from"
and t = Xml.attrib u "to"
and a = String.lowercase (ExtXml.attrib_or_default u "auto" "") in
and a = match auto with
| Some a -> a
| None -> String.lowercase (ExtXml.attrib_or_default u "auto" "") in
if (f = from_unit || a = "display") && (t = to_unit || a = "code") then true else false
) (Xml.children units_xml) in
(* return coef, raise Failure if coef is not a numerical value *)
Expand All @@ -187,13 +189,16 @@ let scale_of_units = fun from_unit to_unit ->
| _ -> raise (Unknown_conversion (from_unit, to_unit))


let alt_unit_coef_of_xml = function xml ->
let alt_unit_coef_of_xml = fun ?auto xml ->
try Xml.attrib xml "alt_unit_coef"
with _ ->
let u = try Xml.attrib xml "unit" with _ -> "" in
let au = try Xml.attrib xml "alt_unit" with _ -> "" in
let coef = try string_of_float (scale_of_units u au) with
Unit_conversion_error s -> prerr_endline (sprintf "Unit conversion error: %s" s); flush stderr; "1." (* Use coef 1. *)
let coef = try string_of_float (match auto with
| None -> scale_of_units u au
| Some a -> scale_of_units u au ~auto:a)
with
| Unit_conversion_error s -> prerr_endline (sprintf "Unit conversion error: %s" s); flush stderr; "1." (* Use coef 1. *)
| Unknown_conversion _ -> "1." (* Use coef 1. *)
| _ -> "1."
in
Expand Down
4 changes: 2 additions & 2 deletions sw/lib/ocaml/pprz.mli
Expand Up @@ -95,15 +95,15 @@ exception No_automatic_conversion of string * string
* and from_unit or to_unit are empty string
*)

val scale_of_units : string -> string -> float
val scale_of_units : ?auto:string -> string -> string -> float
(** scale_of_units from to
* Returns conversion factor between two units
* The possible conversions are described in conf/units.xml
* May raise Invalid_argument if one of the unit is not valid
* or if units.xml is not valid
*)

val alt_unit_coef_of_xml : Xml.xml -> string
val alt_unit_coef_of_xml : ?auto:string -> Xml.xml -> string
(** Return coef for alternate unit
*)

Expand Down

0 comments on commit 8212e54

Please sign in to comment.