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 "low.h"
45 #include "motor.h"
46 #include "utilities.h"
47 #include <cmath>
48 #include <numeric>
49 
50 namespace genesys {
51 
get_table_step_shifted(unsigned step,StepType step_type) const52 unsigned MotorSlope::get_table_step_shifted(unsigned step, StepType step_type) const
53 {
54     // first two steps are always equal to the initial speed
55     if (step < 2) {
56         return initial_speed_w >> static_cast<unsigned>(step_type);
57     }
58     step--;
59 
60     float initial_speed_v = 1.0f / initial_speed_w;
61     float speed_v = std::sqrt(initial_speed_v * initial_speed_v + 2 * acceleration * step);
62     return static_cast<unsigned>(1.0f / speed_v) >> static_cast<unsigned>(step_type);
63 }
64 
compute_acceleration_for_steps(unsigned initial_w,unsigned max_w,unsigned steps)65 float compute_acceleration_for_steps(unsigned initial_w, unsigned max_w, unsigned steps)
66 {
67     float initial_speed_v = 1.0f / static_cast<float>(initial_w);
68     float max_speed_v = 1.0f / static_cast<float>(max_w);
69     return (max_speed_v * max_speed_v - initial_speed_v * initial_speed_v) / (2 * steps);
70 }
71 
72 
create_from_steps(unsigned initial_w,unsigned max_w,unsigned steps)73 MotorSlope MotorSlope::create_from_steps(unsigned initial_w, unsigned max_w,
74                                          unsigned steps)
75 {
76     MotorSlope slope;
77     slope.initial_speed_w = initial_w;
78     slope.max_speed_w = max_w;
79     slope.acceleration = compute_acceleration_for_steps(initial_w, max_w, steps);
80     return slope;
81 }
82 
slice_steps(unsigned count,unsigned step_multiplier)83 void MotorSlopeTable::slice_steps(unsigned count, unsigned step_multiplier)
84 {
85     if (count > table.size() || count < step_multiplier) {
86         throw SaneException("Invalid steps count");
87     }
88     count = align_multiple_floor(count, step_multiplier);
89     table.resize(count);
90     generate_pixeltime_sum();
91 }
92 
expand_table(unsigned count,unsigned step_multiplier)93 void MotorSlopeTable::expand_table(unsigned count, unsigned step_multiplier)
94 {
95     if (table.empty()) {
96         throw SaneException("Can't expand empty table");
97     }
98     count = align_multiple_ceil(count, step_multiplier);
99     table.resize(table.size() + count, table.back());
100     generate_pixeltime_sum();
101 }
102 
generate_pixeltime_sum()103 void MotorSlopeTable::generate_pixeltime_sum()
104 {
105     pixeltime_sum_ = std::accumulate(table.begin(), table.end(),
106                                      std::size_t{0}, std::plus<std::size_t>());
107 }
108 
get_slope_table_max_size(AsicType asic_type)109 unsigned get_slope_table_max_size(AsicType asic_type)
110 {
111     switch (asic_type) {
112         case AsicType::GL646:
113         case AsicType::GL841:
114         case AsicType::GL842: return 255;
115         case AsicType::GL843:
116         case AsicType::GL845:
117         case AsicType::GL846:
118         case AsicType::GL847:
119         case AsicType::GL124: return 1024;
120         default:
121             throw SaneException("Unknown asic type");
122     }
123 }
124 
create_slope_table_for_speed(const MotorSlope & slope,unsigned target_speed_w,StepType step_type,unsigned steps_alignment,unsigned min_size,unsigned max_size)125 MotorSlopeTable create_slope_table_for_speed(const MotorSlope& slope, unsigned target_speed_w,
126                                              StepType step_type, unsigned steps_alignment,
127                                              unsigned min_size, unsigned max_size)
128 {
129     DBG_HELPER_ARGS(dbg, "target_speed_w: %d, step_type: %d, steps_alignment: %d, min_size: %d",
130                     target_speed_w, static_cast<unsigned>(step_type), steps_alignment, min_size);
131     MotorSlopeTable table;
132 
133     unsigned step_shift = static_cast<unsigned>(step_type);
134 
135     unsigned target_speed_shifted_w = target_speed_w >> step_shift;
136     unsigned max_speed_shifted_w = slope.max_speed_w >> step_shift;
137 
138     if (target_speed_shifted_w < max_speed_shifted_w) {
139         dbg.log(DBG_warn, "failed to reach target speed");
140     }
141 
142     if (target_speed_shifted_w >= std::numeric_limits<std::uint16_t>::max()) {
143         throw SaneException("Target motor speed is too low");
144     }
145 
146     unsigned final_speed = std::max(target_speed_shifted_w, max_speed_shifted_w);
147 
148     table.table.reserve(max_size);
149 
150     while (table.table.size() < max_size - 1) {
151         unsigned current = slope.get_table_step_shifted(table.table.size(), step_type);
152         if (current <= final_speed) {
153             break;
154         }
155         table.table.push_back(current);
156     }
157 
158     // make sure the target speed (or the max speed if target speed is too high) is present in
159     // the table
160     table.table.push_back(final_speed);
161 
162     // fill the table up to the specified size
163     while (table.table.size() < max_size - 1 &&
164            (table.table.size() % steps_alignment != 0 || table.table.size() < min_size))
165     {
166         table.table.push_back(table.table.back());
167     }
168 
169     table.generate_pixeltime_sum();
170 
171     return table;
172 }
173 
operator <<(std::ostream & out,const MotorSlope & slope)174 std::ostream& operator<<(std::ostream& out, const MotorSlope& slope)
175 {
176     out << "MotorSlope{\n"
177         << "    initial_speed_w: " << slope.initial_speed_w << '\n'
178         << "    max_speed_w: " << slope.max_speed_w << '\n'
179         << "    a: " << slope.acceleration << '\n'
180         << '}';
181     return out;
182 }
183 
operator <<(std::ostream & out,const MotorProfile & profile)184 std::ostream& operator<<(std::ostream& out, const MotorProfile& profile)
185 {
186     out << "MotorProfile{\n"
187         << "    max_exposure: " << profile.max_exposure << '\n'
188         << "    step_type: " << profile.step_type << '\n'
189         << "    motor_vref: " << profile.motor_vref << '\n'
190         << "    resolutions: " << format_indent_braced_list(4, profile.resolutions) << '\n'
191         << "    scan_methods: " << format_indent_braced_list(4, profile.scan_methods) << '\n'
192         << "    slope: " << format_indent_braced_list(4, profile.slope) << '\n'
193         << '}';
194     return out;
195 }
196 
operator <<(std::ostream & out,const Genesys_Motor & motor)197 std::ostream& operator<<(std::ostream& out, const Genesys_Motor& motor)
198 {
199     out << "Genesys_Motor{\n"
200         << "    id: " << motor.id << '\n'
201         << "    base_ydpi: " << motor.base_ydpi << '\n'
202         << "    profiles: "
203         << format_indent_braced_list(4, format_vector_indent_braced(4, "MotorProfile",
204                                                                     motor.profiles)) << '\n'
205         << "    fast_profiles: "
206         << format_indent_braced_list(4, format_vector_indent_braced(4, "MotorProfile",
207                                                                     motor.fast_profiles)) << '\n'
208         << '}';
209     return out;
210 }
211 
212 } // namespace genesys
213