Sfoglia il codice sorgente

added helper file for generic functions. started bitfield.

Tomi Cvetic 8 anni fa
parent
commit
40fb722d34
4 ha cambiato i file con 216 aggiunte e 0 eliminazioni
  1. 55 0
      src/bitfield/index.js
  2. 61 0
      src/bitfield/initialData.js
  3. 56 0
      src/helpers.js
  4. 44 0
      src/helpers.test.js

+ 55 - 0
src/bitfield/index.js

@@ -0,0 +1,55 @@
+/**
+ * index.js
+ *
+ * If you need this component, import the default exports
+ * in this file. It gives you a reference for everything
+ * needed to interact with it.
+ **/
+
+import * as constants from './constants'
+import { actions, reducer } from './state'
+import * as components from './components'
+// import { createSelector } from 'reselect'
+// import { NAME } from './constants'
+// import _ from 'lodash'
+
+const filters = {
+  all: projects => projects.filter(p => projects.active)
+}
+
+const selectors = {
+  getAll: state => state[constants.NAME],
+  getActive: () => []
+}
+
+/* export const getAll = state => state[NAME]
+export const getActive = _.compose(filterActive, getAll)
+export const getCounts = createSelector(
+    getAll,
+    getActive,
+    (allProjects, activeProjects) => ({
+      all: allProjects.length,
+      active: activeProjects.length
+    })
+) */
+
+const bitfield = {
+  name: '',
+  description: '',
+  nrOfRegisters: 0,
+  bitsPerRegister: 0,
+  modes: []
+}
+
+const setting = {
+  name: '',
+  description: '',
+  nrOfBits: 0,
+  bitMap: [],
+  comment: '',
+  synonyms: [],
+  resetValue: 0,
+  defaultValues: []
+}
+
+export default { actions, constants, components, filters, selectors, reducer }

+ 61 - 0
src/bitfield/initialData.js

@@ -0,0 +1,61 @@
+export const bitfield = {
+  name: 'RF I2C Table',
+  description: 'RF I2C Table for the Jungfrau RF receiver.',
+  nrOfRegisters: 3,
+  bitsPerRegister: 8,
+  modes: [
+    'GPS only',
+    'Multi-GNSS'
+  ]
+  registers: {
+    0: 'iic_lna_ctrl_reg',
+    1: 'iic_mxr_ctrl_a_reg',
+    2: 'iic_mxr_ctrl_b_reg',
+    3: 'iic_mxr_seltf_reg'
+  }
+}
+
+export const settings = [
+{
+  name: 'lna_trim',
+  description: 'LNA trimming',
+  nrOfBits: 3,
+  bitMap: [3, 2, 1],
+  comment: '',
+  values: {
+    func: (value) => Math.pow(2, value)
+  }
+  resetValue: 0,
+  defaultValues: [
+    0,
+    0
+  ]
+}, {
+  name: 'lna_en',
+  description: 'LNA bias enable',
+  nrOfBits: 1,
+  bitMap: [4],
+  comment: '',
+  values: {
+    func: (value) => Math.pow(2, value)
+  }
+  resetValue: 0,
+  defaultValues: [
+    1,
+    1
+  ]
+}, {
+  name: 'lna_trim',
+  description: 'LNA trimming',
+  nrOfBits: 3,
+  bitMap: [3, 2, 1],
+  comment: '',
+  values: {
+    func: (value) => `${}uA`
+  }
+  resetValue: 0,
+  defaultValues: [
+    0,
+    0
+  ]
+}

+ 56 - 0
src/helpers.js

@@ -0,0 +1,56 @@
+export function lockRange (value, nrOfBits) {
+  const lowerValue = 0
+  const upperValue = Math.pow(2, nrOfBits) - 1
+  if (value < lowerValue) {
+    return lowerValue
+  } else if (value > upperValue) {
+    return lowerValue
+  } else {
+    return value
+  }
+}
+
+const prefixMap = {
+  'y': 1e-24,
+  'z': 1e-21,
+  'a': 1e-18,
+  'f': 1e-15,
+  'p': 1e-12,
+  'n': 1e-9,
+  'u': 1e-6,
+  'm': 1e-3,
+  'k': 1e3,
+  'M': 1e6,
+  'G': 1e9,
+  'T': 1e12,
+  'P': 1e15,
+  'E': 1e18,
+  'Z': 1e21,
+  'Y': 1e24
+}
+const REGEX_ENG = /^([+-])?\s*(\d*\.?\d+|\d+\.?\d*)?([yzafpnumkMGTPTEZY]|e[+-]?\d+)?$/
+
+export function printEng (number) {
+  const base = Math.floor(Math.floor(Math.log10(Math.fabs(number)) / 3))
+  return base
+}
+
+export function parseEng (string) {
+  const match = string.match(REGEX_ENG)
+  if (!match) {
+    return null
+  }
+  let num = 0
+  let sign = 1
+  let exp = 0
+  if (match[1] === '-') {
+    sign = -1
+  }
+  num = parseFloat(match[2])
+  if (match[3] && match[3].length > 1) {
+    exp = parseInt(match[3].substring(1))
+  } else if (match[3]) {
+    exp = prefixMap[match[3]]
+  }
+  return sign * num * Math.pow(10, exp)
+}

+ 44 - 0
src/helpers.test.js

@@ -0,0 +1,44 @@
+import * as helpers from './helpers'
+
+describe('Engineering notation', () => {
+  describe('Print numbers', () => {
+    it('prints numbers in engineering notation', () => {
+      // expect(helpers.printEng(1.2)).toBe('1.2')
+      // expect(helpers.printEng(-1.2)).toBe('-1.2')
+      // expect(helpers.printEng(12345678901234.56789)).toBe('12.347T')
+      // expect(helpers.printEng(-12345678901234.56789)).toBe('-12.347T')
+      // expect(helpers.printEng(0.000000000012345678)).toBe('12.347p')
+      // expect(helpers.printEng(-0.000000000012345678)).toBe('-12.347p')
+    })
+  })
+  describe('Parse numbers', () => {
+    it('parses engineering notation to numbers', () => {
+      // simple numbers
+      expect(helpers.parseEng('1')).toBe(1)
+      expect(helpers.parseEng('-2')).toBe(-2)
+      // trailing comma is ok
+      expect(helpers.parseEng('3.')).toBe(3)
+      expect(helpers.parseEng('-4.')).toBe(-4)
+      // leading comma is ok
+      expect(helpers.parseEng('.5')).toBe(0.5)
+      expect(helpers.parseEng('-.6')).toBe(-0.6)
+      // space between sign and number is ok
+      expect(helpers.parseEng('+  7.1')).toBe(7.1)
+      expect(helpers.parseEng('- 8.2')).toBe(-8.2)
+      // many digits before and after comma are ok
+      expect(helpers.parseEng('+00234329.123000')).toBe(234329.123)
+      expect(helpers.parseEng('-00001234.342500')).toBe(-1234.3425)
+      //
+    })
+    it('parses zero correctly', () => {
+      expect(helpers.parseEng('0')).toBe(0)
+      expect(helpers.parseEng('-0')).toBe(0)
+      expect(helpers.parseEng('0.')).toBe(0)
+      expect(helpers.parseEng('-0.')).toBe(0)
+      expect(helpers.parseEng('.0')).toBe(0)
+      expect(helpers.parseEng('-.0')).toBe(0)
+      expect(helpers.parseEng('+  .0')).toBe(0)
+      expect(helpers.parseEng('-   .0')).toBe(0)
+    })
+  })
+})