MWX Library
v0.2.0
v0.2.0
  • The MWX Library
  • 改版履歴
  • MWXライブラリについて
    • License
    • 用語
    • 設計情報
  • インストール・ビルド
    • 環境 (OSなど)
    • TWELITE SDK のインストール
    • アクトのビルド
    • 新しいプロジェクトの作成
    • VSCodeのインストール
    • ビルド定義 Makefile
    • 他のプラットフォーム
  • サンプルアクト
    • act0 .. 4
    • Scratch
    • Slp_Wk_and_Tx
    • Parent_MONOSTICK
    • PingPong
    • BRD_APPTWELITE
    • BRD_I2C_TEMPHUMID
    • BRD_ARIA
    • PAL_AMB
    • PAL_AMB-usenap
    • PAL_AMB-bhv
    • PAL_MAG
    • PAL_MOT-single
    • PAL_MOT-fifo
    • PulseCounter
    • WirelessUART
    • Rcv_Univsl
    • Unit_???
  • API
    • 定義
    • クラスオブジェクト
      • the_twelite
      • Analogue
      • Buttons
      • EEPROM
      • PulseCounter
      • Serial
      • SerialParser
      • SPI
        • SPI (メンバ関数版)
        • SPI (ヘルパークラス版)
      • TickTimer
      • Timer0 .. 4
      • Wire
        • Wire (メンバ関数版)
        • Wire (ヘルパークラス版)
    • クラス
      • MWX_APIRET
      • alloc
      • axis_xyzt
      • packet_rx
      • packet_tx
      • serparser
      • pktparser
        • E_PKT
        • idenify_packet_type()
        • TwePacket
          • TwePacketTwelite
          • TwePacketIO
          • TwePacketUART
          • TwePacketPAL
      • smplbuf
        • .get_stream_helper()
        • smplbuf_strm_u8
      • smplque
      • mwx::stream
        • format (mwx::mwx_format)
        • mwx::bigendian
        • mwx::crlf
        • mwx::flush
        • stream_helper
      • SM_SIMPLE ステートマシン
    • コールバック関数
      • setup()
      • begin()
      • loop()
      • wakeup()
      • init_coldboot()
      • init_warmboot()
      • on_rx_packet()
      • on_tx_comp()
    • ビヘイビア
      • PAL_AMB-behavior
    • 関数
      • システム関数
        • millis()
        • delay()
        • delayMicroseconds()
        • random()
      • DIO 汎用ディジタルIO
        • pinMode()
        • digitalWrite()
        • digitalRead()
        • attachIntDio()
        • detachIntDio()
        • digitalReadBitmap()
      • ユーティリティ関数
        • Printf utils
        • pack_bits()
        • collect_bits()
        • Byte array utils
        • pack_bytes()
        • expand_bytes()
        • CRC8, XOR, LRC
        • div100()
        • Scale utils
        • pnew
    • 外部ライブラリ
      • EASTL
  • ボード (BRD)
    • <BRD_APPTWELITE>
    • <MONOSTICK>
    • PAL
      • <PAL_AMB>
      • <PAL_MAG>
      • <PAL_MOT>
      • <PAL_NOTICE>
    • <CUE>
    • <ARIA>
  • センサー・デバイス (SNS)
    • SHTC3 - 温湿度センサー
    • SHT3x - 温湿度センサー
    • LTR-308ALS - 照度センサー
    • MC3630 - 加速度センサー
    • BMx280 - 環境センサー
    • PCA9632 - LEDドライバ
    • SHT4x - 温湿度センサー
  • ネットワーク (NWK)
    • シンプル中継ネット <NWK_SIMPLE>
    • レイヤー ツリー ネット <NWK_LAYERED>
  • 設定 (STG) - インタラクティブモード
    • <STG_STD>
GitBook提供
このページ内
  • scale_1000_to_127u8()
  • scale_127u8_to_1000()
  • scale_1000_to_255u8()
  • scale_255u8_to_1000()
  • scale_1000_to_256u8()
  • scale_256u16_to_1000()
  • 背景
PDFとしてエクスポート
  1. API
  2. 関数
  3. ユーティリティ関数

Scale utils

除算(`x*1000/255`)の替わり

8bit値(0..255など)の値とユーザが取り扱いやすい0..1000(千分率, ‰)値でスケール(拡縮)する。低い演算コストで実施するため、除算(x*1000/255)の替わりに乗算とビットシフトによって近似計算します。

static inline uint8_t scale_1000_to_127u8(uint16_t x)
static inline uint16_t scale_127u8_to_1000(uint8_t x)
static inline uint8_t scale_1000_to_255u8(uint16_t x)
static inline uint16_t scale_255u8_to_1000(uint8_t x)
static inline uint8_t scale_1000_to_256u8(uint16_t x)
static inline uint16_t scale_256u16_to_1000(uint16_t x)

scale_1000_to_127u8()

0..1000 を 0..127 にスケールします。(16646*x+65000)>>17を用いて近似計算します。

scale_127u8_to_1000()

0..127 を 0..1000 にスケールします。(2064000UL*x+131072)>>18を用いて近似計算します。

scale_1000_to_255u8()

0..1000 を 0..255 にスケールします。(33423*x+65000)>>17を用いて近似計算します。

scale_255u8_to_1000()

0..255 を 0..1000 にスケールします。(1028000UL*uint32_t(x)+131072)>>18を用いて近似計算します。

scale_1000_to_256u8()

0..1000 を 0..256 にスケールします。(33554*x+66000) >> 17を用いて近似計算します。

注: x=999,1000は計算値が256になりますがuint8_tの範囲として255を返します。

scale_256u16_to_1000()

0..256 を 0..1000 にスケールします。(1028000UL*uint32_t(x)+131072)>>18を用いて近似計算します。

背景

ハードウェアに設定すべき値は0..255といった2進数が前提になることが多く、ユーザアプリケーションで取り扱う数は0..1000といった10進数基準のほうが扱いやすい。これらのスケール変換に除算を行わない式を定義した。

前へdiv100()次へpnew

最終更新 2 年前