Farmbot-Web-App/frontend/devices/components/hardware_settings/encoder_type.tsx

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
import { McuParams, Encoder, McuParamName } from "farmbot/dist";
import { FBSelect, DropDownItem } from "../../../ui/index";
2019-04-02 13:59:37 -06:00
import { t } from "../../../i18next_wrapper";
2017-06-29 12:54:02 -06:00
2017-11-10 14:55:30 -07:00
export interface EncoderTypeProps {
2017-06-29 12:54:02 -06:00
hardware: McuParams;
onChange(key: McuParamName, value: Encoder): void;
}
2017-12-14 16:39:52 -07:00
export const LOOKUP: { [name: string]: DropDownItem } = {
2017-06-29 12:54:02 -06:00
[Encoder.differential]: { label: "Differential", value: Encoder.differential },
[Encoder.quadrature]: { label: "Single-Ended", value: Encoder.quadrature },
DEFAULT: { label: "---", value: Encoder.unknown }
2017-08-23 16:26:09 -06:00
};
2017-06-29 12:54:02 -06:00
const OPTIONS = [LOOKUP[Encoder.differential], LOOKUP[Encoder.quadrature]];
const KEYS: McuParamName[] = [
"encoder_type_x",
"encoder_type_y",
"encoder_type_z"
];
export function isEncoderValue(x: unknown): x is Encoder {
return !!Encoder[parseInt("" + x)];
}
2017-06-29 12:54:02 -06:00
2017-12-14 16:39:52 -07:00
export function findByType(input: number | string | undefined) {
2017-06-29 12:54:02 -06:00
return LOOKUP[input || "DEFAULT"] || LOOKUP.DEFAULT;
}
export function EncoderType(props: EncoderTypeProps) {
2017-08-28 05:49:13 -06:00
const { hardware } = props;
const handleChange = (key: McuParamName) => (d: DropDownItem) => {
const val = d.value;
2017-06-29 12:54:02 -06:00
if (isEncoderValue(val)) {
props.onChange(key, val);
} else {
throw new Error("Got bad encoder type in device panel.");
}
2017-08-23 16:26:09 -06:00
};
2017-06-29 12:54:02 -06:00
return <tr>
<td>
<label>{t("ENCODER TYPE")}</label>
<div className="help">
<i className="fa fa-question-circle help-icon" />
<div className="help-text">
{t(`Rotary encoder type. Differential encoders use
A, A+, B, and B+ channels. Single-Ended encoders use
A and B channels.`)}
</div>
</div>
</td>
{KEYS.map(function (key, inx) {
return <td key={inx}>
<FBSelect selectedItem={findByType(hardware.encoder_type_z)}
list={OPTIONS}
onChange={handleChange(key)} />
</td>;
})}
</tr>;
}