/* tested in arduino nano v3 (no wifi version, 2k memory, 30kb flash) */ 

#include "./yingshaoxo_lcd_st7920.h"

void print(unsigned char *a_string) {
    print_string(2, 0, "                ");
    print_string(2, 0, a_string);
    lcd7920_set_pins_to_low();
    delay(1000);
}

void print_number(int a_number) {
    char text[16];
    sprintf(text, "%d", a_number);
    print_string(2, 0, "                ");
    print_string(2, 0, text);
    lcd7920_set_pins_to_low();
    delay(1000);
}

void smart_print_string(unsigned int y, unsigned int x, unsigned char *string) {
    int length = strlen(string);
    unsigned char temp_string[16] = {'\0'};
    int temp_index = 0;
    int line_number = 0;
    for (int i=0; i<length; i++) {
        if (string[i] == '\n') {
            temp_index = 15;
        } else {
            temp_string[temp_index] = string[i];
        }
        if (temp_index >= 15) {
            temp_string[16] = '\0';
            lcd7920_print_string(y+line_number, x, temp_string);
            line_number += 1;
            temp_index = -1;
            for (int i2=0; i2<17; i2++) {
                temp_string[i2] = '\0';
            }
        }
        temp_index += 1;
    }
    lcd7920_print_string(y+line_number, x, temp_string);
}

void setup() {
    lcd7920_initialize_LCD();
    lcd7920_clear_text_screen();
}

int index = 0;
void loop() {
    delay(1000);
    if (index % 2 == 0) {
        smart_print_string(0, 0, "1. it is working!");
    } else {
        smart_print_string(0, 0, "2. it is working!");
    }
    index += 1;
}
