Skip to content

Commit

Permalink
Add readBa method
Browse files Browse the repository at this point in the history
  • Loading branch information
bengineerd committed Oct 15, 2024
1 parent eaedb41 commit 3832713
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/rogue/interfaces/stream/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class Frame : public rogue::EnableSharedFromThis<rogue::interfaces::stream::Fram
rogue::interfaces::stream::FrameIterator endWrite();

#ifndef NO_PYTHON


//! Python Frame data read function
/** Read data from Frame into passed Python byte array.
Expand All @@ -337,6 +338,15 @@ class Frame : public rogue::EnableSharedFromThis<rogue::interfaces::stream::Fram
*/
void readPy(boost::python::object p, uint32_t offset);

//! Python Frame data read function
/** Read data from Frame into a python bytearray which is allocated and returned
*
* Exposed as readBa() to Python
* @param offset First location of Frame data to copy to byte array
*/
boost::python::object readBytearrayPy(uint32_t offset);


//! Python Frame data write function
/** Write data into from Frame from passed Python byte array.
*
Expand Down
19 changes: 18 additions & 1 deletion src/rogue/interfaces/stream/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ ris::FrameIterator ris::Frame::endWrite() {

#ifndef NO_PYTHON

//! Read up to count bytes from frame, starting from offset. Python version.

//! Read bytes from frame into a passed bytearray, starting from offset. Python version.
void ris::Frame::readPy(boost::python::object p, uint32_t offset) {
Py_buffer pyBuf;

Expand All @@ -369,6 +370,20 @@ void ris::Frame::readPy(boost::python::object p, uint32_t offset) {
PyBuffer_Release(&pyBuf);
}

//! Allocate a bytearray and read bytes from frame into it, starting at offset
bp::object ris::Frame::readBytearrayPy(uint32_t offset) {
// Get the size of the frame
uint32_t size = getPayload();

// Create a Python bytearray to hold the data
boost::python::object byteArray = boost::python::eval("bytearray")(size - offset);

this->readPy(byteArray, offset);

return byteArray;
}


//! Write python buffer to frame, starting at offset. Python Version
void ris::Frame::writePy(boost::python::object p, uint32_t offset) {
Py_buffer pyBuf;
Expand Down Expand Up @@ -514,6 +529,8 @@ void ris::Frame::setup_python() {
.def("getPayload", &ris::Frame::getPayload)
.def("read", &ris::Frame::readPy, (
bp::arg("offset")=0))
.def("readBa", &ris::Frame::readBytearrayPy, (
bp::arg("offset")=0))
.def("write", &ris::Frame::writePy, (
bp::arg("offset")=0))
.def("setError", &ris::Frame::setError)
Expand Down

0 comments on commit 3832713

Please sign in to comment.