2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
// Typer.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Typer.hpp"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace Utility;
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
Typer::Typer(const char *string, HalfCycles delay, HalfCycles frequency, std::unique_ptr<CharacterMapper> character_mapper, Delegate *delegate) :
|
|
|
|
counter_(-delay),
|
|
|
|
frequency_(frequency),
|
|
|
|
string_pointer_(0),
|
|
|
|
delegate_(delegate),
|
|
|
|
phase_(0),
|
|
|
|
character_mapper_(std::move(character_mapper)) {
|
2016-11-05 18:47:09 +00:00
|
|
|
size_t string_size = strlen(string) + 3;
|
2016-12-03 15:55:50 +00:00
|
|
|
string_ = (char *)malloc(string_size);
|
2017-07-07 01:17:04 +00:00
|
|
|
snprintf(string_, string_size, "%c%s%c", Typer::BeginString, string, Typer::EndString);
|
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) {
|
2017-03-26 18:34:47 +00:00
|
|
|
if(string_) {
|
|
|
|
if(counter_ < 0 && counter_ + duration >= 0) {
|
|
|
|
if(!type_next_character()) {
|
2016-12-03 15:55:50 +00:00
|
|
|
delegate_->typer_reset(this);
|
2016-11-05 18:47:09 +00:00
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 15:55:50 +00:00
|
|
|
counter_ += duration;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(string_ && counter_ > frequency_) {
|
2016-12-03 15:55:50 +00:00
|
|
|
counter_ -= frequency_;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!type_next_character()) {
|
2016-12-03 15:55:50 +00:00
|
|
|
delegate_->typer_reset(this);
|
2016-11-05 18:47:09 +00:00
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
bool Typer::try_type_next_character() {
|
|
|
|
uint16_t *sequence = character_mapper_->sequence_for_character(string_[string_pointer_]);
|
|
|
|
|
|
|
|
if(!sequence || sequence[0] == CharacterMapper::NotMapped) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!phase_) delegate_->clear_all_keys();
|
|
|
|
else {
|
|
|
|
delegate_->set_key_state(sequence[phase_ - 1], true);
|
|
|
|
return sequence[phase_] == CharacterMapper::EndSequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Typer::type_next_character() {
|
2016-12-03 15:55:50 +00:00
|
|
|
if(string_ == nullptr) return false;
|
2016-08-06 18:33:24 +00:00
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
if(!try_type_next_character()) {
|
2016-12-03 15:55:50 +00:00
|
|
|
phase_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!string_[string_pointer_]) {
|
2016-12-03 15:55:50 +00:00
|
|
|
free(string_);
|
|
|
|
string_ = nullptr;
|
2016-08-06 18:33:24 +00:00
|
|
|
return false;
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 15:55:50 +00:00
|
|
|
string_pointer_++;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 15:55:50 +00:00
|
|
|
phase_++;
|
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
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Typer::~Typer() {
|
2016-12-03 15:55:50 +00:00
|
|
|
free(string_);
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
2016-11-05 18:47:09 +00:00
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
#pragma mark - Character mapper
|
2017-07-22 01:18:51 +00:00
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
uint16_t *CharacterMapper::table_lookup_sequence_for_character(KeySequence *sequences, size_t length, char character) {
|
2017-07-22 01:18:51 +00:00
|
|
|
size_t ucharacter = (size_t)((unsigned char)character);
|
|
|
|
if(ucharacter > (length / sizeof(KeySequence))) return nullptr;
|
|
|
|
if(sequences[ucharacter][0] == NotMapped) return nullptr;
|
|
|
|
return sequences[ucharacter];
|
|
|
|
}
|