1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* sane - Scanner Access Now Easy.
Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define DEBUG_DECLARE_ONLY
#include "device.h"
#include "command_set.h"
#include "low.h"
#include "utilities.h"
namespace genesys {
std::vector<unsigned> MethodResolutions::get_resolutions() const
{
std::vector<unsigned> ret;
std::copy(resolutions_x.begin(), resolutions_x.end(), std::back_inserter(ret));
std::copy(resolutions_y.begin(), resolutions_y.end(), std::back_inserter(ret));
// sort in decreasing order
std::sort(ret.begin(), ret.end(), std::greater<unsigned>());
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
return ret;
}
const MethodResolutions* Genesys_Model::get_resolution_settings_ptr(ScanMethod method) const
{
for (const auto& res_for_method : resolutions) {
for (auto res_method : res_for_method.methods) {
if (res_method == method) {
return &res_for_method;
}
}
}
return nullptr;
}
const MethodResolutions& Genesys_Model::get_resolution_settings(ScanMethod method) const
{
const auto* ptr = get_resolution_settings_ptr(method);
if (ptr)
return *ptr;
throw SaneException("Could not find resolution settings for method %d",
static_cast<unsigned>(method));
}
std::vector<unsigned> Genesys_Model::get_resolutions(ScanMethod method) const
{
return get_resolution_settings(method).get_resolutions();
}
bool Genesys_Model::has_method(ScanMethod method) const
{
return get_resolution_settings_ptr(method) != nullptr;
}
Genesys_Device::~Genesys_Device()
{
clear();
}
void Genesys_Device::clear()
{
calib_file.clear();
calibration_cache.clear();
white_average_data.clear();
dark_average_data.clear();
}
ImagePipelineNodeBufferedCallableSource& Genesys_Device::get_pipeline_source()
{
return static_cast<ImagePipelineNodeBufferedCallableSource&>(pipeline.front());
}
bool Genesys_Device::is_head_pos_known(ScanHeadId scan_head) const
{
switch (scan_head) {
case ScanHeadId::PRIMARY: return is_head_pos_primary_known_;
case ScanHeadId::SECONDARY: return is_head_pos_secondary_known_;
case ScanHeadId::ALL: return is_head_pos_primary_known_ && is_head_pos_secondary_known_;
default:
throw SaneException("Unknown scan head ID");
}
}
unsigned Genesys_Device::head_pos(ScanHeadId scan_head) const
{
switch (scan_head) {
case ScanHeadId::PRIMARY: return head_pos_primary_;
case ScanHeadId::SECONDARY: return head_pos_secondary_;
default:
throw SaneException("Unknown scan head ID");
}
}
void Genesys_Device::set_head_pos_unknown(ScanHeadId scan_head)
{
if ((scan_head & ScanHeadId::PRIMARY) != ScanHeadId::NONE) {
is_head_pos_primary_known_ = false;
}
if ((scan_head & ScanHeadId::SECONDARY) != ScanHeadId::NONE) {
is_head_pos_secondary_known_ = false;
}
}
void Genesys_Device::set_head_pos_zero(ScanHeadId scan_head)
{
if ((scan_head & ScanHeadId::PRIMARY) != ScanHeadId::NONE) {
head_pos_primary_ = 0;
is_head_pos_primary_known_ = true;
}
if ((scan_head & ScanHeadId::SECONDARY) != ScanHeadId::NONE) {
head_pos_secondary_ = 0;
is_head_pos_secondary_known_ = true;
}
}
void Genesys_Device::advance_head_pos_by_session(ScanHeadId scan_head)
{
int motor_steps = session.params.starty +
(session.params.lines * motor.base_ydpi) / session.params.yres;
auto direction = has_flag(session.params.flags, ScanFlag::REVERSE) ? Direction::BACKWARD
: Direction::FORWARD;
advance_head_pos_by_steps(scan_head, direction, motor_steps);
}
static void advance_pos(unsigned& pos, Direction direction, unsigned offset)
{
if (direction == Direction::FORWARD) {
pos += offset;
} else {
if (pos < offset) {
throw SaneException("Trying to advance head behind the home sensor");
}
pos -= offset;
}
}
void Genesys_Device::advance_head_pos_by_steps(ScanHeadId scan_head, Direction direction,
unsigned steps)
{
if ((scan_head & ScanHeadId::PRIMARY) != ScanHeadId::NONE) {
if (!is_head_pos_primary_known_) {
throw SaneException("Trying to advance head while scanhead position is not known");
}
advance_pos(head_pos_primary_, direction, steps);
}
if ((scan_head & ScanHeadId::SECONDARY) != ScanHeadId::NONE) {
if (!is_head_pos_secondary_known_) {
throw SaneException("Trying to advance head while scanhead position is not known");
}
advance_pos(head_pos_secondary_, direction, steps);
}
}
void print_scan_position(std::ostream& out, const Genesys_Device& dev, ScanHeadId scan_head)
{
if (dev.is_head_pos_known(scan_head)) {
out << dev.head_pos(scan_head);
} else {
out <<"(unknown)";
}
}
std::ostream& operator<<(std::ostream& out, const Genesys_Device& dev)
{
StreamStateSaver state_saver{out};
out << "Genesys_Device{\n"
<< std::hex
<< " vendorId: 0x" << dev.vendorId << '\n'
<< " productId: 0x" << dev.productId << '\n'
<< std::dec
<< " usb_mode: " << dev.usb_mode << '\n'
<< " file_name: " << dev.file_name << '\n'
<< " calib_file: " << dev.calib_file << '\n'
<< " force_calibration: " << dev.force_calibration << '\n'
<< " ignore_offsets: " << dev.ignore_offsets << '\n'
<< " model: (not printed)\n"
<< " reg: " << format_indent_braced_list(4, dev.reg) << '\n'
<< " initial_regs: " << format_indent_braced_list(4, dev.initial_regs) << '\n'
<< " settings: " << format_indent_braced_list(4, dev.settings) << '\n'
<< " frontend: " << format_indent_braced_list(4, dev.frontend) << '\n'
<< " frontend_initial: " << format_indent_braced_list(4, dev.frontend_initial) << '\n';
if (!dev.memory_layout.regs.empty()) {
out << " memory_layout.regs: "
<< format_indent_braced_list(4, dev.memory_layout.regs) << '\n';
}
out << " gpo.regs: " << format_indent_braced_list(4, dev.gpo.regs) << '\n'
<< " motor: " << format_indent_braced_list(4, dev.motor) << '\n'
<< " control[0..6]: " << std::hex
<< static_cast<unsigned>(dev.control[0]) << ' '
<< static_cast<unsigned>(dev.control[1]) << ' '
<< static_cast<unsigned>(dev.control[2]) << ' '
<< static_cast<unsigned>(dev.control[3]) << ' '
<< static_cast<unsigned>(dev.control[4]) << ' '
<< static_cast<unsigned>(dev.control[5]) << '\n' << std::dec
<< " average_size: " << dev.average_size << '\n'
<< " calib_session: " << format_indent_braced_list(4, dev.calib_session) << '\n'
<< " gamma_override_tables[0].size(): " << dev.gamma_override_tables[0].size() << '\n'
<< " gamma_override_tables[1].size(): " << dev.gamma_override_tables[1].size() << '\n'
<< " gamma_override_tables[2].size(): " << dev.gamma_override_tables[2].size() << '\n'
<< " white_average_data.size(): " << dev.white_average_data.size() << '\n'
<< " dark_average_data.size(): " << dev.dark_average_data.size() << '\n'
<< " already_initialized: " << dev.already_initialized << '\n'
<< " scanhead_position[PRIMARY]: ";
print_scan_position(out, dev, ScanHeadId::PRIMARY);
out << '\n'
<< " scanhead_position[SECONDARY]: ";
print_scan_position(out, dev, ScanHeadId::SECONDARY);
out << '\n'
<< " read_active: " << dev.read_active << '\n'
<< " parking: " << dev.parking << '\n'
<< " document: " << dev.document << '\n'
<< " total_bytes_read: " << dev.total_bytes_read << '\n'
<< " total_bytes_to_read: " << dev.total_bytes_to_read << '\n'
<< " session: " << format_indent_braced_list(4, dev.session) << '\n'
<< " calibration_cache: (not printed)\n"
<< " line_count: " << dev.line_count << '\n'
<< " segment_order: "
<< format_indent_braced_list(4, format_vector_unsigned(4, dev.segment_order)) << '\n'
<< '}';
return out;
}
void apply_reg_settings_to_device_write_only(Genesys_Device& dev,
const GenesysRegisterSettingSet& regs)
{
GenesysRegisterSettingSet backup;
for (const auto& reg : regs) {
dev.interface->write_register(reg.address, reg.value);
}
}
void apply_reg_settings_to_device(Genesys_Device& dev, const GenesysRegisterSettingSet& regs)
{
apply_reg_settings_to_device_with_backup(dev, regs);
}
GenesysRegisterSettingSet
apply_reg_settings_to_device_with_backup(Genesys_Device& dev,
const GenesysRegisterSettingSet& regs)
{
GenesysRegisterSettingSet backup;
for (const auto& reg : regs) {
std::uint8_t old_val = dev.interface->read_register(reg.address);
std::uint8_t new_val = (old_val & ~reg.mask) | (reg.value & reg.mask);
dev.interface->write_register(reg.address, new_val);
using SettingType = GenesysRegisterSettingSet::SettingType;
backup.push_back(SettingType{reg.address,
static_cast<std::uint8_t>(old_val & reg.mask),
reg.mask});
}
return backup;
}
} // namespace genesys
|