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