Mbed OS 文档翻译 之 参考(API(平台(CircularBuffer)))

本文介绍了一个通用的CircularBuffer类,该类支持不同类型的数据,并提供了一系列关键的方法,如push、pop、empty、full等,用于高效地管理和操作环形缓冲区。通过一个C++示例程序演示了如何使用此类进行数据的读写。
摘要由CSDN通过智能技术生成

CircularBuffer

CircularBuffer 类提供了用于从缓冲区推送和弹出数据的 API。在推送数据之前,应检查缓冲区是否已满,因为完整缓冲区会覆盖数据。在执行弹出操作之前,空 API 可用于检查缓冲区中的内容。

CircularBuffer 类是中断安全的;所有数据操作都在关键部分内执行。

CircularBuffer 是一个支持不同数据类型的模板化类。CircularBuffer 类的声明必须指定数据类型和缓冲区大小。

声明示例

这是 BUF_SIZE长 整数 CircularBuffer 的一个例子:

CircularBuffer<int, BUF_SIZE> buf;

CircularBuffer 类参考

mbed::CircularBuffer< T, BufferSize, CounterType > 类模板参考

公共成员函数
void push (const T &data)
bool pop (T &data)
bool empty () const
bool full () const
void reset ()
CounterType size () const
bool peek (T &data) const

CircularBuffer 示例

main.cpp                                                                                                                                           导入到 Mbed IDE

/*
 * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
#include "platform/CircularBuffer.h"

#define BUF_SIZE    10

CircularBuffer<char, BUF_SIZE> buf;
char data_stream[] = "DataToBeAddedToBuffer";

int main()
{
    uint32_t bytes_written = 0;
    
    while (!buf.full()) {
        buf.push(data_stream[bytes_written++]);
    }
    
    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 
           (buf.full()? "true":"false"), 
           (buf.empty()? "true":"false") );
    printf ("Bytes written %d \n", bytes_written);
    
    // If buffer is full, contents will be over-written
    buf.push(data_stream[bytes_written++]);
    
    char data;
    printf ("Buffer contents: ");
    while (!buf.empty()) {
        buf.pop(data);
        printf("%c", data);
    }
    printf("\n");

    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 
           (buf.full()? "true":"false"), 
           (buf.empty()? "true":"false") );

    return 0;
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值