MWX Library
v0.1.9
v0.1.9
  • The MWX Library
  • 改版履歴
  • MWXライブラリについて
    • License
    • 用語
    • 設計情報
  • インストール・ビルド
    • 環境 (OSなど)
    • TWELITE SDK のインストール
    • VS Codeのインストール
    • アクトのビルド
    • アクトの実行
      • tweterm.py
    • 新しいプロジェクトの作成
    • ビルド定義 Makefile
    • 他のプラットフォーム
  • サンプルアクト
    • act0 .. 4
    • Scratch
    • Slp_Wk_and_Tx
    • Parent_MONOSTICK
    • PingPong
    • BRD_APPTWELITE
    • BRD_ARIA
    • PAL_AMB
    • PAL_AMB-usenap
    • PAL_AMB-bhv
    • PAL_MAG
    • PAL_MOT-single
    • PAL_MOT-fifo
    • PulseCounter
    • WirelessUART
    • 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()
    • ビヘイビア
      • 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
  • ボード (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>
  • 設定 (STG) - インタラクティブモード
    • <STG_STD>
GitBook提供
このページ内
  • 例
  • 背景
PDFとしてエクスポート
  1. API
  2. 関数
  3. ユーティリティ関数

expand_bytes()

バイト列を分解し変数に格納します。

const uint8_t* expand_bytes(
        const uint8_t* b, const uint8_t* e, ...)

expand_bytes()は、パラメータにuint8_t*型のイテレータの組み合わせを指定します。これは解析対象の先頭と末尾の次のイテレータの指定となります。eの位置まで解析が進んだ場合はエラーとなりnullptrを返します。

展開にエラーがない場合は、次の読み出しを行うイテレータを戻します。

可変数パラメータには以下を指定します。

バイト数

データ長

解説

uint8_t

1

uint16_t

2

ビッグエンディアン並びとして展開する

uint32_t

4

ビッグエンディアン並びとして展開する

uint8_t[N]

N

uint8_t 型の固定長配列

std::pair<char*,N>

N

char*,uint8_t*型の配列と配列長Nのペアmake_pair()で生成できる

例

auto&& rx = the_twelite.receiver.read();

char fourchars[5]{}; 
auto&& np = 
	expand_bytes(rx.get_payload().begin(), rx.get_payload().end()
		, make_pair((uint8_t*)fourchars, 4)
    );

// read rest of payload
uint8_t u8DI_BM_remote = 0xff;
uint16_t au16AI_remote[5];
expand_bytes(np, rx.get_payload().end()
	, u8DI_BM_remote
	, au16AI_remote[0]
	, au16AI_remote[1]
	, au16AI_remote[2]
	, au16AI_remote[3]
	, au16AI_remote[4]
);

この例では、まず4バイトの文字列を読み出しています。ここではmake_pair()を用いて明示的に4バイト分のデータを読み出します。

戻されたイテレータnpをもとに、次のデータを読み出します。次のデータはuint8_t型、あとはuint16_t型が5つ続いています。

背景

無全パケットのデータペイロードの生成やデータの取り出しで用いられるuint8_t型のバイト配列の記述を簡素化するため。

auto&& rx = the_twelite.receiver.read();
char fourchars[5]{}; 
uint8_t u8DI_BM_remote = 0xff;
uint16_t au16AI_remote[5];

uint8_t *p = rx.get_payload().begin();
fourchars[0] = p[0];
fourchars[1] = p[1];
fourchars[2] = p[2];
fourchars[3] = p[3];
fourchars[4] = 0;
p += 4;

u8DI_BM_remote = (p[0] << 8) + p[1]; p+=2;
au16AI_remote[0] = (p[0] << 8) + p[1]; p+=2;
...
auto&& rx = the_twelite.receiver.read();
char fourchars[5]{}; 
uint8_t u8DI_BM_remote = 0xff;
uint16_t au16AI_remote[5];

uint8_t *p = rx.get_payload().begin();
fourchars[0] = G_OCTET(p);
fourchars[1] = G_OCTET(p);
fourchars[2] = G_OCTET(p);
fourchars[3] = G_OCTET(p);
fourchars[4] = 0;

u8DI_BM_remote = G_WORD(p);
au16AI_remote[0] = G_WORD(p);
...
前へpack_bytes()次へCRC8, XOR, LRC

最終更新 3 年前

上記はもっとも単純な記述だが、以下のようにを用いてバイト配列から読み出せる。

Byte array utils