portapack-mayhem/firmware/application/ui/ui_receiver.hpp
Maescool 920b98f7c9 Upstream merge to make new revision of PortaPack work (#206)
* Power: Turn off additional peripheral clock branches.

* Update schematic with new symbol table and KiCad standard symbols.
Fix up wires.

* Schematic: Update power net labels.

* Schematic: Update footprint names to match library changes.

* Schematic: Update header vendor and part numbers.

* Schematic: Specify (arbitrary) value for PDN# net.

* Schematic: Remove fourth fiducial. Not standard practice, and was taking up valuable board space.

* Schematic: Add reference oscillator -- options for clipped sine or HCMOS output.

* Schematic: Update copyright year.

* Schematic: Remove CLKOUT to CPLD. It was a half-baked idea.

* Schematic: Add (experimental) GPS circuit.
Add note about charging circuit.
Update date and revision to match PCB.

* PCB: Update from schematic change: now revision 20180819.
Diff was extensive due to net renumbering...

* PCB: Fix GPS courtyard to accommodate crazy solder paste recommendation in integration manual.
PCB: Address DRC clearance violation between via and oscillator pad.

* PCB: Update copyright on drawing.

* Update schematic and PCB date and revision.

* gitignore: Sublime Text editor project/workspace files

* Power: Power up or power down peripheral clock at appropriate times, so firmware doesn't freeze...

* Clocking: Fix incorrect shift for CGU IDIVx_CTRL.PD field.

* LPC43xx: Add CGU IDIVx struct/union type.

* Power: Switch off unused IDIV dividers. Make note of active IDIVs and their use.

* HackRF Mode: Upgrade firmware to 2018.01.1 (API 1.02)

* MAX V CPLD: Refactor class to look more like Xilinx CoolRunner II CPLD class.

* MAX V CPLD: Add BYPASS, SAMPLE support.
Rename enter_isp -> enable, exit_isp -> disable.
Use SAMPLE at start of flash process, which somehow addresses the problem where CFM wouldn't load into SRAM (and become the active bitstream) after flashing.

* MAX V CPLD: Reverse verify data checking logic to make it a little faster.

* CPLD: After reprogramming flash, immediately clamp I/O signals, load to SRAM, and "execute" the new bitstream.

* Si5351: Refactor code, make one of the registers more type-safe.
Clock Manager: Track selected reference clock source for later use in user interface.

* Clock Manager: Add note about PPM only affecting Si5351C PLLA, which always runs from the HackRF 25MHz crystal.
It is assumed an external clock does not need adjustment, though I am open to being convinced otherwise...

* PPM UI: Show "EXT" when showing PPM adjustment and reference clock is external.

* CPLD: Add pins and logic for new PortaPack hardware feature(s).

* CPLD: Bitstream to support new hardware features.

* Clock Generator: Add a couple more setter methods for ClockControl registers.

* Clock Manager: Use shared MCU CLKIN clock control configuration constant.

* Clock Manager: Reduce MCU CLKIN driver current. 2mA should be plenty.

* Clock Manager: Remove redundant clock generator output enable.

* Bootstrap: Remove unnecessary ldscript hack to locate SPIFI mode change code in RAM.

* Bootstrap: Get CPU operating at max frequency as soon as possible.
Update SPIFI speed comment.
Make some more LPC43xx types into unions with uint32_t.

* Bootstrap: Explicitly configure IDIVB for SPIFI, despite LPC43xx bootloader setting it.

* Clock Manager: Init peripherals before CPLD reconfig. Do the clock generator setup after, so we can check presence of PortaPack reference clock with the help of the latest CPLD bitstream.

* Clock Manager: Reverse sense of conditional that determines crystal or non-crystal reference source. This is for an expected upcoming change where multiple external options can be differentiated.

* Bootstrap: Consolidate clock configuration, update SPIFI rate comment.

* Clock Manager: Use IDIVA for clock source for all peripherals, instead of PLL1. Should make switching easier going forward.
Don't use IRC as clock during initial clock manager configuration. Until we switch to GP_CLKIN, we should go flat out...

* ChibiOS M0: Change default clock speed to 204MHz, since bootstrap now maxes out clock speed before starting M0 execution.

* PortaPack IO: Expose method to set reference oscillator enable pin.

* Pin configuration: Do SPIFI pin config with other pins, in preparation for eliminating separate bootloader.

* Pin configuration: Disable input buffers on pins that are never read.

* Revert "ChibiOS M0: Change default clock speed to 204MHz, since bootstrap now maxes out clock speed before starting M0 execution."

This reverts commit c0e2bb6cc4cc656769323bdbb8ee5a16d2d5bb03.

* Remove unused board files.

* Add LPC43xx functions.

* chibios: Replace code with per-peripheral structs defining clocks, interrupts, and reset bits.

* LPC43xx: Add MCPWM peripheral struct.

* clock generator: Use recommended PLL reset register value.

Datasheet recommends a value. AN619 is quiet on the topic, claims the low nibble is default 0b0000.

* GPIO: Tweak masking of SCU function.

I don't remember why I thought this was necessary...

* HAL: Explicitly turn on timer peripheral clocks used as systicks, during init.

* SCU: Add struct to hold pin configuration.

* PAL: Add functions to address The Glitch.

https://greatscottgadgets.com/2018/02-28-we-fixed-the-glitch/

* PAL/board: New IO initialization code

Declare initial state for SCU pin config, GPIOs. Apply initial state during PAL init. Perform VAA slow turn-on to address The Glitch.

* Merge M0 and M4 to eliminate need for bootstrap firmware

During _early_init, detect if we're running on the M4 or M0.
If M4: do M4-specific core initialization, reset peripherals, speed up SPIFI clock, start M0, go to sleep.
If M0: do all the other things.

* Pins: Miscellaneous SCU configuration tweaks.

* Little code clarity improvement.

* bootstrap: Remove, not necessary.

* Clock Manager: Large re-working to support external references.

* Fix merge conflicts
2019-01-11 06:56:21 +00:00

347 lines
7.1 KiB
C++

/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* 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, 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __UI_RECEIVER_H__
#define __UI_RECEIVER_H__
#include "ui.hpp"
#include "ui_navigation.hpp"
#include "ui_painter.hpp"
#include "ui_widget.hpp"
#include "rf_path.hpp"
#include <cstddef>
#include <cstdint>
#include <algorithm>
#include <functional>
namespace ui {
class FrequencyField : public Widget {
public:
std::function<void(rf::Frequency)> on_change { };
std::function<void(void)> on_edit { };
std::function<void(void)> on_show_options { };
using range_t = rf::FrequencyRange;
FrequencyField(const Point parent_pos);
rf::Frequency value() const;
void set_value(rf::Frequency new_value);
void set_step(rf::Frequency new_value);
void paint(Painter& painter) override;
bool on_key(const ui::KeyEvent event) override;
bool on_encoder(const EncoderEvent delta) override;
bool on_touch(const TouchEvent event) override;
void on_focus() override;
private:
const size_t length_;
const range_t range;
rf::Frequency value_ { 0 };
rf::Frequency step { 25000 };
rf::Frequency clamp_value(rf::Frequency value);
};
template<size_t N>
class FieldString {
public:
enum Justify {
Right,
Left,
};
constexpr FieldString(
Justify justify
) : justify { justify }
{
}
uint32_t as_int() const {
uint32_t value = 0;
for(const auto c : s) {
const uint_fast8_t digit = (c == ' ') ? 0 : c - '0';
value = (value * 10) + digit;
}
return value;
}
void set(uint32_t value) {
std::generate(s.rbegin(), s.rend(), [&value]() {
const char digit = (value % 10) + '0';
value /= 10;
return digit;
});
}
void clear() {
s.fill(' ');
}
void add_digit(const char c) {
insert_right(c);
}
void delete_digit() {
if( justify == Justify::Right ) {
shift_right();
s.front() = ' ';
} else {
auto first_digit = std::find_if(s.rbegin(), s.rend(), [](const char& a) {
return a != ' ';
});
if( first_digit != s.rend() ) {
*first_digit = ' ';
}
}
}
std::string as_string() const {
return { s.data(), s.size() };
}
void remove_leading_zeros() {
remove_zeros(s.begin(), s.end());
}
void remove_trailing_zeros() {
remove_zeros(s.rbegin(), s.rend());
}
private:
using array_type = std::array<char, N>;
array_type s { };
Justify justify { Justify::Left };
template<typename Iterator>
void remove_zeros(Iterator begin, Iterator end) {
const auto first_significant_digit =
std::find_if(begin, end, [](const char& a) {
return a != '0';
});
std::fill(begin, first_significant_digit, ' ');
}
void insert_right(const char c) {
auto insert_point = s.end() - 1;
if( justify == Justify::Left ) {
insert_point = std::find_if(s.begin(), s.end(), [](const char& a) {
return a == ' ';
});
}
if( *insert_point != ' ' ) {
insert_point = shift_left();
}
*insert_point = c;
}
typename array_type::iterator shift_left() {
return std::move(s.begin() + 1, s.end(), s.begin());
}
typename array_type::iterator shift_right() {
return std::move_backward(s.begin(), s.end() - 1, s.end());
}
};
class FrequencyKeypadView : public View {
public:
std::function<void(rf::Frequency)> on_changed { };
FrequencyKeypadView(
NavigationView& nav,
const rf::Frequency value
);
void focus() override;
rf::Frequency value() const;
void set_value(const rf::Frequency new_value);
private:
static constexpr int button_w = 240 / 3;
static constexpr int button_h = 48;
static constexpr int mhz_digits = 4;
static constexpr int submhz_digits = 4;
static constexpr int mhz_mod = pow(10, mhz_digits);
static constexpr int submhz_base = pow(10, 6 - submhz_digits);
static constexpr int text_digits = mhz_digits + 1 + submhz_digits;
Text text_value {
{ 0, 4, 240, 16 }
};
std::array<Button, 12> buttons { };
Button button_save {
{ 0, button_h * 5, 60, button_h },
"Save"
};
Button button_load {
{ 60, button_h * 5, 60, button_h },
"Load"
};
Button button_close {
{ 128, button_h * 5, 112, button_h },
"Done"
};
/* TODO: Template arg required in enum?! */
FieldString<mhz_digits> mhz { FieldString<4>::Justify::Right };
FieldString<submhz_digits> submhz { FieldString<4>::Justify::Left };
enum State {
DigitMHz,
DigitSubMHz
};
State state { DigitMHz };
bool clear_field_if_digits_entered { true };
void on_button(Button& button);
void digit_add(const char c);
void digit_delete();
void field_toggle();
void update_text();
};
class FrequencyStepView : public OptionsField {
public:
FrequencyStepView(
Point parent_pos
) : OptionsField {
parent_pos,
5,
{
{ " 100", 100 },
{ " 1k ", 1000 },
{ " 3k ", 3000 }, /* Approximate SSB bandwidth */
{ " 5k ", 5000 },
{ " 6k3", 6250 },
{ " 9k ", 9000 }, /* channel spacing for LF, MF in some regions */
{ " 10k ", 10000 },
{ " 12k5", 12500 },
{ " 25k ", 25000 },
{ "100k ", 100000 },
{ " 1M ", 1000000 },
{ " 10M ", 10000000 },
}
}
{
}
};
class FrequencyOptionsView : public View {
public:
std::function<void(rf::Frequency)> on_change_step { };
std::function<void(int32_t)> on_change_reference_ppm_correction { };
FrequencyOptionsView(const Rect parent_rect, const Style* const style);
void set_step(rf::Frequency f);
void set_reference_ppm_correction(int32_t v);
private:
Text text_step {
{ 0 * 8, 0 * 16, 4 * 8, 1 * 16 },
"Step"
};
FrequencyStepView field_step {
{ 5 * 8, 0 * 16 },
};
void on_step_changed(rf::Frequency v);
void on_reference_ppm_correction_changed(int32_t v);
NumberField field_ppm {
{ 23 * 8, 0 * 16 },
3,
{ -99, 99 },
1,
'0',
};
Text text_ext {
{ 23 * 8, 0 * 16, 3 * 8, 1 * 16 },
"EXT",
};
Text text_ppm {
{ 27 * 8, 0 * 16, 3 * 8, 16 },
"PPM",
};
};
class RFAmpField : public NumberField {
public:
RFAmpField(Point parent_pos);
};
class RadioGainOptionsView : public View {
public:
RadioGainOptionsView(const Rect parent_rect, const Style* const style);
private:
Text label_rf_amp {
{ 0 * 8, 0 * 16, 3 * 8, 1 * 16 },
"Amp"
};
RFAmpField field_rf_amp {
{ 4 * 8, 0 * 16},
};
};
class LNAGainField : public NumberField {
public:
std::function<void(void)> on_show_options { };
LNAGainField(Point parent_pos);
void on_focus() override;
};
class VGAGainField : public NumberField {
public:
std::function<void(void)> on_show_options { };
VGAGainField(Point parent_pos);
void on_focus() override;
};
} /* namespace ui */
#endif/*__UI_RECEIVER_H__*/