2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
// Typer.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Typer.hpp"
|
2017-11-11 01:37:18 +00:00
|
|
|
|
2016-06-19 17:10:52 +00:00
|
|
|
using namespace Utility;
|
|
|
|
|
2020-03-01 23:44:26 +00:00
|
|
|
Typer::Typer(const std::string &string, HalfCycles delay, HalfCycles frequency, CharacterMapper &character_mapper, Delegate *delegate) :
|
2017-08-03 15:42:31 +00:00
|
|
|
frequency_(frequency),
|
2017-11-11 03:35:05 +00:00
|
|
|
counter_(-delay),
|
2017-08-03 15:42:31 +00:00
|
|
|
delegate_(delegate),
|
2020-03-01 23:44:26 +00:00
|
|
|
character_mapper_(character_mapper) {
|
2020-02-29 22:34:21 +00:00
|
|
|
// Retain only those characters that actually map to something.
|
|
|
|
if(sequence_for_character(Typer::BeginString)) {
|
|
|
|
string_ += Typer::BeginString;
|
|
|
|
}
|
|
|
|
if(sequence_for_character(Typer::EndString)) {
|
|
|
|
string_ += Typer::EndString;
|
|
|
|
}
|
|
|
|
|
|
|
|
append(string);
|
2016-11-05 18:47:09 +00:00
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void Typer::run_for(const HalfCycles duration) {
|
2020-02-29 22:34:21 +00:00
|
|
|
if(string_pointer_ >= string_.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(counter_ < 0 && counter_ + duration >= 0) {
|
|
|
|
if(!type_next_character()) {
|
|
|
|
delegate_->typer_reset(this);
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
2020-02-29 22:34:21 +00:00
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
counter_ += duration;
|
|
|
|
while(string_pointer_ < string_.size() && counter_ > frequency_) {
|
|
|
|
counter_ -= frequency_;
|
|
|
|
if(!type_next_character()) {
|
|
|
|
delegate_->typer_reset(this);
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
void Typer::append(const std::string &string) {
|
|
|
|
// Remove any characters that are already completely done;
|
|
|
|
// otherwise things may accumulate here indefinitely.
|
2020-03-01 00:22:54 +00:00
|
|
|
// Note that sequence_for_character may seek to look one backwards,
|
|
|
|
// so keep 'the character before' if there was one.
|
|
|
|
if(string_pointer_ > 1) {
|
|
|
|
string_.erase(string_.begin(), string_.begin() + ssize_t(string_pointer_) - 1);
|
|
|
|
string_pointer_ = 1;
|
|
|
|
}
|
2020-02-29 22:34:21 +00:00
|
|
|
|
|
|
|
// If the final character in the string is not Typer::EndString
|
|
|
|
// then this machine doesn't need Begin and End, so don't worry about it.
|
|
|
|
ssize_t insertion_position = ssize_t(string_.size());
|
2024-09-30 17:37:44 +00:00
|
|
|
if(!string_.empty() && string_.back() == Typer::EndString) --insertion_position;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
string_.reserve(string_.size() + string.size());
|
|
|
|
for(const char c : string) {
|
|
|
|
if(sequence_for_character(c)) {
|
|
|
|
string_.insert(string_.begin() + insertion_position, c);
|
|
|
|
++insertion_position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint16_t *Typer::sequence_for_character(char c) const {
|
2020-03-01 23:44:26 +00:00
|
|
|
const uint16_t *const sequence = character_mapper_.sequence_for_character(c);
|
2020-04-02 03:19:34 +00:00
|
|
|
if(!sequence || sequence[0] == MachineTypes::MappedKeyboardMachine::KeyNotMapped) {
|
2020-02-29 22:34:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return sequence;
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
uint16_t Typer::try_type_next_character() {
|
2020-02-29 22:34:21 +00:00
|
|
|
const uint16_t *const sequence = sequence_for_character(string_[string_pointer_]);
|
|
|
|
|
|
|
|
if(!sequence) {
|
2020-02-29 23:12:32 +00:00
|
|
|
return 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 23:51:55 +00:00
|
|
|
// Advance phase.
|
|
|
|
++phase_;
|
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
// If this is the start of the output sequence, start with a reset all keys.
|
2020-03-01 00:22:54 +00:00
|
|
|
// Then pause if either: (i) the machine requires it; or (ii) this is the same
|
|
|
|
// character that was just typed, in which case the gap in presses will need to
|
|
|
|
// be clear.
|
2020-02-29 23:51:55 +00:00
|
|
|
if(phase_ == 1) {
|
2020-02-29 23:12:32 +00:00
|
|
|
delegate_->clear_all_keys();
|
2020-03-01 23:44:26 +00:00
|
|
|
if(character_mapper_.needs_pause_after_reset_all_keys() ||
|
2020-03-01 00:22:54 +00:00
|
|
|
(string_pointer_ > 0 && string_[string_pointer_ - 1] == string_[string_pointer_])) {
|
2020-02-29 23:12:32 +00:00
|
|
|
return 0xffff; // Arbitrarily. Anything non-zero will do.
|
|
|
|
}
|
2020-02-29 23:51:55 +00:00
|
|
|
++phase_;
|
2017-08-03 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
// If the sequence is over, stop.
|
2020-04-02 03:19:34 +00:00
|
|
|
if(sequence[phase_ - 2] == MachineTypes::MappedKeyboardMachine::KeyEndSequence) {
|
2020-02-29 23:12:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, type the key.
|
2020-02-29 23:51:55 +00:00
|
|
|
delegate_->set_key_state(sequence[phase_ - 2], true);
|
2020-02-29 23:12:32 +00:00
|
|
|
|
2020-02-29 23:51:55 +00:00
|
|
|
return sequence[phase_ - 2];
|
2017-08-03 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Typer::type_next_character() {
|
2017-12-29 20:26:03 +00:00
|
|
|
if(string_pointer_ == string_.size()) return false;
|
2016-08-06 18:33:24 +00:00
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
while(true) {
|
|
|
|
const uint16_t key_pressed = try_type_next_character();
|
|
|
|
|
|
|
|
if(!key_pressed) {
|
|
|
|
phase_ = 0;
|
|
|
|
++string_pointer_;
|
|
|
|
if(string_pointer_ == string_.size()) return false;
|
|
|
|
}
|
|
|
|
|
2020-03-01 23:44:26 +00:00
|
|
|
if(character_mapper_.needs_pause_after_key(key_pressed)) {
|
2020-02-29 23:12:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|
2016-08-06 18:33:24 +00:00
|
|
|
|
|
|
|
return true;
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|