Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 201 additions & 8 deletions camerad/archon_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,17 @@ namespace Camera {
else
if (std::strncmp(suffix, "HEIGHT", 6)==0) this->frameinfo.bufheight[bufnum] = std::atoi(value_start);
break;
case 8: // BUFnCOMPLETE
case 8: // BUFnCOMPLETE, RAWLINES
if (std::strncmp(suffix, "COMPLETE", 8)==0) this->frameinfo.bufcomplete[bufnum] = std::atoi(value_start);
else
if (std::strncmp(suffix, "RAWLINES", 8)==0) this->frameinfo.bufrawlines[bufnum] = std::atoi(value_start);
break;
case 9: // BUFnTIMESTAMP
case 9: // BUFnTIMESTAMP, RAWOFFSET, RAWBLOCKS
if (std::strncmp(suffix, "TIMESTAMP", 9)==0) this->frameinfo.buftimestamp[bufnum] = std::strtoull(value_start, nullptr, 16);
else
if (std::strncmp(suffix, "RAWOFFSET", 9)==0) this->frameinfo.bufrawoffset[bufnum] = std::strtoul(value_start, nullptr, 10);
else
if (std::strncmp(suffix, "RAWBLOCKS", 9)==0) this->frameinfo.bufrawblocks[bufnum] = std::atoi(value_start);
break;
case 11: // BUFnRETIMESTAMP, FETIMESTAMP
if (std::strncmp(suffix, "RETIMESTAMP", 11)==0) this->frameinfo.bufretimestamp[bufnum] = std::strtoull(value_start, nullptr, 16);
Expand Down Expand Up @@ -1608,8 +1614,10 @@ namespace Camera {
this->get_configmap_value("FRAMEMODE", mode->geometry.framemode);
this->get_configmap_value("RAWENABLE", mode->rawenable);
this->get_configmap_value("RAWSEL", this->rawinfo.adchan);
this->get_configmap_value("RAWSAMPLES", this->rawinfo.rawsamples);
this->get_configmap_value("RAWENDLINE", this->rawinfo.rawlines);
this->get_configmap_value("RAWSAMPLES", this->rawinfo.samples);
this->get_configmap_value("RAWSTARTLINE", this->rawinfo.startline);
this->get_configmap_value("RAWENDLINE", this->rawinfo.endline);
this->get_configmap_value("RAWSTARTPIXEL", this->rawinfo.startpixel);

// Read geometry from the mode's configmap (not the global one) since each
// mode section can override LINECOUNT/PIXELCOUNT
Expand Down Expand Up @@ -1911,11 +1919,10 @@ namespace Camera {
//
switch (this->frametype) {
case Camera::ArchonController::FRAME_RAW:
// Archon buffer base address
// RAW data lives at bufbase + rawoffset; block count derives from the
// RAW geometry (RAWSAMPLES x rawlines x 2 bytes), never image_memory
bufaddr = this->frameinfo.bufbase[index] + this->frameinfo.bufrawoffset[index];

// Calculate the number of blocks expected. image_memory is bytes per detector
bufblocks = (unsigned int) floor( (this->interface->camera_info.image_memory + BLOCK_LEN - 1 ) / BLOCK_LEN );
bufblocks = (this->raw_frame_bytes() + BLOCK_LEN - 1) / BLOCK_LEN;
break;

case Camera::ArchonController::FRAME_IMAGE:
Expand Down Expand Up @@ -2034,6 +2041,192 @@ namespace Camera {
}


/***** Camera::ArchonController::is_raw_config_key *************************/
/**
* @brief return true if key is one of the settable RAW config keywords
*/
bool ArchonController::is_raw_config_key(const std::string &key) {
for (const auto* raw_key : {"RAWENABLE", "RAWSEL", "RAWSTARTLINE",
"RAWENDLINE", "RAWSTARTPIXEL", "RAWSAMPLES"}) {
if (key == raw_key) return true;
}
return false;
}
/***** Camera::ArchonController::is_raw_config_key *************************/


/***** Camera::ArchonController::raw_geometry ****************************/
/**
* @brief resolve RAW capture geometry for the newest buffer
* @details Prefers the controller-reported BUFnRAWBLOCKS/BUFnRAWLINES,
* which already account for the per-line rounding to whole
* 1024-byte blocks. Falls back to the config keys when the
* controller has not reported them (e.g. emulator). The line
* range is inclusive: RAWSTARTLINE=0,RAWENDLINE=1 is two lines.
*/
ArchonController::raw_geometry_t ArchonController::raw_geometry() const {
const auto index = this->frameinfo.index.load();
raw_geometry_t geom;
geom.samples = static_cast<uint32_t>(this->rawinfo.samples);
geom.blocks_per_line = static_cast<uint32_t>(this->frameinfo.bufrawblocks[index]);
geom.lines = static_cast<uint32_t>(this->frameinfo.bufrawlines[index]);

if (geom.blocks_per_line == 0 || geom.lines == 0) {
geom.blocks_per_line =
(static_cast<size_t>(geom.samples) * sizeof(uint16_t) + BLOCK_LEN - 1) / BLOCK_LEN;
const int span = this->rawinfo.endline - this->rawinfo.startline + 1;
geom.lines = span > 0 ? static_cast<uint32_t>(span) : 0u;
}
return geom;
}
/***** Camera::ArchonController::raw_geometry ****************************/


/***** Camera::ArchonController::raw_frame_bytes **************************/
/**
* @brief padded, size-aware byte count for one RAW fetch
* @details Each line occupies whole 1024-byte blocks, so the fetch reads
* blocks_per_line x lines blocks. RAW samples are always 16-bit;
* RAWSTARTPIXEL only sets where sampling begins in a line and so
* does not change the count.
*/
uint32_t ArchonController::raw_frame_bytes() const {
const raw_geometry_t geom = this->raw_geometry();
return geom.blocks_per_line * geom.lines * BLOCK_LEN;
}
/***** Camera::ArchonController::raw_frame_bytes **************************/


/***** Camera::ArchonController::get_raw_config **************************/
/**
* @brief report the current RAW configuration keywords
* @param[out] retstring space-delimited KEY=VALUE pairs
*/
long ArchonController::get_raw_config(std::string &retstring) {
std::ostringstream oss;
for (const auto* key : {"RAWENABLE", "RAWSEL", "RAWSTARTLINE",
"RAWENDLINE", "RAWSTARTPIXEL", "RAWSAMPLES"}) {
auto it = this->configmap.find(key);
oss << key << "=" << (it != this->configmap.end() ? it->second.value : "?") << " ";
}
retstring = oss.str();
return NO_ERROR;
}
/***** Camera::ArchonController::get_raw_config **************************/


/***** Camera::ArchonController::set_raw_config **************************/
/**
* @brief set one or more RAW config keywords then APPLYALL
* @param[in] args "KEY VALUE [KEY VALUE ...]"
* @param[out] retstring resulting RAW configuration
*/
long ArchonController::set_raw_config(const std::string &args, std::string &retstring) {
const std::string function("Camera::ArchonController::set_raw_config");

std::vector<std::string> tokens;
Tokenize(args, tokens, " ");
if (tokens.empty() || tokens.size() % 2 != 0) {
logwrite(function, "ERROR expected KEY VALUE pairs");
retstring = "expected KEY VALUE pairs";
return ERROR;
}

bool changed = false;
for (size_t i = 0; i < tokens.size(); i += 2) {
std::string key = tokens[i];
std::transform(key.begin(), key.end(), key.begin(), ::toupper);
if (!is_raw_config_key(key)) {
logwrite(function, "ERROR unknown RAW key: "+key);
retstring = "unknown RAW key: "+key;
return ERROR;
}
if (this->write_config_key(key.c_str(), tokens[i+1].c_str(), changed) != NO_ERROR) {
retstring = "failed writing "+key;
return ERROR;
}
}

if (changed && this->send_cmd(APPLYCDS) != NO_ERROR) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question about the use of the changed bool -
So let's say one RAW setting changed, but another one doesn't. Regardless do we apply CDS if one of the RAW values change?

If so would the following case fail: first key RAWSEL changed would be true, then the second key RAWSAMPLES changed would be false so APPLYCDS would be skipped?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every change of any RAW related keyword or TAPLINE related keyword or CDS related keyword requires an APPLYCDS command before it takes effect. There is no way to know strictly what the current status is of these keywords because one can update the config but not do an APPLYCDS. This is the reason (though it's annoying) why in foxtrot you can't individually set any of those, you have to provide a struct containing all CDS related keywords, so there is never this inconsistency.

Therefore, in my opinion, it's very hard to decide on any situation when you should NOT do APPLYCDS. Because it might be that the values in your config are simply not reflected in the real world anyway, there's no way to tell from just querying the archon.

logwrite(function, "ERROR applying RAW configuration");
retstring = "failed to apply";
return ERROR;
}

// refresh cached geometry from the now-updated configmap
this->get_configmap_value("RAWSEL", this->rawinfo.adchan);
this->get_configmap_value("RAWSAMPLES", this->rawinfo.samples);
this->get_configmap_value("RAWSTARTLINE", this->rawinfo.startline);
this->get_configmap_value("RAWENDLINE", this->rawinfo.endline);
this->get_configmap_value("RAWSTARTPIXEL", this->rawinfo.startpixel);

return this->get_raw_config(retstring);
}
/***** Camera::ArchonController::set_raw_config **************************/


/***** Camera::ArchonController::read_raw *******************************/
/**
* @brief retrieve RAW (pre-CDS) data from the newest frame buffer
* @details Reads the size-aware RAW region as 16-bit unsigned samples and
* dispatches it in-band as a (RAWSAMPLES x rawlines) frame,
* independent of the post-CDS pixel mode.
* @param[out] retstring "samples=<w> lines=<h> bytes=<n>"
*/
long ArchonController::read_raw(std::string &retstring) {
const std::string function("Camera::ArchonController::read_raw");

this->get_frame_status();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to handle the case of get_frame_status returns with an error before computing the raw geometry


const raw_geometry_t geom = this->raw_geometry();
if (geom.samples == 0 || geom.lines == 0) {
logwrite(function, "ERROR RAW geometry is empty; check RAW config");
retstring = "invalid RAW geometry";
return ERROR;
}

const size_t fetch_bytes = static_cast<size_t>(geom.blocks_per_line) * geom.lines * BLOCK_LEN;
std::shared_ptr<char[]> raw_buffer(new char[fetch_bytes]);
char* bufptr = raw_buffer.get();

if (this->read_frame(FRAME_RAW, bufptr) != NO_ERROR) {
logwrite(function, "ERROR reading RAW frame");
retstring = "read failed";
return ERROR;
}

// The Archon pads each raw line out to whole 1024-byte blocks, so copy only
// the valid RAWSAMPLES from each line into a contiguous lines x samples array
const size_t line_stride = static_cast<size_t>(geom.blocks_per_line) * BLOCK_LEN;
const size_t payload_samples = static_cast<size_t>(geom.lines) * geom.samples;
std::vector<uint16_t> samples(payload_samples);
for (uint32_t line = 0; line < geom.lines; ++line) {
std::memcpy(samples.data() + static_cast<size_t>(line) * geom.samples,
raw_buffer.get() + line * line_stride,
static_cast<size_t>(geom.samples) * sizeof(uint16_t));
}

const auto index = this->frameinfo.index.load();
const size_t payload_bytes = payload_samples * sizeof(uint16_t);

Camera::FrameMetadata meta;
meta.frame_number = this->frameinfo.bufframen[index];
meta.timestamp = this->frameinfo.buftimestamp[index];
meta.width = geom.samples;
meta.height = geom.lines;
meta.bytes_per_pixel = sizeof(uint16_t);
this->interface->dispatch_frame(reinterpret_cast<const char*>(samples.data()),
payload_bytes, meta);

std::ostringstream oss;
oss << "samples=" << geom.samples << " lines=" << geom.lines << " bytes=" << payload_bytes;
retstring = oss.str();
logwrite(function, retstring);
return NO_ERROR;
}
/***** Camera::ArchonController::read_raw *******************************/


/***** Camera::ArchonController::wait_for_readout ***************************/
/**
* @brief creates a wait until the next completed frame buffer is ready
Expand Down
24 changes: 21 additions & 3 deletions camerad/archon_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,13 @@ namespace Camera {
cfg_map_t configmap;
param_map_t parammap;

/** @brief Archon RAW (pre-CDS) capture configuration, mirrors ACF keywords */
struct rawinfo_t {
int adchan;
uint16_t rawsamples;
uint16_t rawlines;
int adchan{0}; // RAWSEL: AD channel captured
uint16_t samples{0}; // RAWSAMPLES: 16-bit samples per line
uint16_t startline{0}; // RAWSTARTLINE
uint16_t endline{0}; // RAWENDLINE
uint16_t startpixel{0}; // RAWSTARTPIXEL
} rawinfo;

/**
Expand Down Expand Up @@ -345,6 +348,21 @@ namespace Camera {
long write_config_key(const char* key, const char* newvalue, bool &changed);
long write_config_key(const char* key, int newvalue, bool &changed);

// RAW (pre-CDS) capture: configuration and retrieval
/** @brief resolved RAW capture geometry for the newest buffer */
struct raw_geometry_t {
uint32_t samples; // valid 16-bit samples per line (RAWSAMPLES)
uint32_t blocks_per_line; // 1024-byte blocks per line, padded per Archon
uint32_t lines; // number of raw lines (RAWENDLINE-RAWSTARTLINE+1)
};

static bool is_raw_config_key(const std::string &key);
raw_geometry_t raw_geometry() const;
uint32_t raw_frame_bytes() const; // padded, size-aware byte count for a RAW fetch
long set_raw_config(const std::string &args, std::string &retstring);
long get_raw_config(std::string &retstring);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return a std::optionalstd::string here? RTVO is guaranteed to elide that copy since c++17 AFAICS if performance is an issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It matches the signature, and the long return encodes three states (NO_ERROR/ERROR/HELP), the optional can't do that

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough.... we do have std::expected for that case ;-).
Broader question then why is this a long and not an enum class?
Or if there are only 3 states why is it a long and not a short?#

Anyway who cares, not to worry

long read_raw(std::string &retstring);


std::map<std::string, modeinfo_t> modemap;

Expand Down
46 changes: 46 additions & 0 deletions camerad/archon_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace Camera {
return this->set_camera_mode(args, retstring);
}
else
if ( cmd == CAMERAD_RAW ) {
return this->raw(args, retstring);
}
else
if ( cmd == "autofetch_mode" ) {
return this->autofetch_mode(args, retstring);
}
Expand Down Expand Up @@ -944,6 +948,48 @@ namespace Camera {
/***** Camera::ArchonInterface::set_vcpu_inreg ******************************/


/***** Camera::ArchonInterface::raw ****************************************/
/**
* @brief configure and retrieve Archon RAW (pre-CDS) data
* @param[in] args "config" | "set <KEY> <VAL> [...]" | "read"
* @param[out] retstring RAW configuration or retrieval summary
* @return ERROR | NO_ERROR | HELP
*
*/
long ArchonInterface::raw( const std::string args, std::string &retstring ) {
const std::string function("Camera::ArchonInterface::raw");

if (args=="?" || args=="help") {
retstring = CAMERAD_RAW;
retstring.append( " [ config | set <KEY> <VAL> [...] | read ]\n" );
retstring.append( " config report the RAW config keywords\n" );
retstring.append( " set <KEY> <VAL> .. set RAW keyword(s) then apply\n" );
retstring.append( " read retrieve RAW data in-band as 16-bit samples\n" );
retstring.append( " Keys: RAWENABLE RAWSEL RAWSTARTLINE RAWENDLINE RAWSTARTPIXEL RAWSAMPLES\n" );
return HELP;
}

std::size_t sep = args.find_first_of(" ");
const std::string subcmd = args.substr(0, sep);
const std::string subargs = (sep==std::string::npos) ? "" : args.substr(sep+1);

if (subcmd.empty() || subcmd=="config") {
return this->controller->get_raw_config(retstring);
}
if (subcmd=="set") {
return this->controller->set_raw_config(subargs, retstring);
}
if (subcmd=="read") {
return this->controller->read_raw(retstring);
}

logwrite(function, "ERROR unrecognized subcommand: "+subcmd);
retstring = "unrecognized subcommand: "+subcmd;
return ERROR;
}
/***** Camera::ArchonInterface::raw ****************************************/


/***** Camera::ArchonInterface::native **************************************/
/**
* @brief send native commands directly to Archon and log result
Expand Down
1 change: 1 addition & 0 deletions camerad/archon_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace Camera {
long load_firmware( const std::string &args, std::string &retstring ) override;
long native( const std::string args, std::string &retstring ) override;
long power( const std::string args, std::string &retstring ) override;
long raw( const std::string args, std::string &retstring );
long test( const std::string args, std::string &retstring ) override;

long do_expose() override;
Expand Down
16 changes: 16 additions & 0 deletions camerad/camera_interface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "camera_interface.h"
#include "frame_output_factory.h"

namespace Camera {

Expand All @@ -8,6 +9,21 @@ namespace Camera {
this->server=s;
}

/***** Camera::Interface::configure_frame_outputs ***************************/
/**
* @brief build the in-band frame output sinks (FITS, shared memory)
* @details dispatch_frame fans every retrieved frame out to these
*
*/
void Interface::configure_frame_outputs() {
const std::string function("Camera::Interface::configure_frame_outputs");
FrameOutputsConfig outcfg;
apply_config_overrides(outcfg, this->configfile);
this->frame_outputs = make_frame_outputs(outcfg);
logwrite(function, "configured "+std::to_string(this->frame_outputs.size())+" frame output(s)");
}
/***** Camera::Interface::configure_frame_outputs ***************************/

void Interface::func_shared() {
std::string function("Camera::Interface::func_shared");
logwrite(function, "common implementation function");
Expand Down
1 change: 1 addition & 0 deletions camerad/camera_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace Camera {
//
void set_server(Camera::Server* s);
void func_shared();
void configure_frame_outputs();
void disconnect_controller();
bool is_exposuremode_set() { return ( this->exposuremode && !this->exposuremode->get_type().empty() ); }

Expand Down
4 changes: 4 additions & 0 deletions camerad/camera_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ namespace Camera {
ret = interface->controller_cmd(cmd, args, retstring);
}
else
if ( cmd == CAMERAD_RAW ) {
ret = interface->controller_cmd(cmd, args, retstring);
}
else
if ( cmd == "bob" ) {
ret = interface->controller_cmd(cmd, args, retstring);
}
Expand Down
Loading
Loading