2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
// Typer.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Typer_hpp
|
|
|
|
#define Typer_hpp
|
|
|
|
|
|
|
|
#include <memory>
|
2017-12-29 20:26:03 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-10-21 14:30:02 +00:00
|
|
|
#include "../KeyboardMachine.hpp"
|
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
2016-06-19 17:10:52 +00:00
|
|
|
|
|
|
|
namespace Utility {
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/*!
|
|
|
|
An interface that provides a mapping from logical characters to the sequence of keys
|
|
|
|
necessary to type that character on a given machine.
|
|
|
|
*/
|
|
|
|
class CharacterMapper {
|
|
|
|
public:
|
|
|
|
/// @returns The EndSequence-terminated sequence of keys that would cause @c character to be typed.
|
|
|
|
virtual uint16_t *sequence_for_character(char character) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef uint16_t KeySequence[16];
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provided in the base class as a convenience: given the lookup table of key sequences @c sequences,
|
|
|
|
with @c length entries, returns the sequence for character @c character if it exists; otherwise
|
|
|
|
returns @c nullptr.
|
|
|
|
*/
|
2017-11-11 20:28:40 +00:00
|
|
|
uint16_t *table_lookup_sequence_for_character(KeySequence *sequences, std::size_t length, char character);
|
2017-08-03 15:42:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a stateful mechanism for typing a sequence of characters. Each character is mapped to a key sequence
|
|
|
|
by a character mapper. That key sequence is then replayed to a delegate.
|
|
|
|
|
|
|
|
Being given a delay and frequency at construction, the run_for interface can be used to produce time-based
|
|
|
|
typing. Alternatively, an owner may decline to use run_for and simply call type_next_character each time a
|
|
|
|
fresh key transition is ready to be consumed.
|
|
|
|
*/
|
2016-06-19 17:10:52 +00:00
|
|
|
class Typer {
|
|
|
|
public:
|
2018-03-09 20:19:02 +00:00
|
|
|
class Delegate: public KeyboardMachine::KeyActions {
|
2016-06-19 17:10:52 +00:00
|
|
|
public:
|
2016-11-05 19:28:03 +00:00
|
|
|
virtual void typer_reset(Typer *typer) = 0;
|
2016-06-19 17:10:52 +00:00
|
|
|
};
|
|
|
|
|
2017-12-29 20:26:03 +00:00
|
|
|
Typer(const std::string &string, HalfCycles delay, HalfCycles frequency, std::unique_ptr<CharacterMapper> character_mapper, Delegate *delegate);
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void run_for(const HalfCycles duration);
|
2016-08-06 18:33:24 +00:00
|
|
|
bool type_next_character();
|
2017-08-03 15:42:31 +00:00
|
|
|
bool is_completed();
|
|
|
|
|
2016-11-05 18:47:09 +00:00
|
|
|
const char BeginString = 0x02; // i.e. ASCII start of text
|
|
|
|
const char EndString = 0x03; // i.e. ASCII end of text
|
|
|
|
|
2016-06-19 17:10:52 +00:00
|
|
|
private:
|
2017-12-29 20:26:03 +00:00
|
|
|
std::string string_;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t string_pointer_ = 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2017-07-28 01:53:45 +00:00
|
|
|
HalfCycles frequency_;
|
|
|
|
HalfCycles counter_;
|
2017-11-11 03:35:05 +00:00
|
|
|
int phase_ = 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2016-12-03 15:55:50 +00:00
|
|
|
Delegate *delegate_;
|
2017-08-03 15:42:31 +00:00
|
|
|
std::unique_ptr<CharacterMapper> character_mapper_;
|
|
|
|
|
|
|
|
bool try_type_next_character();
|
2016-06-19 17:10:52 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/*!
|
|
|
|
Provides a default base class for type recipients: classes that want to attach a single typer at a time and
|
|
|
|
which may or may not want to nominate an initial delay and typing frequency.
|
|
|
|
*/
|
2016-06-19 17:10:52 +00:00
|
|
|
class TypeRecipient: public Typer::Delegate {
|
2018-03-09 20:19:02 +00:00
|
|
|
protected:
|
2017-08-03 15:42:31 +00:00
|
|
|
/// Attaches a typer to this class that will type @c string using @c character_mapper as a source.
|
2017-12-29 20:26:03 +00:00
|
|
|
void add_typer(const std::string &string, std::unique_ptr<CharacterMapper> character_mapper) {
|
2017-08-03 15:42:31 +00:00
|
|
|
typer_.reset(new Typer(string, get_typer_delay(), get_typer_frequency(), std::move(character_mapper), this));
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/*!
|
|
|
|
Provided in order to conform to that part of the Typer::Delegate interface that goes above and
|
|
|
|
beyond KeyboardMachine::Machine; responds to the end of typing by clearing all keys.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void typer_reset(Typer *typer) {
|
2016-11-05 19:28:03 +00:00
|
|
|
clear_all_keys();
|
2017-08-20 14:24:01 +00:00
|
|
|
|
|
|
|
// It's unsafe to deallocate typer right now, since it is the caller, but also it has a small
|
|
|
|
// memory footprint and it's desireable not to imply that the subclass need call it any more.
|
|
|
|
// So shuffle it off into a siding.
|
|
|
|
previous_typer_ = std::move(typer_);
|
|
|
|
typer_ = nullptr;
|
2016-11-05 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 01:53:45 +00:00
|
|
|
virtual HalfCycles get_typer_delay() { return HalfCycles(0); }
|
|
|
|
virtual HalfCycles get_typer_frequency() { return HalfCycles(0); }
|
2016-12-03 15:55:50 +00:00
|
|
|
std::unique_ptr<Typer> typer_;
|
2017-08-20 14:24:01 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Typer> previous_typer_;
|
2016-06-19 17:10:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Typer_hpp */
|