arduino-simple-tts
Loading...
Searching...
No Matches
TimeToText.h
1#pragma once
2#include "NumberToText.h"
3
4namespace simple_tts {
5
12class TimeToText : public SimpleTTSBase {
13 public:
15 audio_tools::Vector<const char *> &say(int hour, int minutes) {
16 SimpleTime time(hour,minutes);
17 return say(time);
18 }
19
21 audio_tools::Vector<const char *> &say(SimpleTime time) {
22 result.clear();
23 LOGI("say: %d:%d",time.hour, time.minute);
24 if (time.minute < 0 || time.minute >= 60) {
25 LOGE("Invalid minute (range 0-59): %d", time.minute);
26 return result;
27 }
28 if (time.hour < 0 || time.hour > 24) {
29 LOGE("Invalid hour (range 0-24): %d", time.hour);
30 return result;
31 }
32
33 process(time);
34
35 // provide result to callback
36 if (callback){
37 callback(result, reference);
38 }
39
40 return result;
41 }
42
44 audio_tools::Vector<const char *> &allTexts() {
45 result.clear();
46 for (int j = 0; j < 10; j++) {
47 add(j);
48 }
49 return result;
50 }
51
52 protected:
53 audio_tools::Vector<const char *> result;
54 NumberToText ntt;
55 const int ITS = 0;
56 const int NOON = 1;
57 const int MIDNIGHT = 2;
58 const int OCLOCK = 3;
59 const int TO = 4;
60 const int PAST = 5;
61 const int AM = 6;
62 const int PM = 7;
63 const int HALF = 8;
64 const int QUARTER = 9;
65
66 const char *words[10] = {"ITS", "NOON", "MIDNIGHT", "OCLOCK", "TO",
67 "PAST", "AM", "PM", "HALF", "QUARTER"};
68
69 void add(int idx) { add(words[idx]); }
70
71 void add(const char *str) { result.push_back(str); }
72
73 void addAll(audio_tools::Vector<const char *> &words) {
74 for (auto word : words) {
75 add(word);
76 }
77 }
78
79 void process(SimpleTime time) {
80 add(ITS);
81 // process exceptional times
82 if (time.hour == 12 && time.minute == 00) {
83 add(NOON);
84 return;
85 }
86 if (time.hour == 0 && time.minute == 00) {
87 add(MIDNIGHT);
88 return;
89 }
90
91 // add minutes
92 processMinutes(time);
93
94 // add hour
95 if (time.hour <= 12) {
96 processHourAM(time);
97 } else {
98 processHourPM(time);
99 }
100 return;
101 }
102
103 void processMinutes(SimpleTime &time) {
104 // process minutes
105 if (time.minute == 0) {
106 // do nothing
107 } else if (time.minute == 15) {
108 add(QUARTER);
109 add(PAST);
110 } else if (time.minute < 30) {
111 addAll(ntt.say(time.minute,0u));
112 add(PAST);
113 } else if (time.minute == 30) {
114 add(HALF);
115 add(PAST);
116 } else if (time.minute == 45) {
117 time.hour += 1;
118 add(QUARTER);
119 add(TO);
120 } else if (time.minute > 30) {
121 time.hour += 1;
122 time.minute = 60 - time.minute;
123 addAll(ntt.say(time.minute,0u));
124 add(TO);
125 }
126 }
127
128 void processHourPM(SimpleTime time) {
129 int hour = time.hour;
130 hour -= 12;
131 // we do not say 0pm -> 12pm
132 if (hour == 0) {
133 hour = 12;
134 }
135 addAll(ntt.say(hour,0u));
136
137 if (time.minute==0){
138 add(OCLOCK);
139 }
140
141 add(PM);
142 }
143
144 void processHourAM(SimpleTime time) {
145 int hour = time.hour;
146 // we do not say 0am -> 12am
147 if (hour == 0) {
148 hour = 12;
149 }
150 addAll(ntt.say(hour,0u));
151
152 if (time.minute==0){
153 add(OCLOCK);
154 }
155
156 add(AM);
157 }
158};
159
160} // namespace simple_tts
Translates a number into englich words.
Definition: NumberToText.h:15
audio_tools::Vector< const char * > & say(double value, int decimals=2)
converts a real number to it's text representation (with the indicated number of digits)
Definition: NumberToText.h:19
Common Functionality for TTS classes.
Definition: SimpleTTSBase.h:15
Translates a time in hour and minutes into englich words. This implementation builds upon the NumberT...
Definition: TimeToText.h:12
audio_tools::Vector< const char * > & say(SimpleTime time)
converts a number to it's text representation
Definition: TimeToText.h:21
audio_tools::Vector< const char * > & say(int hour, int minutes)
converts a number to it's text representation
Definition: TimeToText.h:15
audio_tools::Vector< const char * > & allTexts()
provides all texts
Definition: TimeToText.h:44
Just a simple structure with the hour and minutes.
Definition: SimpleTTSBase.h:83