summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt17
-rw-r--r--src/calculator.cpp54
-rw-r--r--src/exceptions/tokenizer_exception.hpp13
-rw-r--r--src/include/calculator.hpp32
-rw-r--r--src/include/tokenizer.hpp47
-rw-r--r--src/pico-calc.cpp6
-rw-r--r--src/tokenizer.cpp125
7 files changed, 292 insertions, 2 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..89b60e6
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,17 @@
+# Dependencies
+# Keypad
+add_library(pico-keypad ../libs/pico-keypad/src/keypad.cpp)
+target_link_libraries(pico-keypad PUBLIC pico_stdlib hardware_gpio)
+target_include_directories(pico-keypad INTERFACE ../libs/pico-keypad/src)
+target_include_directories(pico-keypad PUBLIC ../libs/pico-keypad/src)
+# I2C LCD
+add_library(Pico-I2C-LCD ../libs/Pico-I2C-LCD/LCD_I2C.cpp)
+target_link_libraries(Pico-I2C-LCD PUBLIC pico_stdlib hardware_i2c)
+target_include_directories(Pico-I2C-LCD INTERFACE ../libs/Pico-I2C-LCD/)
+target_include_directories(Pico-I2C-LCD PUBLIC ../libs/Pico-I2C-LCD/)
+
+# Main executable
+add_executable(pico-calc pico-calc.cpp calculator.cpp tokenizer.cpp)
+target_link_libraries(pico-calc
+ hardware_i2c Pico-I2C-LCD pico-keypad hardware_timer)
+target_include_directories(pico-calc PUBLIC include exceptions)
diff --git a/src/calculator.cpp b/src/calculator.cpp
new file mode 100644
index 0000000..5de706b
--- /dev/null
+++ b/src/calculator.cpp
@@ -0,0 +1,54 @@
+#include <new>
+#include <string>
+#include <array>
+#include "hardware/i2c.h"
+#include "pico/time.h"
+#include "LCD_I2C.hpp"
+#include "keypad.hpp"
+#include "include/calculator.hpp"
+#include "include/tokenizer.hpp"
+
+
+Calculator::Calculator() {
+ display = new LCD_I2C(I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS,
+ PICO_DEFAULT_I2C_INSTANCE,
+ PICO_DEFAULT_I2C_SDA_PIN,
+ PICO_DEFAULT_I2C_SCL_PIN);
+ default_keypad = new Keypad(keypad_row_pins, keypad_col_pins,
+ default_keypad_chars);
+ display->BacklightOn();
+}
+
+Calculator::~Calculator() {
+ delete display;
+ delete default_keypad;
+}
+
+void Calculator::run() {
+ while(true) {
+ display->Clear();
+ std::string input;
+ short str_len = 0;
+ char pressed_key = default_keypad->getKey();
+
+ if(pressed_key == '\0')
+ continue;
+
+ while(str_len <= 15) {
+ pressed_key = default_keypad->getKey();
+
+ if(pressed_key != '\0' && pressed_key != '=') {
+ display->PrintChar(pressed_key);
+ input += pressed_key;
+ str_len += 1;
+ }
+
+ if(pressed_key == '=') {
+ std::array tokens = tokenizer.tokenize(input);
+ break;
+ }
+
+ busy_wait_ms(170);
+ }
+ }
+}
diff --git a/src/exceptions/tokenizer_exception.hpp b/src/exceptions/tokenizer_exception.hpp
new file mode 100644
index 0000000..2366bb1
--- /dev/null
+++ b/src/exceptions/tokenizer_exception.hpp
@@ -0,0 +1,13 @@
+#pragma once
+#include <string>
+
+class TokenizerException {
+private:
+ std::string message;
+public:
+ TokenizerException(std::string error) : message{error} {}
+ const std::string &getError() const {
+ return message;
+ }
+};
+
diff --git a/src/include/calculator.hpp b/src/include/calculator.hpp
new file mode 100644
index 0000000..8c5e97a
--- /dev/null
+++ b/src/include/calculator.hpp
@@ -0,0 +1,32 @@
+#pragma once
+#include "LCD_I2C.hpp"
+#include "keypad.hpp"
+#include "tokenizer.hpp"
+#define LCD_COLUMNS 16
+#define LCD_ROWS 2
+#define I2C_ADDRESS 0x27
+
+class Calculator {
+private:
+ LCD_I2C *display;
+ Keypad *default_keypad;
+ Tokenizer tokenizer = Tokenizer();
+ char default_keypad_chars[4][4] = {
+ {'1', '2', '3', '+'},
+ {'4', '5', '6', '-'},
+ {'7', '8', '9', '*'},
+ {'(', '0', ')', '='}
+ };
+ char secondary_keypad[4][4] = {
+ {'1', '2', '3', '+'},
+ {'4', '5', '6', '-'},
+ {'7', '8', '9', '/'},
+ {'(', '0', ')', '='}
+ };
+ uint keypad_col_pins[4] = {6, 7, 8, 9};
+ uint keypad_row_pins[4] = {16, 17, 18, 19};
+public:
+ Calculator();
+ ~Calculator();
+ void run();
+};
diff --git a/src/include/tokenizer.hpp b/src/include/tokenizer.hpp
new file mode 100644
index 0000000..a3f9998
--- /dev/null
+++ b/src/include/tokenizer.hpp
@@ -0,0 +1,47 @@
+#pragma once
+#include <regex>
+#include <string>
+#include <array>
+
+enum Type {
+ operand,
+ sum,
+ substraction,
+ multiplication,
+ division,
+ left_parens,
+ right_parens,
+ equals
+};
+
+struct Token {
+ Type type;
+ int value;
+};
+
+class Tokenizer {
+private:
+ // Private attributes
+ std::array<Token *, 16> tokens;
+ const std::string *to_tokenize;
+ size_t current_char_index;
+ short tokens_head;
+ // Regexes
+ std::regex operand_regex = std::regex("^0{1}|\\d+");
+ std::regex operation_regex = std::regex("\\+|-|/|\\*");
+ std::regex parens_regex = std::regex("(|)");
+ // End of private attributes
+ // Private methods
+ void clearTokens();
+ void insertToken(Type type, int value);
+ void matchOperation(const std::string &operation);
+ void matchParens(const std::string &operation);
+ void matchOperand(const std::string &operation);
+ std::string buildNumber(char *current);
+ // End of private methods
+public:
+ Tokenizer();
+ ~Tokenizer();
+ const std::array<Token *, 16> &tokenize(const std::string &operation);
+ const std::array<Token *, 16> &getTokens();
+};
diff --git a/src/pico-calc.cpp b/src/pico-calc.cpp
index 14ab8b3..91228e5 100644
--- a/src/pico-calc.cpp
+++ b/src/pico-calc.cpp
@@ -1,6 +1,8 @@
-#include "pico-keypad/src/keypad.hpp"
-#include "Pico-I2C-LCD/LCD_I2C.hpp"
+#include "include/calculator.hpp"
int main() {
+ Calculator calc = Calculator();
+ calc.run();
+
return 0;
}
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
new file mode 100644
index 0000000..a156000
--- /dev/null
+++ b/src/tokenizer.cpp
@@ -0,0 +1,125 @@
+#include <string>
+#include <array>
+#include "include/tokenizer.hpp"
+#include "exceptions/tokenizer_exception.hpp"
+
+Tokenizer::Tokenizer() {
+ tokens.fill(nullptr);
+ tokens_head = 0;
+}
+
+Tokenizer::~Tokenizer() {
+ clearTokens();
+}
+
+void Tokenizer::clearTokens() {
+ for(auto token{ tokens.begin() }; *token != nullptr; ++token)
+ delete token;
+
+ tokens.fill(nullptr);
+}
+
+void Tokenizer::insertToken(Type type, int value = 0) {
+ Token *new_token = new Token { .type = type, .value = value};
+
+ tokens[tokens_head] = new_token;
+ tokens_head += 1;
+}
+
+
+const std::array<Token *, 16>
+&Tokenizer::tokenize(const std::string &operation) {
+ to_tokenize = &operation;
+ current_char_index = 0;
+
+ while(current_char_index < operation.length()) {
+ size_t unchanged_index = current_char_index;
+
+ matchOperand(operation);
+ matchParens(operation);
+ matchOperation(operation);
+
+ // No matches.
+ if(unchanged_index == current_char_index)
+ throw TokenizerException("Invalid character detected.");
+ }
+
+ return getTokens();
+}
+
+void Tokenizer::matchOperand(const std::string &operation) {
+ std::smatch match;
+
+ if(current_char_index > operation.length())
+ return;
+
+ std::string remaining(operation.substr(current_char_index));
+ bool result = std::regex_search(remaining, match,
+ operand_regex);
+
+ if(!result)
+ return;
+
+ insertToken(operand, std::stoi(match.str(0)));
+ current_char_index += match.length(0);
+}
+
+void Tokenizer::matchParens(const std::string &operation) {
+ std::smatch match;
+
+ if(current_char_index > operation.length())
+ return;
+
+ std::string remaining(operation.substr(current_char_index));
+ bool result = std::regex_search(remaining, match,
+ operand_regex);
+
+ if(!result)
+ return;
+
+ switch(match.str(0)[0]) {
+ case '(':
+ insertToken(left_parens);
+ break;
+ case ')':
+ insertToken(right_parens);
+ break;
+ }
+
+ current_char_index += 1;
+}
+
+void Tokenizer::matchOperation(const std::string &operation) {
+ std::smatch match;
+
+ if(current_char_index > operation.length())
+ return;
+
+ std::string remaining(operation.substr(current_char_index));
+ bool result = std::regex_search(remaining, match,
+ operand_regex);
+
+ if(!result)
+ return;
+
+ switch(match.str(0)[0]) {
+ case '+':
+ insertToken(sum);
+ break;
+ case '-':
+ insertToken(substraction);
+ break;
+ case '*':
+ insertToken(multiplication);
+ break;
+ case '/':
+ insertToken(division);
+ break;
+ }
+
+ current_char_index += 1;
+}
+
+const std::array<Token *, 16> &Tokenizer::getTokens() {
+ return tokens;
+}