Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/actions/exportChartAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { RootState } from '../slices';
import { hideExportDialog } from '../slices/appSlice';
import EventAction from '../usageDataActions';
import { averagedBitState } from '../utils/bitConversion';
import { getSingleBitState } from '../utils/bitConversion';

// create and array of [index, length] to split longer range
const indexer = (indexBegin: number, indexEnd: number, length: number) => {
Expand Down Expand Up @@ -55,7 +55,7 @@ export const formatDataForExport = (
if (bitsData) {
const bitstring = dc.map(
(_, i) =>
['-', '0', '1', 'X'][averagedBitState(bitsData[n], i)]
['-', '0', '1', 'X'][getSingleBitState(bitsData[n], i)]
);
content += selectivePrint(
[
Expand Down
38 changes: 18 additions & 20 deletions src/components/Chart/data/bitDataAccumulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,67 @@ import { numberOfDigitalChannels } from '../../../globals';
import {
always0,
always1,
averagedBitState,
getSingleBitState,
sometimes0And1,
} from '../../../utils/bitConversion';
import bitDataStorage, { BitDataStorage } from './bitDataStorage';
import {
BitStateIndexType,
DigitalChannelStates,
TimestampType,
} from './dataTypes';
import { BitState, DigitalChannelStates, TimestampType } from './dataTypes';

export interface BitDataAccumulator {
bitDataStorage: BitDataStorage;
accumulator: Array<BitStateIndexType | null>;
accumulatedBitStates: Array<BitState | null>;
digitalChannelsToCompute: number[] | undefined;
initialise: (digitalChannelsToCompute: number[]) => void;
processBits: (bits: number) => void;
processBitState: (bitState: BitStateIndexType, channel: number) => void;
processBitState: (bitState: BitState, channel: number) => void;
processAccumulatedBits: (timestamp: TimestampType) => void;
getLineData: () => DigitalChannelStates[];
}

export default (): BitDataAccumulator => ({
bitDataStorage: bitDataStorage(),
accumulator: new Array(numberOfDigitalChannels),
accumulatedBitStates: new Array(numberOfDigitalChannels),
digitalChannelsToCompute: undefined as number[] | undefined,

initialise(digitalChannelsToCompute) {
this.bitDataStorage.initialise(digitalChannelsToCompute);
this.digitalChannelsToCompute = digitalChannelsToCompute;
// .fill is slower then a normal for loop when array is large
for (let i = 0; i < this.accumulator.length; i += 1) {
this.accumulator[i] = null;
for (let i = 0; i < this.accumulatedBitStates.length; i += 1) {
this.accumulatedBitStates[i] = null;
}
},

processBits(bits) {
this.digitalChannelsToCompute!.forEach(i => {
const bitState = averagedBitState(bits, i);
const bitState = getSingleBitState(bits, i);
this.processBitState(bitState, i);
});
},

processBitState(bitState, channel) {
if (this.accumulator[channel] === null) {
this.accumulator[channel] = bitState;
if (this.accumulatedBitStates[channel] === null) {
this.accumulatedBitStates[channel] = bitState;
} else if (
(this.accumulator[channel] === always1 && bitState !== always1) ||
(this.accumulator[channel] === always0 && bitState !== always0)
(this.accumulatedBitStates[channel] === always1 &&
bitState !== always1) ||
(this.accumulatedBitStates[channel] === always0 &&
bitState !== always0)
) {
this.accumulator[channel] = sometimes0And1;
this.accumulatedBitStates[channel] = sometimes0And1;
}
},

processAccumulatedBits(timestamp) {
this.digitalChannelsToCompute!.forEach(i => {
const bitState = this.accumulator[i];
const bitState = this.accumulatedBitStates[i];
if (bitState != null)
this.bitDataStorage.storeBit(timestamp, i, bitState);
});

// .fill is slower then a normal for loop when array is large
for (let i = 0; i < this.accumulator.length; i += 1) {
this.accumulator[i] = null;
for (let i = 0; i < this.accumulatedBitStates.length; i += 1) {
this.accumulatedBitStates[i] = null;
}
},

Expand Down
4 changes: 2 additions & 2 deletions src/components/Chart/data/bitDataSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { numberOfDigitalChannels } from '../../../globals';
import { averagedBitState } from '../../../utils/bitConversion';
import { getSingleBitState } from '../../../utils/bitConversion';
import bitDataStorage, { BitDataStorage } from './bitDataStorage';
import { DigitalChannelStates, TimestampType } from './dataTypes';

Expand All @@ -31,7 +31,7 @@ export default (): BitDataSelector => ({
this.bitDataStorage.storeBit(
timestamp,
i,
averagedBitState(bits, i)
getSingleBitState(bits, i)
);
});
},
Expand Down
8 changes: 4 additions & 4 deletions src/components/Chart/data/bitDataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
import { numberOfDigitalChannels } from '../../../globals';
import { lineDataForBitState } from '../../../utils/bitConversion';
import type {
BitStateType,
BitState,
DigitalChannelStates,
TimestampType,
} from './dataTypes';

export interface BitDataStorage {
lineData: DigitalChannelStates[];
previousBitStates: Array<BitStateType | null>;
previousBitStates: Array<BitState | null>;
digitalChannelsToCompute: number[] | undefined;
latestTimestamp: TimestampType;

initialise: (digitalChannelsToCompute: number[]) => void;
storeEntry: (
timestamp: TimestampType,
bitNumber: number,
bitState: BitStateType
bitState: BitState
) => void;
storeBit: (
timestamp: TimestampType,
bitNumber: number,
bitState: BitStateType
bitState: BitState
) => void;
addFinalEntries: () => void;
getLineData: () => DigitalChannelStates[];
Expand Down
25 changes: 14 additions & 11 deletions src/components/Chart/data/dataAccumulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { always0, always1, sometimes0And1 } from '../../../utils/bitConversion';
import bitDataAccumulator from './bitDataAccumulator';
import {
AmpereState,
BitStateIndexType,
BitStateType,
BitState,
ChartLineValue,
DigitalChannelState,
DigitalChannelStates,
TimestampType,
Expand Down Expand Up @@ -417,22 +417,22 @@ const findMissingRanges = (
let cacheValidTimeGroup: number;
let cachedDigitalChannelsToCompute: number[];

const stateToIndex = (
mainLineState: BitStateType | undefined,
uncertaintyLineState: BitStateType | undefined
): BitStateIndexType => {
const chartLineToBitState = (
mainLineState: ChartLineValue | undefined,
uncertaintyLineState: ChartLineValue | undefined
): BitState => {
if (mainLineState === undefined && uncertaintyLineState === undefined) {
return 0;
}
if (
mainLineState === BitStateType.one &&
uncertaintyLineState === BitStateType.one
mainLineState === ChartLineValue.one &&
uncertaintyLineState === ChartLineValue.one
) {
return always1;
}
if (
mainLineState === BitStateType.zero &&
uncertaintyLineState === BitStateType.zero
mainLineState === ChartLineValue.zero &&
uncertaintyLineState === ChartLineValue.zero
) {
return always0;
}
Expand Down Expand Up @@ -461,7 +461,10 @@ const joinBitLines = (
);
for (let i = 0; i < numberOfElement; i += 1) {
bitDataProcessor?.processBitState(
stateToIndex(line.mainLine[i].y, line.uncertaintyLine[i].y),
chartLineToBitState(
line.mainLine[i].y,
line.uncertaintyLine[i].y
),
index
);

Expand Down
20 changes: 8 additions & 12 deletions src/components/Chart/data/dataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
*/

export type DigitalChannelsType = boolean[];
export type BitStateIndexType = 0 | 1 | 2 | 3;
export type BitNumberType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
export type TimestampType = number | undefined;
export type AmpereStateType = number | null | undefined;

export enum BitStateType {
export type BitState =
| 0b00 // = 0: invalid (undefined)
| 0b01 // = 1: was always 0
| 0b10 // = 2: was always 1
| 0b11; // = 3: was sometimes 0 and sometimes 1

export enum ChartLineValue {
zero = -0.4,
one = 0.4,
}
Expand All @@ -22,7 +27,7 @@ export enum BitStateType {
*/
export interface DigitalChannelState {
x: TimestampType;
y: BitStateType | undefined;
y: ChartLineValue | undefined;
}

export interface DigitalChannelStates {
Expand All @@ -34,12 +39,3 @@ export interface AmpereState {
x: TimestampType;
y: AmpereStateType;
}

export function isBitStateIndexType(
value: unknown
): value is BitStateIndexType {
return (
typeof value === 'number' &&
(value === 0 || value === 1 || value === 2 || value === 3)
);
}
49 changes: 25 additions & 24 deletions src/utils/bitConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
*/

import {
BitStateIndexType,
BitStateType,
} from '../components/Chart/data/dataTypes';
import { BitState, ChartLineValue } from '../components/Chart/data/dataTypes';

/* eslint-disable no-bitwise */

Expand All @@ -29,27 +26,31 @@ export const convertBits16 = (b8: number): number =>
((b8 & 1) + 1);

/**
* Extract out of a 16 bit value the averaged bit state.
* There are 3 valid states as described in the return value.
* Extract out of a 16 bit value the two bits for the single bit state.
*
* E.g. getSingleBitState(0b00011011, 0) => 0b11
* ^^
* E.g. getSingleBitState(0b00011011, 1) => 0b10
* ^^
* E.g. getSingleBitState(0b00011011, 2) => 0b01
* ^^
* E.g. getSingleBitState(0b00011011, 3) => 0b00
* ^^
*
* @param {number} b16 16-bit input of 8 bit-pairs
* @param {number} n index of bit-pair
* @returns {number}
* - 0 (b00): invalid (undefined)
* - 1 (b01): was always 0
* - 2 (b10): was always 1
* - 3 (b11): was sometimes 0 and sometimes 1
* @returns {BitState} the single bit state
*/
export const averagedBitState = (b16: number, n: number): BitStateIndexType =>
(3 & (b16 >> (n + n))) as BitStateIndexType;
export const getSingleBitState = (b16: number, n: number): BitState =>
(3 & (b16 >> (n + n))) as BitState;

export const always0: BitStateIndexType = 1;
export const always1: BitStateIndexType = 2;
export const sometimes0And1: BitStateIndexType = 3;
export const always0: BitState = 1;
export const always1: BitState = 2;
export const sometimes0And1: BitState = 3;

export interface LineDataBitState {
mainLine: BitStateType | undefined;
uncertaintyLine: BitStateType | undefined;
mainLine: ChartLineValue | undefined;
uncertaintyLine: ChartLineValue | undefined;
}

export const lineDataForBitState: LineDataBitState[] = [
Expand All @@ -58,15 +59,15 @@ export const lineDataForBitState: LineDataBitState[] = [
uncertaintyLine: undefined,
},
/* 1: always 0 */ {
mainLine: BitStateType.zero,
uncertaintyLine: BitStateType.zero,
mainLine: ChartLineValue.zero,
uncertaintyLine: ChartLineValue.zero,
},
/* 2: always 1 */ {
mainLine: BitStateType.one,
uncertaintyLine: BitStateType.one,
mainLine: ChartLineValue.one,
uncertaintyLine: ChartLineValue.one,
},
/* sometimes 0 and sometimes 1 */ {
mainLine: BitStateType.zero,
uncertaintyLine: BitStateType.one,
mainLine: ChartLineValue.zero,
uncertaintyLine: ChartLineValue.one,
},
];