#[cfg(feature="std")]
use std::marker::PhantomData;
#[cfg(not(feature="std"))]
use core::marker::PhantomData;
use {
WriteableAxisParameter,
ReadableAxisParameter,
};
pub trait Instruction {
const INSTRUCTION_NUMBER: u8;
fn type_number(&self) -> u8;
fn motor_bank_number(&self) -> u8;
fn operand(&self) -> [u8; 4];
}
pub trait DirectInstruction: Instruction {
type Return: Return;
}
pub trait Return {
fn from_operand(operand: [u8; 4]) -> Self;
}
#[derive(Debug, PartialEq)]
pub struct ROR {
motor_number: u8,
velocity: u32,
}
impl ROR {
pub fn new(motor_number: u8, velocity: u32) -> ROR {ROR{motor_number, velocity}}
}
impl Instruction for ROR {
const INSTRUCTION_NUMBER: u8 = 1;
fn operand(&self) -> [u8; 4] {
return [
(self.velocity & 0xff) as u8,
((self.velocity >> 8) & 0xff) as u8,
((self.velocity >> 16) & 0xff) as u8,
((self.velocity >> 24) & 0xff) as u8
]
}
fn type_number(&self) -> u8 {
0
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl DirectInstruction for ROR {
type Return = ();
}
#[derive(Debug, PartialEq)]
pub struct ROL {
motor_number: u8,
velocity: u32,
}
impl ROL {
pub fn new(motor_number: u8, velocity: u32) -> ROL {ROL{motor_number, velocity}}
}
impl Instruction for ROL {
const INSTRUCTION_NUMBER: u8 = 2;
fn operand(&self) -> [u8; 4] {
return [
(self.velocity & 0xff) as u8,
((self.velocity >> 8) & 0xff) as u8,
((self.velocity >> 16) & 0xff) as u8,
((self.velocity >> 24) & 0xff) as u8
]
}
fn type_number(&self) -> u8 {
0
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl DirectInstruction for ROL {
type Return = ();
}
#[derive(Debug, PartialEq)]
pub struct MST {
motor_number: u8,
}
impl MST {
pub fn new(motor_number: u8) -> MST {MST{motor_number}}
}
impl Instruction for MST {
const INSTRUCTION_NUMBER: u8 = 3;
fn operand(&self) -> [u8; 4] {
return [0, 0, 0, 0]
}
fn type_number(&self) -> u8 {
0
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl DirectInstruction for MST {
type Return = ();
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MoveOperation {
Absolute(i32),
Relative(i32),
Coordinate(u32),
}
#[derive(Debug, PartialEq)]
pub struct MVP {
motor_number: u8,
value: MoveOperation,
}
impl MVP {
pub fn new(motor_number: u8, value: MoveOperation) -> MVP {MVP{motor_number, value}}
}
impl Instruction for MVP {
const INSTRUCTION_NUMBER: u8 = 4;
fn operand(&self) -> [u8; 4] {
match self.value {
MoveOperation::Absolute(x) => {
[
(x & 0xff) as u8,
((x >> 8) & 0xff) as u8,
((x >> 16) & 0xff) as u8,
((x >> 24) & 0xff) as u8
]
},
MoveOperation::Relative(x) => {
[
(x & 0xff) as u8,
((x >> 8) & 0xff) as u8,
((x >> 16) & 0xff) as u8,
((x >> 24) & 0xff) as u8
]
},
MoveOperation::Coordinate(x) => {
[
(x & 0xff) as u8,
((x >> 8) & 0xff) as u8,
((x >> 16) & 0xff) as u8,
((x >> 24) & 0xff) as u8
]
},
}
}
fn type_number(&self) -> u8 {
0
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl DirectInstruction for MVP {
type Return = ();
}
#[derive(Debug, PartialEq)]
pub struct SAP<T: WriteableAxisParameter> {
motor_number: u8,
axis_parameter: T,
}
impl<T: WriteableAxisParameter> SAP<T> {
pub fn new(motor_number: u8, axis_parameter: T) -> SAP<T> {
SAP{
motor_number,
axis_parameter
}
}
}
impl<T: WriteableAxisParameter> Instruction for SAP<T> {
const INSTRUCTION_NUMBER: u8 = 5;
fn operand(&self) -> [u8; 4] {
self.axis_parameter.operand()
}
fn type_number(&self) -> u8 {
T::NUMBER
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl<T: WriteableAxisParameter> DirectInstruction for SAP<T> {
type Return = ();
}
#[derive(Debug, PartialEq)]
pub struct GAP<T: ReadableAxisParameter> {
motor_number: u8,
phantom: PhantomData<T>,
}
impl<T: ReadableAxisParameter> GAP<T> {
pub fn new(motor_number: u8) -> GAP<T> {
GAP{
motor_number,
phantom: PhantomData,
}
}
}
impl<T: ReadableAxisParameter> Instruction for GAP<T> {
const INSTRUCTION_NUMBER: u8 = 6;
fn operand(&self) -> [u8; 4] {
[0u8, 0u8, 0u8, 0u8]
}
fn type_number(&self) -> u8 {
T::NUMBER
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl<T: ReadableAxisParameter> DirectInstruction for GAP<T> {
type Return = T;
}
#[derive(Debug, PartialEq)]
pub struct STAP<T: WriteableAxisParameter> {
motor_number: u8,
phantom: PhantomData<T>,
}
impl<T: WriteableAxisParameter> STAP<T> {
pub fn new(motor_number: u8) -> STAP<T> {
STAP{
motor_number,
phantom: PhantomData,
}
}
}
impl<T: WriteableAxisParameter> Instruction for STAP<T> {
const INSTRUCTION_NUMBER: u8 = 7;
fn operand(&self) -> [u8; 4] {
[0u8, 0u8, 0u8, 0u8]
}
fn type_number(&self) -> u8 {
T::NUMBER
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl<T: WriteableAxisParameter> DirectInstruction for STAP<T> {
type Return = ();
}
#[derive(Debug, PartialEq)]
pub struct RSAP<T: WriteableAxisParameter> {
motor_number: u8,
phantom: PhantomData<T>,
}
impl<T: WriteableAxisParameter> RSAP<T> {
pub fn new(motor_number: u8) -> RSAP<T> {
RSAP {
motor_number,
phantom: PhantomData,
}
}
}
impl<T: WriteableAxisParameter> Instruction for RSAP<T> {
const INSTRUCTION_NUMBER: u8 = 8;
fn operand(&self) -> [u8; 4] {
[0u8, 0u8, 0u8, 0u8]
}
fn type_number(&self) -> u8 {
T::NUMBER
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl<T: WriteableAxisParameter> DirectInstruction for RSAP<T> {
type Return = ();
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ReferenceSearchAction {
Start = 0,
Stop = 1,
Status = 2,
}
#[derive(Debug, PartialEq)]
pub struct RFS {
motor_number: u8,
action: ReferenceSearchAction,
}
impl RFS {
pub fn new(motor_number: u8, action: ReferenceSearchAction) -> RFS {
RFS {
motor_number,
action
}
}
}
impl Instruction for RFS {
const INSTRUCTION_NUMBER: u8 = 13;
fn operand(&self) -> [u8; 4] {
[0u8, 0u8, 0u8, 0u8]
}
fn type_number(&self) -> u8 {
self.action as u8
}
fn motor_bank_number(&self) -> u8 {
self.motor_number
}
}
impl DirectInstruction for RFS {
type Return = bool;
}
#[derive(Debug, PartialEq)]
pub struct SIO {
bank_number: u8,
port_number: u8,
state: bool,
}
impl SIO {
pub fn new(bank_number: u8, port_number: u8, state: bool) -> Self {
SIO {bank_number, port_number, state}
}
}
impl Instruction for SIO {
const INSTRUCTION_NUMBER: u8 = 14;
fn operand(&self) -> [u8; 4] {[self.state as u8, 0u8, 0u8, 0u8]}
fn type_number(&self) -> u8 { self.port_number }
fn motor_bank_number(&self) -> u8 { self.bank_number }
}
impl DirectInstruction for SIO {
type Return = ();
}
#[derive(Debug, PartialEq)]
pub struct GIO {
bank_number: u8,
port_number: u8,
}
impl GIO {
pub fn new(bank_number: u8, port_number: u8) -> Self {
GIO {bank_number, port_number}
}
}
impl Instruction for GIO {
const INSTRUCTION_NUMBER: u8 = 15;
fn operand(&self) -> [u8; 4] {[0u8, 0u8, 0u8, 0u8]}
fn type_number(&self) -> u8 { self.port_number }
fn motor_bank_number(&self) -> u8 { self.bank_number }
}
impl DirectInstruction for GIO {
type Return = u32;
}
#[derive(Debug, PartialEq)]
pub enum CALC {
Add(i32),
Sub(i32),
Mul(i32),
Div(i32),
Mod(i32),
And(i32),
Or(i32),
Xor(i32),
Not,
Load(i32),
}
impl Instruction for CALC {
const INSTRUCTION_NUMBER: u8 = 19;
fn operand(&self) -> [u8; 4] {
match self {
CALC::Add(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Sub(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Mul(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Div(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Mod(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::And(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Or(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Xor(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
CALC::Not => [0u8, 0u8, 0u8, 0u8],
CALC::Load(x) => [(x >> 0) as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8],
}
}
fn type_number(&self) -> u8 {
match self {
CALC::Add(_) => 0,
CALC::Sub(_) => 1,
CALC::Mul(_) => 2,
CALC::Div(_) => 3,
CALC::Mod(_) => 4,
CALC::And(_) => 5,
CALC::Or(_) => 6,
CALC::Xor(_) => 7,
CALC::Not => 8,
CALC::Load(_) => 9,
}
}
fn motor_bank_number(&self) -> u8 { 0 }
}
impl DirectInstruction for CALC {
type Return = ();
}