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