Last active
July 29, 2019 13:21
-
-
Save fvilante/a4652afc92114d1440649bd325a9174b to your computer and use it in GitHub Desktop.
A fully static driver configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CmppParamType, Milimeter, Space, Speed, Acceleration, MilimeterPerSecond, MilimeterPerSquareSecond, Pulse } from './application-types' | |
import { mapObjectIndexed } from '@nextrobot/core-utils'; | |
//import { ParamMemmap } from './memmap-base'; | |
// drive instance | |
// core | |
type AnyEntry = { readonly type: unknown } | |
type AnySetup = { readonly [K:string]: AnyEntry } | |
// core utils | |
type GetKeys<T extends AnySetup> = Extract<keyof T, string> | |
type GetEntry<T extends AnySetup, K extends GetKeys<T>> = T[K] | |
type GetType<T extends AnySetup, K extends GetKeys<T>> = GetEntry<T, K>['type'] | |
type GetTypes<T extends AnySetup> = GetType<T, GetKeys<T>> | |
// ==== | |
// example of use of utils: | |
type MySetup1 = { | |
readonly 'A': { readonly type: Space } | |
readonly 'B': { readonly type: Speed } | |
readonly 'C': { readonly type: Acceleration } | |
} | |
type MySetup2 = { | |
readonly 'A': { readonly type: Space } // same name and same type | |
readonly 'B': { readonly type: Acceleration } //same name different type | |
readonly 'Z': { readonly type: Acceleration } // diferente name | |
} | |
//put mouse over variables bellow to see `type inference` | |
type Keys = GetKeys<MySetup1> | |
type Types = GetTypes<MySetup1> | |
type TypeB1 = GetType<MySetup1, 'B'> | |
type TypeB2 = GetType<MySetup2, 'B'> | |
// derived program | |
type UserProgram<T extends AnySetup> = { | |
[K in GetKeys<T>]: GetType<T, K> | |
} | |
type MyUserProgram1 = UserProgram<MySetup1> | |
type MyUserProgram2 = UserProgram<MySetup2> | |
// derived memmap | |
type Wave = { readonly wave: number } | |
type Memmap<T extends AnySetup> = { | |
[K in GetKeys<T>]: { | |
readonly option1: string, | |
readonly option2: number, | |
readonly toWave: (value: GetType<T,K>) => Wave | |
readonly fromWave: (value: Wave) => GetType<T,K> | |
} | |
} | |
type MyMemmap1 = Memmap<MySetup1> | |
type MyMemmap2 = Memmap<MySetup2> | |
// use | |
// Driver | |
const MyMemmap1 : MyMemmap1 = { | |
'A': { option1: '', option2: 0, toWave: (space) => ({wave:0}), fromWave: (wave) => Pulse(10) }, | |
'B': { option1: '', option2: 0, toWave: (speed) => ({wave:0}), fromWave: (wave) => MilimeterPerSecond(10) }, | |
'C': { option1: '', option2: 0, toWave: (accel) => ({wave:0}), fromWave: (wave) => MilimeterPerSquareSecond(10) }, | |
} | |
const MyMemmap2 : MyMemmap2 = { | |
'A': { option1: '', option2: 0, toWave: (space) => ({wave:0}), fromWave: (wave) => Pulse(10) }, | |
'B': { option1: '', option2: 0, toWave: (speed) => ({wave:0}), fromWave: (wave) => MilimeterPerSquareSecond(10) }, | |
'Z': { option1: '', option2: 0, toWave: (speed) => ({wave:0}), fromWave: (wave) => MilimeterPerSquareSecond(10) }, | |
} | |
const mapUserProgramToWave = <T extends AnySetup>(program: UserProgram<T>, memmap: Memmap<T>) => { | |
return mapObjectIndexed( program, (value, key) => { | |
return memmap[key].toWave(value) | |
}) | |
} | |
// use | |
const programA: MyUserProgram1 = { | |
'A': Milimeter(10), | |
'B': MilimeterPerSecond(20), | |
'C': MilimeterPerSquareSecond(30), | |
} | |
const programAWaved = mapUserProgramToWave(programA, MyMemmap1) | |
// | |
console.log('ProgramaA') | |
console.log(programA) | |
console.log('ProgramaA - Waved') | |
console.log(programAWaved) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment