1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4141cc406Sopenharmony_ci
5141cc406Sopenharmony_ci   This file is part of the SANE package.
6141cc406Sopenharmony_ci
7141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
8141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
9141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
10141cc406Sopenharmony_ci   License, or (at your option) any later version.
11141cc406Sopenharmony_ci
12141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
13141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
14141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15141cc406Sopenharmony_ci   General Public License for more details.
16141cc406Sopenharmony_ci
17141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
18141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
19141cc406Sopenharmony_ci*/
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci#define DEBUG_DECLARE_ONLY
22141cc406Sopenharmony_ci
23141cc406Sopenharmony_ci#include "low.h"
24141cc406Sopenharmony_ci#include "motor.h"
25141cc406Sopenharmony_ci#include "utilities.h"
26141cc406Sopenharmony_ci#include <cmath>
27141cc406Sopenharmony_ci#include <numeric>
28141cc406Sopenharmony_ci
29141cc406Sopenharmony_cinamespace genesys {
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ciunsigned MotorSlope::get_table_step_shifted(unsigned step, StepType step_type) const
32141cc406Sopenharmony_ci{
33141cc406Sopenharmony_ci    // first two steps are always equal to the initial speed
34141cc406Sopenharmony_ci    if (step < 2) {
35141cc406Sopenharmony_ci        return initial_speed_w >> static_cast<unsigned>(step_type);
36141cc406Sopenharmony_ci    }
37141cc406Sopenharmony_ci    step--;
38141cc406Sopenharmony_ci
39141cc406Sopenharmony_ci    float initial_speed_v = 1.0f / initial_speed_w;
40141cc406Sopenharmony_ci    float speed_v = std::sqrt(initial_speed_v * initial_speed_v + 2 * acceleration * step);
41141cc406Sopenharmony_ci    return static_cast<unsigned>(1.0f / speed_v) >> static_cast<unsigned>(step_type);
42141cc406Sopenharmony_ci}
43141cc406Sopenharmony_ci
44141cc406Sopenharmony_cifloat compute_acceleration_for_steps(unsigned initial_w, unsigned max_w, unsigned steps)
45141cc406Sopenharmony_ci{
46141cc406Sopenharmony_ci    float initial_speed_v = 1.0f / static_cast<float>(initial_w);
47141cc406Sopenharmony_ci    float max_speed_v = 1.0f / static_cast<float>(max_w);
48141cc406Sopenharmony_ci    return (max_speed_v * max_speed_v - initial_speed_v * initial_speed_v) / (2 * steps);
49141cc406Sopenharmony_ci}
50141cc406Sopenharmony_ci
51141cc406Sopenharmony_ci
52141cc406Sopenharmony_ciMotorSlope MotorSlope::create_from_steps(unsigned initial_w, unsigned max_w,
53141cc406Sopenharmony_ci                                         unsigned steps)
54141cc406Sopenharmony_ci{
55141cc406Sopenharmony_ci    MotorSlope slope;
56141cc406Sopenharmony_ci    slope.initial_speed_w = initial_w;
57141cc406Sopenharmony_ci    slope.max_speed_w = max_w;
58141cc406Sopenharmony_ci    slope.acceleration = compute_acceleration_for_steps(initial_w, max_w, steps);
59141cc406Sopenharmony_ci    return slope;
60141cc406Sopenharmony_ci}
61141cc406Sopenharmony_ci
62141cc406Sopenharmony_civoid MotorSlopeTable::slice_steps(unsigned count, unsigned step_multiplier)
63141cc406Sopenharmony_ci{
64141cc406Sopenharmony_ci    if (count > table.size() || count < step_multiplier) {
65141cc406Sopenharmony_ci        throw SaneException("Invalid steps count");
66141cc406Sopenharmony_ci    }
67141cc406Sopenharmony_ci    count = align_multiple_floor(count, step_multiplier);
68141cc406Sopenharmony_ci    table.resize(count);
69141cc406Sopenharmony_ci    generate_pixeltime_sum();
70141cc406Sopenharmony_ci}
71141cc406Sopenharmony_ci
72141cc406Sopenharmony_civoid MotorSlopeTable::expand_table(unsigned count, unsigned step_multiplier)
73141cc406Sopenharmony_ci{
74141cc406Sopenharmony_ci    if (table.empty()) {
75141cc406Sopenharmony_ci        throw SaneException("Can't expand empty table");
76141cc406Sopenharmony_ci    }
77141cc406Sopenharmony_ci    count = align_multiple_ceil(count, step_multiplier);
78141cc406Sopenharmony_ci    table.resize(table.size() + count, table.back());
79141cc406Sopenharmony_ci    generate_pixeltime_sum();
80141cc406Sopenharmony_ci}
81141cc406Sopenharmony_ci
82141cc406Sopenharmony_civoid MotorSlopeTable::generate_pixeltime_sum()
83141cc406Sopenharmony_ci{
84141cc406Sopenharmony_ci    pixeltime_sum_ = std::accumulate(table.begin(), table.end(),
85141cc406Sopenharmony_ci                                     std::size_t{0}, std::plus<std::size_t>());
86141cc406Sopenharmony_ci}
87141cc406Sopenharmony_ci
88141cc406Sopenharmony_ciunsigned get_slope_table_max_size(AsicType asic_type)
89141cc406Sopenharmony_ci{
90141cc406Sopenharmony_ci    switch (asic_type) {
91141cc406Sopenharmony_ci        case AsicType::GL646:
92141cc406Sopenharmony_ci        case AsicType::GL841:
93141cc406Sopenharmony_ci        case AsicType::GL842: return 255;
94141cc406Sopenharmony_ci        case AsicType::GL843:
95141cc406Sopenharmony_ci        case AsicType::GL845:
96141cc406Sopenharmony_ci        case AsicType::GL846:
97141cc406Sopenharmony_ci        case AsicType::GL847:
98141cc406Sopenharmony_ci        case AsicType::GL124: return 1024;
99141cc406Sopenharmony_ci        default:
100141cc406Sopenharmony_ci            throw SaneException("Unknown asic type");
101141cc406Sopenharmony_ci    }
102141cc406Sopenharmony_ci}
103141cc406Sopenharmony_ci
104141cc406Sopenharmony_ciMotorSlopeTable create_slope_table_for_speed(const MotorSlope& slope, unsigned target_speed_w,
105141cc406Sopenharmony_ci                                             StepType step_type, unsigned steps_alignment,
106141cc406Sopenharmony_ci                                             unsigned min_size, unsigned max_size)
107141cc406Sopenharmony_ci{
108141cc406Sopenharmony_ci    DBG_HELPER_ARGS(dbg, "target_speed_w: %d, step_type: %d, steps_alignment: %d, min_size: %d",
109141cc406Sopenharmony_ci                    target_speed_w, static_cast<unsigned>(step_type), steps_alignment, min_size);
110141cc406Sopenharmony_ci    MotorSlopeTable table;
111141cc406Sopenharmony_ci
112141cc406Sopenharmony_ci    unsigned step_shift = static_cast<unsigned>(step_type);
113141cc406Sopenharmony_ci
114141cc406Sopenharmony_ci    unsigned target_speed_shifted_w = target_speed_w >> step_shift;
115141cc406Sopenharmony_ci    unsigned max_speed_shifted_w = slope.max_speed_w >> step_shift;
116141cc406Sopenharmony_ci
117141cc406Sopenharmony_ci    if (target_speed_shifted_w < max_speed_shifted_w) {
118141cc406Sopenharmony_ci        dbg.vlog(DBG_warn, "failed to reach target speed %d %d", target_speed_w,
119141cc406Sopenharmony_ci                  slope.max_speed_w);
120141cc406Sopenharmony_ci    }
121141cc406Sopenharmony_ci
122141cc406Sopenharmony_ci    if (target_speed_shifted_w >= std::numeric_limits<std::uint16_t>::max()) {
123141cc406Sopenharmony_ci        throw SaneException("Target motor speed is too low");
124141cc406Sopenharmony_ci    }
125141cc406Sopenharmony_ci
126141cc406Sopenharmony_ci    unsigned final_speed = std::max(target_speed_shifted_w, max_speed_shifted_w);
127141cc406Sopenharmony_ci
128141cc406Sopenharmony_ci    table.table.reserve(max_size);
129141cc406Sopenharmony_ci
130141cc406Sopenharmony_ci    while (table.table.size() < max_size - 1) {
131141cc406Sopenharmony_ci        unsigned current = slope.get_table_step_shifted(table.table.size(), step_type);
132141cc406Sopenharmony_ci        if (current <= final_speed) {
133141cc406Sopenharmony_ci            break;
134141cc406Sopenharmony_ci        }
135141cc406Sopenharmony_ci        table.table.push_back(current);
136141cc406Sopenharmony_ci    }
137141cc406Sopenharmony_ci
138141cc406Sopenharmony_ci    // make sure the target speed (or the max speed if target speed is too high) is present in
139141cc406Sopenharmony_ci    // the table
140141cc406Sopenharmony_ci    table.table.push_back(final_speed);
141141cc406Sopenharmony_ci
142141cc406Sopenharmony_ci    // fill the table up to the specified size
143141cc406Sopenharmony_ci    while (table.table.size() < max_size - 1 &&
144141cc406Sopenharmony_ci           (table.table.size() % steps_alignment != 0 || table.table.size() < min_size))
145141cc406Sopenharmony_ci    {
146141cc406Sopenharmony_ci        table.table.push_back(table.table.back());
147141cc406Sopenharmony_ci    }
148141cc406Sopenharmony_ci
149141cc406Sopenharmony_ci    table.generate_pixeltime_sum();
150141cc406Sopenharmony_ci
151141cc406Sopenharmony_ci    return table;
152141cc406Sopenharmony_ci}
153141cc406Sopenharmony_ci
154141cc406Sopenharmony_cistd::ostream& operator<<(std::ostream& out, const MotorSlope& slope)
155141cc406Sopenharmony_ci{
156141cc406Sopenharmony_ci    out << "MotorSlope{\n"
157141cc406Sopenharmony_ci        << "    initial_speed_w: " << slope.initial_speed_w << '\n'
158141cc406Sopenharmony_ci        << "    max_speed_w: " << slope.max_speed_w << '\n'
159141cc406Sopenharmony_ci        << "    a: " << slope.acceleration << '\n'
160141cc406Sopenharmony_ci        << '}';
161141cc406Sopenharmony_ci    return out;
162141cc406Sopenharmony_ci}
163141cc406Sopenharmony_ci
164141cc406Sopenharmony_cistd::ostream& operator<<(std::ostream& out, const MotorProfile& profile)
165141cc406Sopenharmony_ci{
166141cc406Sopenharmony_ci    out << "MotorProfile{\n"
167141cc406Sopenharmony_ci        << "    max_exposure: " << profile.max_exposure << '\n'
168141cc406Sopenharmony_ci        << "    step_type: " << profile.step_type << '\n'
169141cc406Sopenharmony_ci        << "    motor_vref: " << profile.motor_vref << '\n'
170141cc406Sopenharmony_ci        << "    resolutions: " << format_indent_braced_list(4, profile.resolutions) << '\n'
171141cc406Sopenharmony_ci        << "    scan_methods: " << format_indent_braced_list(4, profile.scan_methods) << '\n'
172141cc406Sopenharmony_ci        << "    slope: " << format_indent_braced_list(4, profile.slope) << '\n'
173141cc406Sopenharmony_ci        << '}';
174141cc406Sopenharmony_ci    return out;
175141cc406Sopenharmony_ci}
176141cc406Sopenharmony_ci
177141cc406Sopenharmony_cistd::ostream& operator<<(std::ostream& out, const Genesys_Motor& motor)
178141cc406Sopenharmony_ci{
179141cc406Sopenharmony_ci    out << "Genesys_Motor{\n"
180141cc406Sopenharmony_ci        << "    id: " << motor.id << '\n'
181141cc406Sopenharmony_ci        << "    base_ydpi: " << motor.base_ydpi << '\n'
182141cc406Sopenharmony_ci        << "    profiles: "
183141cc406Sopenharmony_ci        << format_indent_braced_list(4, format_vector_indent_braced(4, "MotorProfile",
184141cc406Sopenharmony_ci                                                                    motor.profiles)) << '\n'
185141cc406Sopenharmony_ci        << "    fast_profiles: "
186141cc406Sopenharmony_ci        << format_indent_braced_list(4, format_vector_indent_braced(4, "MotorProfile",
187141cc406Sopenharmony_ci                                                                    motor.fast_profiles)) << '\n'
188141cc406Sopenharmony_ci        << '}';
189141cc406Sopenharmony_ci    return out;
190141cc406Sopenharmony_ci}
191141cc406Sopenharmony_ci
192141cc406Sopenharmony_ci} // namespace genesys
193