Skip to content

Commit

Permalink
Refactor configs
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jun 9, 2024
1 parent af0cd2c commit db9707e
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 148 deletions.
46 changes: 46 additions & 0 deletions src/PTPDatacode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {BiMap} from 'bim'

import {ConfigName, DriveMode, ExposureMode, WhiteBalance} from './configs'

export enum OpCode {
Undefined = 0x1000,
GetDeviceInfo = 0x1001,
Expand Down Expand Up @@ -195,3 +199,45 @@ export enum PTPAccessCapabilityCode {
ReadOnlyWithoutObjectDeletion = 0x0001,
ReadOnlyWithObjectDeletion = 0x0002,
}

export const ConfigForDevicePropTable = new BiMap<number, ConfigName>([
[0x5001, 'batteryLevel'],
[0x5005, 'whiteBalance'],
[0x5007, 'aperture'],
[0x5008, 'focalLength'],
[0x5009, 'focusDistance'],
[0x500d, 'shutterSpeed'],
[0x500e, 'exposureMode'],
[0x500f, 'iso'],
[0x5010, 'exposureComp'],
[0x5012, 'captureDelay'],
[0x5013, 'driveMode'],
[0x5017, 'colorMode'],
[0x501a, 'timelapseNumber'],
[0x501b, 'timelapseInterval'],
])

export const ExposureModeTable = new BiMap<number, ExposureMode>([
[0x1, 'M'],
[0x2, 'P'],
[0x3, 'A'],
[0x4, 'S'],
[0x5, 'creative'],
[0x6, 'action'],
[0x7, 'portrait'],
])

export const WhiteBalanceTable = new BiMap<number, WhiteBalance>([
[0x1, 'manual'],
[0x2, 'auto'],
[0x3, 'custom'],
[0x4, 'daylight'],
[0x5, 'fluorescent'],
[0x6, 'tungsten'],
])

export const DriveModeTable = new BiMap<number, DriveMode>([
[0x1, 'normal'],
[0x2, 'burst'],
[0x3, 'timelapse'],
])
30 changes: 10 additions & 20 deletions src/Tethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ConfigNameList,
ConfigType,
DriveMode,
ExposureComp,
ExposureMeteringMode,
ExposureMode,
FlashMode,
Expand All @@ -19,10 +20,15 @@ import {
FunctionalMode,
ISO,
ManualFocusOption,
ShutterSpeed,
WhiteBalance,
} from './configs'
import {TethrObject} from './TethrObject'
import {isntNil} from './util'
import {
isntNil,
UnsupportedConfigDesc,
UnsupportedOperationResult,
} from './util'

export type OperationResultStatus =
| 'ok'
Expand Down Expand Up @@ -85,22 +91,6 @@ type ConfigDescGetters = {
>
}

export const UnsupportedConfigDesc: ConfigDesc<any> = {
writable: false,
value: null,
}

export const UnsupportedOperationResult: OperationResult<any> = {
status: 'unsupported',
}

export function createReadonlyConfigDesc<T>(value: T): ConfigDesc<T> {
return {
writable: false,
value,
}
}

export abstract class Tethr
extends EventEmitter<EventTypes>
implements ConfigSetters, ConfigGetters, ConfigDescGetters
Expand Down Expand Up @@ -432,7 +422,7 @@ export abstract class Tethr
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async setExposureComp(value: string): Promise<OperationResult> {
async setExposureComp(value: ExposureComp): Promise<OperationResult> {
return UnsupportedOperationResult
}
async getExposureComp() {
Expand Down Expand Up @@ -697,13 +687,13 @@ export abstract class Tethr
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async setShutterSpeed(value: string): Promise<OperationResult> {
async setShutterSpeed(value: ShutterSpeed): Promise<OperationResult> {
return UnsupportedOperationResult
}
async getShutterSpeed() {
return (await this.getShutterSpeedDesc()).value
}
async getShutterSpeedDesc(): Promise<ConfigDesc<string>> {
async getShutterSpeedDesc(): Promise<ConfigDesc<ShutterSpeed>> {
return UnsupportedConfigDesc
}

Expand Down
19 changes: 10 additions & 9 deletions src/TethrPTPUSB/TethrPTPUSB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import {times} from 'lodash'
import {
Aperture,
BatteryLevel,
ConfigForDevicePropTable,
DriveMode,
DriveModeTable,
ExposureMode,
ExposureModeTable,
ISO,
WhiteBalance,
WhiteBalanceTable,
} from '../configs'
import {DeviceInfo} from '../DeviceInfo'
import {
Expand All @@ -24,19 +20,24 @@ import {
PTPStorageTypeCode,
ResCode,
} from '../PTPDatacode'
import {
ConfigForDevicePropTable,
DriveModeTable,
ExposureModeTable,
WhiteBalanceTable,
} from '../PTPDatacode'
import {PTPDataView} from '../PTPDataView'
import {PTPDevice, PTPEvent} from '../PTPDevice'
import {
ConfigDesc,
ConfigDescOption,
createReadonlyConfigDesc,
OperationResult,
TakePhotoOption,
Tethr,
} from '../Tethr'
import {TethrObject, TethrObjectInfo} from '../TethrObject'
import {TethrStorage} from '../TethrStorage'
import {isntNil, toHexString} from '../util'
import {isntNil, readonlyConfigDesc, toHexString} from '../util'

type DevicePropScheme<T, D extends DatatypeCode> = {
devicePropCode: number
Expand Down Expand Up @@ -146,7 +147,7 @@ export class TethrPTPUSB extends Tethr {
async getCanTakePhotoDesc() {
const {operationsSupported} = await this.getDeviceInfo()
const value = operationsSupported.includes(OpCode.InitiateCapture)
return createReadonlyConfigDesc(value)
return readonlyConfigDesc(value)
}

setCaptureDelay(value: number) {
Expand Down Expand Up @@ -323,12 +324,12 @@ export class TethrPTPUSB extends Tethr {

async getManufacturerDesc() {
const value = (await this.getDeviceInfo()).manufacturer
return createReadonlyConfigDesc(value)
return readonlyConfigDesc(value)
}

async getModelDesc() {
const value = (await this.getDeviceInfo()).model
return createReadonlyConfigDesc(value)
return readonlyConfigDesc(value)
}

setWhiteBalance(value: WhiteBalance) {
Expand Down
23 changes: 9 additions & 14 deletions src/TethrPTPUSB/TethrPanasonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ import {
import {ObjectFormatCode, ResCode} from '../PTPDatacode'
import {PTPDataView} from '../PTPDataView'
import {PTPDevice, PTPEvent} from '../PTPDevice'
import {
ConfigDesc,
createReadonlyConfigDesc,
OperationResult,
TakePhotoOption,
} from '../Tethr'
import {ConfigDesc, OperationResult, TakePhotoOption} from '../Tethr'
import {TethrObject, TethrObjectInfo} from '../TethrObject'
import {isntNil} from '../util'
import {isntNil, readonlyConfigDesc} from '../util'
import {TethrPTPUSB} from './TethrPTPUSB'

enum OpCodePanasonic {
Expand Down Expand Up @@ -258,7 +253,7 @@ export class TethrPanasonic extends TethrPTPUSB {
}

async getManualFocusOptionsDesc() {
return createReadonlyConfigDesc([
return readonlyConfigDesc([
'near:2',
'near:1',
'far:1',
Expand All @@ -267,19 +262,19 @@ export class TethrPanasonic extends TethrPTPUSB {
}

async getCanTakePhotoDesc() {
return createReadonlyConfigDesc(true)
return readonlyConfigDesc(true)
}

async getCanRunAutoFocusDesc() {
return createReadonlyConfigDesc(true)
return readonlyConfigDesc(true)
}

async getCanRunManualFocusDesc() {
return createReadonlyConfigDesc(true)
return readonlyConfigDesc(true)
}

async getCanStartLiveviewDesc() {
return createReadonlyConfigDesc(true)
return readonlyConfigDesc(true)
}

async setColorTemperature(value: number) {
Expand Down Expand Up @@ -506,9 +501,9 @@ export class TethrPanasonic extends TethrPTPUSB {
return null
}
if ((value & 0x80000000) === 0x00000000) {
return '1/' + value / 1000
return `1/${value / 1000}` as const
} else {
return ((value & 0x7fffffff) / 1000).toString()
return `${(value & 0x7fffffff) / 1000}` as const
}
},
valueSize: 4,
Expand Down
13 changes: 6 additions & 7 deletions src/TethrPTPUSB/TethrRicohTheta.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {BiMap} from 'bim'
import {ConfigDesc} from 'src/Tethr'

import {ExposureMode, FocalLength, WhiteBalance} from '../configs'
import {
ExposureMode,
DatatypeCode,
DevicePropCode,
ExposureModeTable,
FocalLength,
WhiteBalance,
WhiteBalanceTable,
} from '../configs'
import {DatatypeCode, DevicePropCode} from '../PTPDatacode'
} from '../PTPDatacode'
import {TethrPTPUSB} from './TethrPTPUSB'

enum DevicePropCodeRicohTheta {
Expand Down Expand Up @@ -97,10 +96,10 @@ export class TethrRicohTheta extends TethrPTPUSB {
const fraction = Number(num & BigInt(0xffffffff))

if (denominator === 1 || denominator === 10) {
return (fraction / denominator).toString()
return `${fraction / denominator}` as const
}

return fraction + '/' + denominator
return `${fraction}/${denominator}` as const
},
})
}
Expand Down
Loading

0 comments on commit db9707e

Please sign in to comment.