// EME3153 EA Stage_1
// Student: Alan Lee
// Student ID: 250258181
// Class: 1B (EG114403)
// Wokwi: https://wokwi.com/projects/450978038241930241
#define SW_In_Pin 15
#define textasize 1200
char texta_buf[textasize];
int countoftexta = 0;
bool textacomplete = false;
char exceedtexta[1000];
unsigned long countofexceedtexta = 0;
long timea = 0;
bool textbt = false;
bool textc = true;
int textct = 0;
int textanf = 20000;
unsigned int loopinga = 1;
unsigned int maxloopinga = 10000;
void setup()
{
// Initialize serial port
Serial.begin(115200);
// Set pins
pinMode(SW_In_Pin, INPUT_PULLUP);
//Setup
textcp(0);
Serial.println("Please enter text, and I will count the number of characters in the text you input.");
Serial.print("The maximum number of characters that can be counted in the input text: ");
Serial.print(textasize);
Serial.println(" characters");
textbt = true;
textcp(0);
}
void loop()
{
if (Serial.available() > 0) {
while(Serial.available() > 0 && !textacomplete && loopinga <= maxloopinga) {
char texttempa = Serial.read();
loopinga++;
if (texttempa == '\n' || texttempa == '\r' || texttempa == '\0') {
textacomplete = true;
countofexceedtexta = 0;
break;
}
if (countoftexta < textasize) {
texta_buf[countoftexta] = texttempa;
countoftexta++;
}
}
if (!textacomplete && countoftexta >= textasize) {
textacomplete = true;
cleanchattext();
}
if (loopinga > maxloopinga) {
textcp(0);
Serial.println();
Serial.println("[Error] OverLooping");
textc = true;
textcp(0);
while(Serial.available() > 0) {
cleanchattext();
}
countofexceedtexta = 0;
textacomplete = false;
countoftexta = 0;
loopinga = 1;
}
} else {
if (textacomplete) {
cleanchattext();
texta();
// textb();
textcp(1);
Serial.println("Please input again: ");
textacomplete = false;
countoftexta = 0;
loopinga = 1;
}
if (!textbt && timea >= textanf) {
textcp(1);
Serial.println();
Serial.println("[System] We are ready! We look forward to your input.");
textcp(1);
timea = -3600000;
}
}
timea++;
delay(1);
}
void texta() {
textcp(0);
Serial.println();
Serial.print("Size of input data = ");
Serial.print(countoftexta);
Serial.println(" characters");
Serial.println("Input data: ");
for(int i = 0; i < countoftexta; i++) {
Serial.print(texta_buf[i]);
}
}
void textb() {
int tempa = map(countoftexta, 0, textasize, 0, 100);
if (digitalRead(SW_In_Pin))
{
textbt = true;
}
if (textbt) {
int tempta = tempa / 5;
int temptb = tempa % 5;
textcp(0);
Serial.print("Maximum of text size: ");
Serial.println(textasize);
Serial.print("Count of last text: ");
Serial.print(countoftexta);
Serial.print(" | Last text size percentage: ");
Serial.print(tempa);
Serial.println("% ");
Serial.println("Last text size percentage bar: ");
Serial.print(" 0% ");
for (int i = tempta; i > 0; i--){
Serial.print("■");
}
if (temptb > 0){
Serial.print("▥");
for (int i = (20 - tempta - 1); i > 0; i--){
Serial.print("□");
}
} else {
for (int i = (20 - tempta); i > 0; i--){
Serial.print("□");
}
}
Serial.print(" 100%(");
Serial.print(textasize);
Serial.println(")");
Serial.println("* Each block occupies 5%.");
textcp(1);
textcp(0);
textbt= false;
}
}
void textcp(int tempx) {
if (textct > 0 || tempx > 0) {
textc = true;
}
if (textc) {
Serial.println();
if (tempx == 0) {
Serial.println("=======================================================");
textc = false;
} else if (tempx == 1) {
Serial.println("-------------------------------------------------------");
}
}
textct = tempx;
}
void cleanchattext() {
countofexceedtexta = 0;
while(Serial.available() > 0 && countofexceedtexta < 1000) {
exceedtexta[countofexceedtexta] = Serial.read();
countofexceedtexta++;
}
while(Serial.available() > 0) {
Serial.read();
}
}