Contents of data stream are pushed and popped to demonstrate usage of CircularBuffer API's.
Fork of mbed-os-example-circular-buffer by
main.cpp
00001 /* 00002 * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "mbed.h" 00018 #include "platform/CircularBuffer.h" 00019 00020 #define BUF_SIZE 10 00021 00022 CircularBuffer<char, BUF_SIZE> buf; 00023 char data_stream[] = "DataToBeAddedToBuffer"; 00024 00025 int main() 00026 { 00027 uint32_t bytes_written = 0; 00028 00029 while (!buf.full()) { 00030 buf.push(data_stream[bytes_written++]); 00031 } 00032 00033 printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 00034 (buf.full()? "true":"false"), 00035 (buf.empty()? "true":"false") ); 00036 printf ("Bytes written %d \n", bytes_written); 00037 00038 // If buffer is full, contents will be over-written 00039 buf.push(data_stream[bytes_written++]); 00040 00041 char data; 00042 printf ("Buffer contents: "); 00043 while (!buf.empty()) { 00044 buf.pop(data); 00045 printf("%c", data); 00046 } 00047 printf("\n"); 00048 00049 printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 00050 (buf.full()? "true":"false"), 00051 (buf.empty()? "true":"false") ); 00052 00053 return 0; 00054 00055 }
Generated on Fri Jul 15 2022 02:38:29 by 1.7.2