LoginSignup
2
3

More than 5 years have passed since last update.

Arduino リングバッファー

Last updated at Posted at 2018-05-20

Arduino ノリングバッファーライブラリを使用してみます。
ライブラリマネージャーでCircularBufferと検索してインストールします。:relaxed:

image.png

公式マニュアルなどはここ↓
https://github.com/rlogiacco/CircularBuffer

<宣言>
CircularBuffer buffer; // buffer capacity is 5

<データ書き込み>
・先頭に追加する関数 (最後尾のデータが押し出される。)
buffer.unshift(1);

・最後尾に追加する関数 (先頭のデータが押し出される。)
buffer.push(0);

書き込みsample.
CircularBuffer<int,5> buffer; // buffer capacity is 5

// all of the following return true
buffer.unshift(1); // [1] 
buffer.unshift(2); // [2,1]
buffer.unshift(3); // [3,2,1]
buffer.push(0);  // [3,2,1,0]
buffer.push(5);  // [3,2,1,0,5]

buffer.unshift(2);  // [2,3,2,1,0] returns false
buffer.unshift(10); // [10,2,3,2,1] returns false
buffer.push(-5);  // [2,3,2,1,-5] returns false

<データ読み込み>
・先頭読み込み
 buffer.first();
・最後尾読み込み
 buffer.last();
・最後尾データを取り出し。(最後尾のデータはなくなる)
 buffer.pop();
・先頭データを取り出し。(先頭のデータはなくなる)
 buffer.shift();
・指定場所のデータを読み出し
 buffer[0];

読み込みsample.
CircularBuffer<char, 50> buffer; // ['a','b','c','d','e','f','g']

buffer.first(); // ['a','b','c','d','e','f','g'] returns 'a'
buffer.last(); // ['a','b','c','d','e','f','g'] returns 'g'
buffer.pop(); // ['a','b','c','d','e','f'] returns 'g'
buffer.pop(); // ['a','b','c','d','e'] returns 'f'
buffer.shift(); // ['b','c','d','e'] returns 'a'
buffer.shift(); // ['c','d','e'] returns 'b'
buffer[0]; // ['c','d','e'] returns 'c'
buffer[1]; // ['c','d','e'] returns 'd'
buffer[2]; // ['c','d','e'] returns 'e'

buffer[10]; // ['c','d','e'] returned value is unpredictable
buffer[15]; // ['c','d','e'] returned value is unpredictable

<その他>
isEmpty()
isFull()
available()
capacity() など

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3