{"id":16147886,"url":"https://github.com/bakercp/bufferutils","last_synced_at":"2025-04-24T00:49:43.670Z","repository":{"id":18354093,"uuid":"21534081","full_name":"bakercp/BufferUtils","owner":"bakercp","description":"An Arduino library to facilitate working with raw byte arrays.","archived":false,"fork":false,"pushed_at":"2017-11-22T00:53:00.000Z","size":192,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T00:49:37.429Z","etag":null,"topics":["arduino","buffers","bytes","c-plus-plus","communication"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bakercp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-06T05:17:07.000Z","updated_at":"2023-01-08T10:09:07.000Z","dependencies_parsed_at":"2022-08-28T19:00:57.457Z","dependency_job_id":null,"html_url":"https://github.com/bakercp/BufferUtils","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakercp%2FBufferUtils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakercp%2FBufferUtils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakercp%2FBufferUtils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakercp%2FBufferUtils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bakercp","download_url":"https://codeload.github.com/bakercp/BufferUtils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250540956,"owners_count":21447426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","buffers","bytes","c-plus-plus","communication"],"created_at":"2024-10-10T00:28:19.756Z","updated_at":"2025-04-24T00:49:43.651Z","avatar_url":"https://github.com/bakercp.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"BufferUtils\n===========\n\n[![Build Status](https://travis-ci.org/bakercp/BufferUtils.svg?branch=master)](https://travis-ci.org/bakercp/BufferUtils)\n\n## Description\n\nAn Arduino library to facilitate working with raw byte arrays.\n\n## Features\n\n- Javascript `DataView` style byte buffer access for reading (`BufferReader`) and writing (`BufferWriter`).\n- A `Print` interface for a raw byte buffer via `BufferPrinter`\n- A circular buffer (aka ring buffer or cyclic buffer) implementation for data arrays of any type in `CircularBuffer`.\n\n## Background\n\nWhen working with memory constrained systems, low-level communication protocols and other specialized code, it is very useful to be able to reduce everything down to an array of bytes, (aka byte buffers). Just about all quantitative data can be interpreted as arrays of bytes and this library will help you do that without too much complex C/C++ syntax.\n\n## Use\n\n### BufferReader\n\nSay you have an array of bytes representing a communication packet with the form\n\n```\nPACKET TYPE uint16_t\nPACKET LENGTH uint32_t\nPACKET DATA variable array of uint8_t\nCRC32 CHECKSUM uint32_t\n```\n\nOne could read this data from the buffer like so:\n\n```c++\nuint8_t rawData[128] = { 0x00, 0x01, ..., 0x22 };\n\nuint16_t packetType;\nuint32_t packetLength;\nuint8_t data[256];\nuint32_t checksum;\n\nBufferReader reader(rawData, 128);\n\n// Fill packetType with the correct value.\nreader.read(packetType);\n\n// Fill packet length with the correct value.\nreader.read(packetLength);\n\n// Fill the data with the correct data using the variable packet length.\nreader.read(data, packetLength);\n\n// Fill the checksum.\nreader.read(checksum);\n\n```\n\nThe `BufferReader` keeps track of the current read position based on the sizes of the data types read. Each `read` operation returns the number of bytes read, or zero if there is a read error (e.g. running out of buffer to read).  Note the above example doesn't have any error checking.\n\n### BufferWriter\n\nSimilarly, if we want to write a packet like that described above we might do the following:\n\n```c++\nuint8_t sendBuffer[128];\n\nuint16_t packetType = 13;\nuint32_t packetLength = 11;\nuint8_t data[11] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };\nuint32_t checksum = 0x4A17B156;\n\nBufferWriter writer(sendBuffer, 128);\n\n// Write the packetType to the send buffer.\nwriter.write(packetType);\n\n// Write the packetLength to the send buffer.\nwriter.write(packetLength);\n\n// Write the data to the send buffer.\nreader.read(data, packetLength);\n\n// Write the checksum.\nreader.read(checksum);\n\n// Send the data that was written to the buffer.\nSerial.write(sendBuffer, reader.getOffset());\n```\n\n### BufferPrinter\n\n`BufferPrinter` is much like `BufferWriter` but implements the Arduino `Print` interface.\n\n```c++\nuint8_t bufferToFill[128];\n\nBufferPrinter printer(bufferToFill, 128);\n\n// Write the packetType to the send buffer.\nprinter.println(\"Hello there.\");\n\n// All Print commands are available.\nprinter.print(\".\");\n\n// Print commands including ASCII translations.\nprinter.print(99, HEX);\n\n// Send the data that was written to the buffer.\nSerial.write(bufferToFill, printer.getOffset());\n```\n\n### CircularBuffer\n\nSee the `CircularBuffer` example.\n\n## Examples\n\nSee the included examples and tests for further usage examples.\n\n## Changelog\nSee [CHANGELOG.md](CHANGELOG.md)\n\n\n## License\nSee [LICENSE.md](LICENSE.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbakercp%2Fbufferutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbakercp%2Fbufferutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbakercp%2Fbufferutils/lists"}