diff --git a/docs/modules/gsm.md b/docs/modules/gsm.md new file mode 100644 index 0000000..10ec507 --- /dev/null +++ b/docs/modules/gsm.md @@ -0,0 +1,117 @@ +# GSM module + +This document details the implementation of the GSM module within the PaxOS operating system. The module handles communication with a GSM/GPRS modem, enabling functionalities like sending/receiving SMS messages, making/receiving calls, and managing MMS. + +## 1. `contacts.cpp` / `contacts.hpp` + +This module manages contact information. Contacts are stored in a JSON file (`/apps/contacts/list.json`) and loaded into memory for quick access. + +### Data Structures + +* **`Contacts::contact`:** Represents a single contact with `name` (std::string) and `phone` (std::string) fields. + +### Functions + +* **`Contacts::load()`:** Loads contacts from the `list.json` file into the `contactList` vector. Handles JSON parsing errors. +* **`Contacts::save()`:** Saves the `contactList` to the `list.json` file in JSON format. +* **`Contacts::listContacts()`:** Returns a copy of the `contactList`. +* **`Contacts::addContact(contact c)`:** Adds a new contact to the `contactList`. +* **`Contacts::deleteContact(std::string name)`:** Deletes a contact with the given name from the `contactList`. +* **`Contacts::editContact(std::string name, contact c)`:** Edits the contact with the given name, updating its information with the provided `contact` object. +* **`Contacts::getContact(std::string name)`:** Returns the `contact` object associated with the given name. Returns an empty contact if not found. +* **`Contacts::getByNumber(std::string number)`:** Returns the `contact` object associated with the given phone number. Returns an empty contact if not found. + + +## 2. `conversation.cpp` / `conversation.hpp` + +This module handles conversation data, including loading and saving messages for each conversation. + +### Data Structures + +* **`Conversations::Message`:** Represents a single message with `message` (std::string), `who` (bool indicating sender - false for self, true for other), and `date` (std::string) fields. +* **`Conversations::Conversation`:** Represents a conversation with a specific `number` (std::string) and a vector of `messages` (std::vector). + +### Functions + +* **`Conversations::loadConversation(const storage::Path &filePath, Conversation &conv)`:** Loads a conversation from the specified JSON file. Handles file errors and JSON parsing exceptions. +* **`Conversations::saveConversation(const storage::Path &filePath, const Conversation &conv)`:** Saves a conversation to the specified JSON file. Limits the number of saved messages to `MAX_MESSAGES` (40), keeping the most recent ones. Creates the necessary directories if they don't exist. + + +## 3. `gsm.cpp` / `gsm.hpp` + +This module provides the core GSM functionality, interacting directly with the GSM modem. It manages calls, SMS, MMS, network status, and battery level. + +### Constants + +* `BAUDRATE`: Default baud rate for serial communication (921600). +* `MESSAGES_LOCATION`: Directory for storing message data (`apps/messages/data`). +* `MESSAGES_IMAGES_LOCATION`: Directory for storing MMS images (`apps/messages/images`). +* `MESSAGES_NOTIF_LOCATION`: File storing unread message notifications (`apps/messages/unread.txt`). +* `MAX_MESSAGES`: Maximum number of messages stored per conversation (40). + +### Data Structures + +* **`GSM::Request`:** Represents a request to be executed by the GSM module, with a `function` (std::function) and a `priority` (enum). +* **`GSM::Key`:** Represents a key string to watch for in the GSM modem's responses, with a `key` (std::string) and a `function` (std::function) to execute when the key is found. +* **`GSM::State`:** Represents the current state of the GSM module (call state, call failure, calling number). +* **`GSM::Message`:** Represents an SMS message, with `number`, `message`, and `date`. +* **`GSM::ExternalEvents`:** Namespace containing callback functions for incoming calls, new messages, and message errors. +* **`GSM::Stream::Chunk`:** Used for streaming MMS data; contains a data pointer and size. (ESP_PLATFORM only) + +### Functions + +* **`GSM::init()`:** Initializes the GSM modem, including setting baud rate and handling modem power-on (ESP_PLATFORM specific). +* **`GSM::reInit()`:** Re-initializes the serial communication after clock speed changes. +* **`GSM::download(uint64_t timeout = 50)`:** Reads data from the GSM modem's serial port with a timeout. +* **`GSM::send(const std::string &message, const std::string &answerKey, uint64_t timeout = 200)`:** Sends a command to the GSM modem and waits for a specific response key. Returns the modem's full response. +* **`GSM::appendRequest(Request request)`:** Adds a request to the queue of requests to be processed by the GSM module. +* **`GSM::process()`:** Processes received data, checking for key strings and executing associated functions. +* **`GSM::checkRequest()`:** Executes pending requests in the queue, prioritizing high-priority requests. +* **`GSM::onRinging()`:** Handles incoming call events, updating the module's state. +* **`GSM::onHangOff()`:** Handles call hang-up events. +* **`GSM::onMessage()`:** Handles incoming SMS/MMS messages. Decodes PDUs, saves messages to conversations, and triggers the `onNewMessage` callback. +* **`GSM::sendMessage(const std::string &number, const std::string &message)`:** Sends an SMS message. Saves the sent message to the conversation history. +* **`GSM::newMessage(std::string number, std::string message)`:** Queues a message to be sent. +* **`GSM::sendCall(const std::string &number)`:** Initiates a call to the specified number. +* **`GSM::newCall(std::string number)`:** Queues a call to be made. +* **`GSM::endCall()`:** Ends the current call. +* **`GSM::acceptCall()`:** Accepts an incoming call. +* **`GSM::rejectCall()`:** Rejects an incoming call. +* **`GSM::getVoltage()`:** Retrieves the battery voltage. +* **`GSM::getBatteryLevel()`:** Calculates and returns the battery level (0.0 - 1.0) based on voltage. +* **`GSM::updateHour()`:** Updates the current time from the GSM modem. +* **`GSM::getHour()`:** Queues a request to update the time. +* **`GSM::getNetworkStatus()`:** Returns the network signal quality. +* **`GSM::updateNetworkQuality()`:** Updates the network signal quality. +* **`GSM::getNetworkQuality()`:** Queues a request to update the network quality. +* **`GSM::isFlightMode()`:** Checks if flight mode is enabled. +* **`GSM::setFlightMode(bool mode)`:** Sets the flight mode. +* **`GSM::run()`:** Main loop of the GSM module. Handles initialization, request processing, and event handling. +* **`GSM::getHttpMMS(std::string number, std::string url)`:** Downloads and processes MMS messages (ESP_PLATFORM only). + + +## 4. `message.cpp` / `message.hpp` + +Provides functions for loading and saving messages from/to JSON files. This module seems redundant given the similar functionality in `conversation.cpp/hpp`, and its usage isn't clear within the provided code. + +### Data Structures + +* **`Message::Message`:** Same structure as `Conversations::Message`. + +### Functions + +* **`Message::loadMessages(const std::string& filePath, std::vector& messages)`:** Loads messages from a JSON file. +* **`Message::saveMessages(const std::string& filePath, const std::vector& messages)`:** Saves messages to a JSON file. + + +## 5. `pdu.cpp` / `pdu.hpp` + +This module handles the decoding of PDU-formatted messages received from the GSM modem. + +### Data Structures + +* **`PDU`:** Contains the decoded information from a PDU string: `sender` (std::string), `message` (std::string), `url` (std::string for MMS URLs), and `type` (enum PDU_type: SMS, MMS, UNKNOWN). + +### Functions + +* **`decodePDU(std::string pdu)`:** Decodes a PDU string and returns a `PDU` object containing the extracted information (sender, message, URL, type). diff --git a/docs/modules/gui.md b/docs/modules/gui.md new file mode 100644 index 0000000..30ae657 --- /dev/null +++ b/docs/modules/gui.md @@ -0,0 +1,10 @@ +# Module graphique + +S'occupe de la gestion de l'affichage + +Le module est séparé en deux parties: + - une partie pour la gestion des buffers, gestion de l'écran + - une partie pour la gestion de la hirarchie des widgets, leur rendu, l'algorithme de rendu... + + +La classe Widget représente la base de construction d'un widget diff --git a/docs/modules/storage.md b/docs/modules/storage.md new file mode 100644 index 0000000..98fdad6 --- /dev/null +++ b/docs/modules/storage.md @@ -0,0 +1,95 @@ +# Storage module + +This document details the `storage` and `appFile` modules used within the Paxos project for file system interaction. The modules provide a platform-agnostic interface for file and directory manipulation, reading, and writing. + +## 1. `storage` Module + +This module provides the core file system functionalities. + +### 1.1 `storage::Path` Class + +Represents a file or directory path. + +#### 1.1.1 Constructors + +* `Path()`: Creates an empty path. +* `Path(const std::string& raw)`: Creates a path from a string, parsing and simplifying it. +* `Path(const Path& other)`: Copy constructor. + +#### 1.1.2 Methods + +* `join(const Path& other)`: Appends another path to this path. +* `join(const std::string& other)`: Appends a string representation of a path to this path. +* `operator/(const Path& other) const`: Returns a new path by joining this path with another. +* `operator/(const std::string& other) const`: Returns a new path by joining this path with a string representation of a path. +* `operator/=(const Path& other)`: Appends another path to this path (in-place). +* `operator/=(const std::string& other)`: Appends a string representation of a path to this path (in-place). +* `operator=(const Path& other)`: Assignment operator. +* `operator=(const std::string& other)`: Assigns a string representation of a path to this path. +* `operator==(const Path& other) const`: Equality operator. +* `assign(const Path& other)`: Assigns another path to this path. +* `assign(const std::string& other)`: Assigns a string representation of a path to this path. +* `clear()`: Clears the path. +* `str() const`: Returns the string representation of the path. +* `listdir(bool onlyDirs = false) const`: Returns a vector of filenames within the directory represented by this path. If `onlyDirs` is true, only directory names are returned. +* `exists() const`: Checks if the path exists. +* `isfile() const`: Checks if the path represents a file. +* `isdir() const`: Checks if the path represents a directory. +* `newfile() const`: Creates a new empty file at the specified path. +* `newdir() const`: Creates a new directory at the specified path. +* `remove() const`: Removes the file or directory at the specified path. +* `rename(const Path& to)`: Renames the file or directory at the specified path. + +### 1.2 `storage::FileStream` Class + +Provides an interface for reading and writing files. + +#### 1.2.1 Constructors + +* `FileStream()`: Creates an empty filestream. +* `FileStream(const std::string& path, Mode mode)`: Creates a filestream and opens the specified file with the given mode. + +#### 1.2.2 Methods + +* `open(const std::string& path, Mode mode)`: Opens the specified file with the given mode. +* `close()`: Closes the file. +* `read()`: Reads the entire file content into a string. +* `readline()`: Reads a single line from the file. +* `readword()`: Reads a single word from the file. +* `readchar()`: Reads a single character from the file. +* `write(const std::string& str)`: Writes a string to the file. +* `write(const char* str, std::size_t len)`: Writes a character array to the file. +* `write(const char c)`: Writes a single character to the file. +* `isopen() const`: Checks if the file is open. +* `size()`: Returns the size of the file. + +#### 1.2.3 Operators + +* `operator<<(FileStream& stream, const std::string& text)`: Writes a string to the filestream. +* `operator>>(FileStream& stream, std::string& buff)`: Reads a word from the filestream. + +### 1.3 `storage::init()` function + +Initializes the storage module (specifically for ESP32 platform). Returns `true` on success, `false` otherwise. + +## 2. `appFile` Module + +Provides higher-level file operations, including JSON parsing and saving. + +### 2.1 Functions + +* `load(std::string filename)`: Loads the content of a file into a string, handling path restrictions. +* `parse(std::string str)`: Parses a JSON string into a `nlohmann::json` object. Returns `NULL` on failure. +* `save(std::string filename, json jsonObj)`: Saves a `nlohmann::json` object to a file. + + +## 3. Notes + +* The codebase is designed to be platform-agnostic, supporting both desktop (Linux, Windows, macOS) and embedded (ESP32) systems. +* The `PATH_LOCATION` macro defines the base directory for file operations. +* The `storage::init()` function should be called before any other `storage` functions on ESP32. +* Error handling is implemented for JSON parsing and SD card initialization. File operations generally rely on the underlying system's error handling. +* The `appFile` module provides a convenient way to work with JSON files, built upon the `storage` module's functionalities. + + +This documentation provides a comprehensive overview of the `storage` and `appFile` modules, enabling developers to understand and utilize their functionalities effectively within the Paxos project. diff --git a/docs/reference/gui.md b/docs/reference/gui.md new file mode 100644 index 0000000..7618018 --- /dev/null +++ b/docs/reference/gui.md @@ -0,0 +1,527 @@ +# GUI library + +This document provides comprehensive documentation for the Paxos GUI library, detailing its core components, element types, and usage examples. + +## Core Class: ElementBase + +The `ElementBase` class serves as the foundation for all GUI elements within the Paxos library. It defines common properties and methods inherited by all other element types. + +### Members: + +- `m_x`: X-coordinate of the element's top-left corner. +- `m_y`: Y-coordinate of the element's top-left corner. +- `m_width`: Width of the element. +- `m_height`: Height of the element. +- `m_backgroundColor`: Background color of the element. +- `m_borderColor`: Border color of the element. +- `m_borderSize`: Thickness of the element's border. +- `m_borderRadius`: Radius of the element's rounded corners. +- `m_parent`: Pointer to the parent element (if any). +- `m_children`: Vector of child elements. +- `m_isEnabled`: Boolean indicating whether the element is enabled. +- `m_surface`: Pointer to the underlying graphics surface. + +### Methods: + +- `render()`: Virtual function responsible for rendering the element's visual representation. **Must be overridden by derived classes.** +- `update()`: Updates the element's state and handles user interaction. +- `setX(uint16_t x)`: Sets the X-coordinate. +- `setY(uint16_t y)`: Sets the Y-coordinate. +- `setWidth(uint16_t width)`: Sets the width. +- `setHeight(uint16_t height)`: Sets the height. +- `getX() const`: Returns the X-coordinate. +- `getY() const`: Returns the Y-coordinate. +- `getWidth() const`: Returns the width. +- `getHeight() const`: Returns the height. +- `setBackgroundColor(color_t color)`: Sets the background color. +- `setBorderColor(color_t color)`: Sets the border color. +- `setRadius(uint16_t r)`: Sets the border radius. +- `getRadius() const`: Returns the border radius. +- `setBorderSize(uint16_t size)`: Sets the border size. +- `getBorderSize() const`: Returns the border size. +- `getBackgroundColor() const`: Returns the background color. +- `getBorderColor() const`: Returns the border color. +- `isTouched()`: Returns `true` if the element was touched and released. +- `isFocused(bool forced = false)`: Returns `true` if the element is currently being touched. +- `enable()`: Enables the element. +- `disable()`: Disables the element. +- `setEnabled(bool enabled)`: Sets the enabled state. +- `isEnabled() const`: Returns the enabled state. +- `addChild(ElementBase *child)`: Adds a child element. +- `getSurface()`: Returns a pointer to the surface. +- `forceUpdate()`: Forces a graphical update. + + +### Example: + +```cpp +#include "ElementBase.hpp" + +// Create a derived class +class MyElement : public gui::ElementBase { +public: + MyElement(uint16_t x, uint16_t y, uint16_t width, uint16_t height) : ElementBase() { + setX(x); + setY(y); + setWidth(width); + setHeight(height); + setBackgroundColor(COLOR_RED); + } + + void render() override { + m_surface->fillRect(0,0, getWidth(), getHeight(), getBackgroundColor()); + } +}; + +int main() { + // Initialize graphics (implementation-specific) + // ... + + // Create an instance of MyElement + MyElement myElement(10, 10, 50, 50); + + // Add to window + gui::elements::Window win; + win.addChild(&myElement); + + while (true) + { + win.updateAll(); + } + + return 0; +} +``` + + +## GUI Elements + +### 1. Box + +A simple rectangular box element. + +#### Members: + +Inherits all members from `ElementBase`. + +#### Methods: + +- `Box(uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Renders the box. + + +#### Example: + +```cpp +#include "Box.hpp" + +gui::elements::Box myBox(10, 20, 100, 50); +myBox.setBackgroundColor(COLOR_BLUE); +myBox.setBorderColor(COLOR_BLACK); +myBox.setBorderSize(2); +myBox.setRadius(5); +``` + +### 2. Button + +A clickable button element. + +#### Members: + +- `m_label`: Pointer to the button's label. +- `m_image`: Pointer to the button's image. +- `m_theme`: Boolean representing the button theme (e.g., light or dark). + + +#### Methods: + +- `Button(uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Renders the button. +- `setText(std::string text)`: Sets the button text. +- `setIcon(storage::Path path)`: Sets the button icon. +- `getText()`: Returns the button text. +- `setTheme(bool value)`: Sets the button theme. +- `onClick() override`: Called when the button is clicked. +- `onReleased() override`: Called when the button is released + + +#### Example: + +```cpp +#include "Button.hpp" + +gui::elements::Button myButton(10, 20, 80, 30); +myButton.setText("Click Me"); + +// In the update loop: +if (myButton.isTouched()) { + // Handle button click +} +``` + +### 3. Canvas + +A drawable surface for custom graphics. + +#### Members: + +Inherits members from `ElementBase`. + +#### Methods: + +- `Canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Dummy render function (drawing is done directly on the surface). +- `setPixel(int16_t x, int16_t y, color_t color)`: Sets a pixel's color. +- `drawRect(...)`, `fillRect(...)`, `drawCircle(...)`, `fillCircle(...)`, `drawRoundRect(...)`, `fillRoundRect(...)`, `drawPolygon(...)`, `fillPolygon(...)`, `drawLine(...)`: Drawing functions. +- `drawText(...)`: Draw text with optional font size. +- `drawTextCentered(...)`: Draw centered text with optional alignment parameters. + +#### Example: + +```cpp +#include "Canvas.hpp" + +gui::elements::Canvas myCanvas(10, 20, 200, 100); +myCanvas.setPixel(50, 50, COLOR_RED); +myCanvas.drawLine(0, 0, 100, 50, COLOR_BLUE); +std::string text = "Hello"; +myCanvas.drawText(0,0, text, COLOR_BLACK); +``` + +### 4. Checkbox + +A checkbox element. + +#### Members: + +- `m_state`: Boolean indicating whether the checkbox is checked. + +#### Methods: + +- `Checkbox(uint16_t x, uint16_t y)`: Constructor. +- `render() override`: Renders the checkbox. +- `setState(bool state)`: Sets the checked state. +- `getState()`: Returns the checked state. + +#### Example: + +```cpp +#include "Checkbox.hpp" + +gui::elements::Checkbox myCheckbox(10, 20); + +// In the update loop: +if (myCheckbox.isTouched()) { + bool isChecked = myCheckbox.getState(); + // ... +} +``` + +### 5. Filter (Deprecated) + +Marked as deprecated; avoid using. + +### 6. Image + +Displays an image from storage. + +#### Members: + +- `m_path`: Path to the image file. + +#### Methods: + +- `Image(storage::Path path, uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Renders the image. +- `load()`: Loads the image from the specified path. + +#### Example: + +```cpp +#include "Image.hpp" + +gui::elements::Image myImage(storage::Path("/path/to/image.png"), 10, 20, 50, 50); +myImage.load(); +``` + +### 7. Input + +A text input field. + +#### Members: + +- `m_text`: The inputted text. +- `m_placeHolder`: Placeholder text displayed when the input is empty. + +#### Methods: + +- `Input(uint16_t x, uint16_t y)`: Constructor. +- `render() override`: Renders the input field. +- `setText(const std::string& text)`: Sets the input text. +- `setPlaceHolder(const std::string& text)`: Sets the placeholder text. +- `getText()`: Returns the input text. +- `getPlaceHolder()`: Returns the placeholder text. + + +#### Example: + +```c++ +#include "Input.hpp" + +gui::elements::Input myInput(10, 20); +myInput.setPlaceHolder("Enter text..."); + +// In the update loop: +if (myInput.isTouched()) { + std::string inputText = myInput.getText(); + // ... +} +``` + + +### 8. Keyboard + +An on-screen keyboard for text input. + +#### Members: +(Numerous members related to keyboard layout and state - see Keyboard.hpp for full list) + +#### Methods: + +- `Keyboard(const std::string &defaultText = "")`: Constructor. +- `render() override`: Renders the keyboard. +- `widgetUpdate() override`: Handles key presses and other keyboard interactions. +- `getText()`: Returns the entered text and clears the keyboard buffer. +- `hasExitKeyBeenPressed()`: Checks if the exit key has been pressed. +- `setPlaceholder(const std::string &placeholder)`: Sets placeholder text for the input field. + + +#### Example: + +```cpp +#include "Keyboard.hpp" + +gui::elements::Keyboard keyboard; + +// In the update loop: +keyboard.updateAll(); + +if (keyboard.hasExitKeyBeenPressed()) { + std::string enteredText = keyboard.getText(); + // ... +} +``` + + +### 9. Label + +Displays static text. + +#### Members: + +- `m_text`: The text to display. +- `m_textColor`: Color of the text. +- `m_fontSize`: Size of the font. + +#### Methods: + +- `Label(uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Renders the label. +- `setText(const std::string& text)`: Sets the text. +- `getText() const`: Gets the text. +- `setTextColor(color_t color)`: Sets text color. +- `setFontSize(uint16_t fontSize)`: Set font size. +- `setHorizontalAlignment(Alignement alignment)`: Sets horizontal alignment. +- `setVerticalAlignment(Alignement alignment)`: Sets vertical alignment. + + + +#### Example: + +```cpp +#include "Label.hpp" + +gui::elements::Label myLabel(10, 20, 100, 20); +myLabel.setText("Hello, world!"); +myLabel.setTextColor(COLOR_GREEN); +myLabel.setHorizontalAlignment(gui::elements::Label::CENTER); +``` + + +### 10. List (VerticalList & HorizontalList) + + +Creates scrollable lists of elements. + + +#### Members: (VerticalList) +- `m_lineSpace`: Spacing between list items. +- `m_focusedIndex`: Index of the currently focused item. + + +#### Methods: (VerticalList) + +- `VerticalList(uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Renders the list. +- `add(ElementBase* widget)`: Adds an element to the list. +- `setSpaceLine(uint16_t y)`: Sets spacing between lines. +- `onScrollUp()`: Called when scrolling up. +- `onScrollDown()`: Called when scrolling down. + +#### Methods: (HorizontalList) + +- `HorizontalList(uint16_t x, uint16_t y, uint16_t width, uint16_t height)`: Constructor. +- `render() override`: Renders the list. +- `add(ElementBase* widget)`: Adds an element to the list. +- `setSpaceLine(uint16_t y)`: Sets spacing between lines. + + +#### Example (VerticalList): + +```cpp +#include "List.hpp" +#include "Label.hpp" + + +gui::elements::VerticalList myList(10, 20, 100, 200); + +for (int i = 0; i < 20; ++i) { + gui::elements::Label* label = new gui::elements::Label(0, 0, 80, 20); + label->setText("Item " + std::to_string(i)); + myList.add(label); +} + + +// In the update loop: +myList.updateAll(); +``` + + +### 11. Radio + +A radio button element. + + +#### Members: + +- `m_state`: Boolean indicating whether the radio button is selected. + +#### Methods: + +- `Radio(uint16_t x, uint16_t y)`: Constructor. +- `render() override`: Renders the radio button. +- `setState(bool state)`: Sets the selected state. +- `getState()`: Returns the selected state. + +#### Example: + + +```cpp +#include "Radio.hpp" + +gui::elements::Radio myRadio(10, 20); + +// In the update loop: +if (myRadio.isTouched()) { + bool isSelected = myRadio.getState(); + // ... +} +``` + + + + +### 12. Switch + +A toggle switch element. + +#### Members: + +- `m_state`: Boolean indicating the switch state (on/off). + +#### Methods: + +- `Switch(uint16_t x, uint16_t y)`: Constructor. +- `render() override`: Renders the switch. +- `setState(bool state)`: Sets the switch state. +- `getState()`: Returns the switch state. + +#### Example: + +```c++ +#include "Switch.hpp" + +gui::elements::Switch mySwitch(10, 20); + +// In the update loop: +if (mySwitch.isTouched()) { + bool isOn = mySwitch.getState(); + // ... +} +``` + +### 13. Window + +A top-level window element. + +#### Members: +- `windows`: A static vector containing all created windows. + + +#### Methods: + +- `Window()`: Constructor. +- `render() override`: Renders the window. + +#### Example: + +```c++ +#include "Window.hpp" + +gui::elements::Window myWindow; + +// Add other elements to the window using myWindow.addChild(...); + +// In the main loop: +while(true) { + myWindow.updateAll(); +} + +``` + + +## GuiManager + +A singleton class providing utility functions for the GUI. + + +#### Methods: + +- `getInstance()`: Returns the singleton instance. +- `getWindow()`: Returns the main window. +- `showInfoMessage(const std::string& msg )`: Shows an info message popup. +- `showWarningMessage(const std::string& msg )`: Shows a warning message popup. +- `showErrorMessage(const std::string& msg )`: Shows an error message popup. + + + +#### Example: + +```c++ +#include "GuiManager.hpp" + +GuiManager& guiManager = GuiManager::getInstance(); +gui::elements::Window& mainWindow = guiManager.getWindow(); + +// Add some elements to mainWindow + +// ... later in the code +guiManager.showErrorMessage("Something went wrong!"); + +while(true) { + mainWindow.updateAll(); +} +``` + + +This documentation provides a basic overview of the Paxos GUI library. Refer to the individual header files (`.hpp`) for more detailed information about each class and its members. Remember to consult the graphics library documentation for specifics on color definitions and other graphics-related functions. diff --git a/docs/reference/lua.md b/docs/reference/lua.md new file mode 100644 index 0000000..453a3f0 --- /dev/null +++ b/docs/reference/lua.md @@ -0,0 +1,395 @@ +# Lua API + +This document details the Lua API available within the Paxos operating system. This API allows developers to create applications using Lua, leveraging the system's capabilities for GUI development, file system access, hardware interaction, event handling, and more. + +## Modules + +The Lua API is organized into several modules: + +* **gui:** Provides functions for creating and managing graphical user interface elements. +* **storage:** Enables access to the Paxos file system. +* **hardware:** Offers limited interaction with the device's hardware. +* **time:** Facilitates time-related operations, including scheduling events. +* **events:** Allows registering callbacks for system events like calls, messages, and battery status changes. +* **gsm:** (Paxos ESP32 only) Provides access to GSM functionalities like calls, SMS, and contacts. +* **json:** Offers JSON parsing and manipulation capabilities. + +## Modules Documentation + +### 1. `gui` Module + +This module allows creating graphical user interface elements. + +**Example:** + +```lua +local win = gui.window() -- Create a window +local box = gui.box(win, 10, 10, 100, 50) -- Create a box inside the window +gui.setWindow(win) -- Set the window active +``` + +**Available Functions:** + +* `window()`: Creates a new window. Returns a `LuaWindow` object. +* `box(parent, x, y, width, height)`: Creates a box within the `parent` widget. +* `canvas(parent, x, y, width, height)`: Creates a canvas for drawing within the `parent` widget. +* `image(parent, path, x, y, width, height, background)`: Creates an image widget. `path` can be relative to the app directory or absolute. +* `label(parent, x, y, width, height)`: Creates a text label. +* `input(parent, x, y)`: Creates a text input field. +* `button(parent, x, y, width, height)`: Creates a button. +* `verticalList(parent, x, y, width, height)`: Creates a vertically scrollable list. +* `horizontalList(parent, x, y, width, height)`: Creates a horizontally scrollable list. +* `checkbox(parent, x, y)`: Creates a checkbox widget. +* `switch(parent, x, y)`: Creates a switch widget. +* `radio(parent, x, y)`: Creates a radio button widget. +* `del(widget)`: Deletes a widget and its children. +* `setWindow(window)`: Sets the main window for the application. +* `getWindow()`: Returns the main window. +* `keyboard(placeholder, defaultText)`: Displays a virtual keyboard and returns the entered text. +* `showInfoMessage(msg)`: Displays an info message box. +* `showWarningMessage(msg)`: Displays a warning message box. +* `showErrorMessage(msg)`: Displays an error message box. + +**Widget Methods (Common to most widgets):** + +These methods are available on most `gui` objects, like `LuaBox`, `LuaCanvas`, `LuaButton`, etc. + +* `setX(x)`, `setY(y)`, `setWidth(width)`, `setHeight(height)`: Set the widget's dimensions and position. +* `getX()`, `getY()`, `getWidth()`, `getHeight()`: Get the widget's dimensions and position. +* `setBackgroundColor(color)`, `setBorderColor(color)`, `setBorderSize(size)`, `setRadius(radius)`: Set visual properties. Available color constants are listed below in the GUI Constants section. +* `enable()`, `disable()`: Enable or disable user interaction. +* `isEnabled()`: Checks if the widget is enabled. +* `isTouched()`: Checks if the widget is currently being touched. +* `onClick(function)`: Sets a callback function to be executed when the widget is clicked. +* `clear()`: Removes all child widgets. + +**Specific Widget Methods:** + +* **LuaBox:** `setRadius(radius)`: Sets the corner radius. +* **LuaCanvas:** + * `setPixel(x, y, color)`: Sets a pixel's color. + * `drawRect(x, y, w, h, color)`, `fillRect(...)`: Draws or fills a rectangle. + * `drawCircle(x, y, radius, color)`, `fillCircle(...)`: Draws or fills a circle. + * `drawRoundRect(x, y, w, h, radius, color)`, `fillRoundRect(...)`: Draws or fills a rounded rectangle. + * `drawPolygon(vertices, color)`, `fillPolygon(...)`: Draws or fills a polygon. `vertices` is a Lua table of `{x, y}` coordinate pairs. + * `drawLine(x1, y1, x2, y2, color)`: Draws a line. + * `drawText(x, y, text, color)`: Draws text. + * `drawTextCentered(x, y, text, color, horizontallyCentered, verticallyCentered)`: Draws centered text. + * `drawTextCenteredInRect(x, y, w, h, text, color, horizontallyCentered, verticallyCentered)`: Draws text centered within a rectangle. + * `getTouch()`: Returns a table containing the touch coordinates relative to the canvas. `{x, y}` + * `onTouch(function)`: Sets a callback function executed when the canvas is touched, passing the touch coordinates as a table argument. +* **LuaImage:** `setTransparentColor(color)`: Sets the transparent color for the image. +* **LuaLabel:** + * `setText(text)`, `getText()`: Sets or gets the label's text. + * `setFontSize(size)`: Sets the font size. + * `getTextWidth()`, `getTextHeight()`: Gets the dimensions of the text. + * `setVerticalAlignment(alignment)`, `setHorizontalAlignment(alignment)`: Sets the text alignment. + * `setTextColor(color)`: Sets the text color. Alignment constants are defined below. +* **LuaInput:** + * `setText(text)`, `getText()`: Sets or gets the input text. + * `setPlaceholder(text)`: Sets placeholder text. + * `setTitle(text)`: Sets the title for the input. + * `onChange(function)`: sets a callback to be called when the text changes. +* **LuaButton:** + * `setText(text)`, `getText()`: Sets or gets the button text. + * `setIcon(path)`: Sets an icon for the button. Path can be relative or absolute. + * `setTheme(theme)`: Sets the button theme (true for dark, false for light). + * `format()`: Refreshes the button's layout. +* **LuaSwitch:** + * `setState(state)`, `getState()`: Sets or gets the switch state (true/false). + * `onChange(function)`: callback called when the switch state changes. +* **LuaRadio:** `setState(state)`, `getState()`: Sets or gets the radio button state (true/false). +* **LuaCheckbox:** `setState(state)`, `getState()`: Sets or gets the checkbox state (true/false). +* **LuaVerticalList:** + * `setIndex(index)`: Sets the currently selected index. + * `setSpaceLine(line)`: Sets the spacing between list items. + * `setSelectionFocus(focus)`: Sets the selection focus (UP/CENTER). Selection constants are defined below. + * `getSelected()`: Returns the index of the selected item. + * `select(index)`: Selects an item at the given index and triggers the `onSelect` callback. + * `setSelectionColor(color)`: Sets the selection highlight color. + * `setAutoSelect(autoSelect)`: Enables/disables automatic selection on touch. + * `onSelect(function)`: Sets a callback function to be executed when an item is selected. +* **LuaHorizontalList:** + * `setSpaceLine(line)`: Sets the spacing between list items. + + + +#### GUI Constants + +* **Alignment:** `LEFT_ALIGNMENT`, `RIGHT_ALIGNMENT`, `CENTER_ALIGNMENT`, `UP_ALIGNMENT`, `DOWN_ALIGNMENT` +* **Colors:** `COLOR_DARK`, `COLOR_LIGHT`, `COLOR_SUCCESS`, `COLOR_WARNING`, `COLOR_ERROR`, `COLOR_WHITE`, `COLOR_BLACK`, `COLOR_RED`, `COLOR_GREEN`, `COLOR_BLUE`, `COLOR_YELLOW`, `COLOR_GREY`, `COLOR_MAGENTA`, `COLOR_CYAN`, `COLOR_VIOLET`, `COLOR_ORANGE`, `COLOR_PINK`, `COLOR_LIGHT_ORANGE`, `COLOR_LIGHT_GREEN`, `COLOR_LIGHT_BLUE`, `COLOR_LIGHT_GREY` +* **Selection Focus:** `SELECTION_UP`, `SELECTION_CENTER` + + +### 2. `storage` Module + +This module provides file system access. + +**Example:** + +```lua +local file = storage.file("my_file.txt", storage.WRITE) -- Open a file for writing +file:open() +file:write("Hello, Paxos!\n") +file:close() + +local file = storage.file("my_file.txt", storage.READ) -- open a file for reading +file:open() +local content = file:readAll() +print(content) +file:close() + + +local list = storage.listDir("./") +for i, item in ipairs(list) do + if storage.isDir(item) then print(item .. ": directory") + elseif storage.isFile(item) then print(item .. ": file") + end +end +``` + + + +**Available Functions:** + +* `file(filename, mode)`: Creates a `LuaStorageFile` object. `mode` can be `storage.READ`, `storage.WRITE`, or `storage.APPEND`. +* `newDir(path)`: Creates a new directory. +* `mvFile(oldpath, newpath)`, `mvDir(...)`: Renames a file or directory. +* `rmFile(path)`, `rmDir(path)`: Deletes a file or directory. +* `isDir(path)`, `isFile(path)`: Checks if a path is a directory or file. +* `listDir(path)`: Returns a table of filenames in a directory. + +**`LuaStorageFile` Methods:** + +* `open()`: Opens the file. +* `close()`: Closes the file. +* `write(text)`: Writes text to the file. +* `readChar()`: Reads a single character. +* `readLine()`: Reads a line. +* `readAll()`: Reads the entire file content. + + +### 3. `hardware` Module (If permitted) + +Provides limited hardware interaction. + +**Example:** + +```lua +hardware.flash(true) -- Enable the flash LED +``` + +**Available Functions:** + +* `flash(state)`: Controls the flash LED (if available). + +### 4. `time` Module (If permitted) + +Handles time-related tasks. + +**Example:** + +```lua +local intervalId = time.setInterval(function() print("Tick!") end, 1000) -- Print "Tick!" every second + +local timeoutId = time.setTimeout(function() print("Timeout!") end, 5000) -- Print "Timeout!" after 5 seconds + +time.removeInterval(intervalId) +time.removeTimeout(timeoutId) + + +local currentTime = time.get("h,mi,s") +print("Current Time") +for k,v in pairs(currentTime) do + print("Key: ",k , ", Value: ",v) +end + +print(time.monotonic()) +``` + + + +**Available Functions:** + +* `monotonic()`: Returns milliseconds since the application started. +* `get(format)`: Returns the current time based on given comma separated identifiers ("s": seconds, "mi": minutes, "h": hours, "d": days, "mo": months, "y": year). Returns Lua table indexed by requested identifiers. +* `setInterval(function, interval)`: Executes a function repeatedly at a given interval (in milliseconds). Returns an ID to remove the interval. +* `setTimeout(function, timeout)`: Executes a function after a given timeout (in milliseconds). Returns an ID to remove the timeout. +* `removeInterval(id)`: Cancels a repeating interval. +* `removeTimeout(id)`: Cancels a timeout. + + +### 5. `events` Module + +Registers callbacks for system events. + +**Example:** + +```lua +events.onmessage(function(msg) print("New message:", msg) end) +events.onlowbattery(function() print("Battery low!") end) +events.oncharging(function() print("Charging...") end) +events.oncall(function() print("Incoming call!") end) +events.onmessageerror(function() print("Error delivering message!") end) +``` + +**Available Functions:** + +* `onmessage(function)`: Callback for new messages (SMS). +* `onlowbattery(function)`: Callback for low battery warnings. +* `oncharging(function)`: Callback for charging status changes. +* `oncall(function)`: Callback for incoming calls. +* `onmessageerror(function)`: Callback for message delivery errors. + + +### 6. `gsm` Module (Paxos ESP32 only) + +Provides GSM functionalities. + +**Example:** + +```lua +gsm.newMessage("+33xxxxxxxxxx", "Hello from Lua!") -- Send an SMS +gsm.newCall("+33xxxxxxxxxx") -- Initiate a call +gsm.endCall() -- End a call +gsm.acceptCall() -- Accept an incoming call +gsm.rejectCall() -- Reject an incoming call + +local number = gsm.getNumber() -- retrieve current calling number +local call_state = gsm.getCallState() -- retrieve current call state + + +local messages = gsm.getMessages("+33xxxxxxxxxx") +for i,msg in pairs(messages) do + print("From " .. msg.who .. " at " .. msg.date .. ":" .. msg.message) +end + + + +-- Contact manipulation example +local contact1 = { name = "John Doe", phone = "+33123456789" } +gsm.addContact(contact1) + +local contactList = gsm.listContacts() +for i, contact in ipairs(contactList) do + print(contact.name, contact.phone) +end + +gsm.deleteContact("+33123456789") + +gsm.saveContacts() +``` + + + +**Available Functions:** + +* `newMessage(number, message)`: Sends an SMS. +* `newCall(number)`: Initiates a call. +* `endCall()`: Ends a call. +* `acceptCall()`: Accepts an incoming call. +* `rejectCall()`: Rejects an incoming call. +* `getNumber()`: Gets the calling number for the current or last call. +* `getCallState()`: Get the current call state. See GSM.h for the state integer values. +* `getMessages(number)`: Gets the messages history with a given number. +* `addContact(contact)`: Adds a new contact (`contact` is a Lua table `{name=..., phone=...}`). +* `deleteContact(phone)` : delete the given contact. +* `listContacts()`: Lists all contacts, returns a lua table of `Contact` objects. +* `saveContacts()`: saves contact list. +* `getContact(index)` : retrieve the contact at given index. +* `editContact(contact)`: Edits an existing contact. The `contact` table should contain the phone number to identify the contact to edit. +* `getContactByNumber(phone)`: Retrieves a contact by phone number. + + +### 7. `json` Module + +Provides JSON parsing and manipulation capabilities. + +**Example:** + +```lua +local jsonData = '{"name": "Paxos", "version": 1.0}' +local json_object = json.new(jsonData) + +print(json_object.name) -- Access fields like regular Lua tables +print(json_object.version) + +json_object.version = 1.1 +json_object["new_field"] = "new value" + +print(json_object:get()) -- retrieve the json string + +local noJson = json.new('{"hello') +print(noJson:get()) -- retrieve json string from a malformed string + +-- Nested JSON Example +local nestedJsonData = '{ "person": { "name": "Bob", "age": 30 } }' +local nestedJsonObject = json.new(nestedJsonData) + +print(nestedJsonObject.person.name) -- Access nested fields +print(nestedJsonObject.person.age) + +nestedJsonObject.person.age = 31 -- Modify nested fields + +local newObject = nestedJsonObject.person -- store sub-table and manipulate it as a regular Json object + +newObject.name = "alice" +print(nestedJsonObject:get()) -- print the modified json + + +local array = json.new('[{"a": 1}, {"a": 2}]') +print(array[1].a) -- 1 +print(array[2].a) -- 2 + + +``` + + + +**Available Functions:** + +* `new(data)`: Creates a new JSON object from a JSON string. + +**`LuaJson` Methods:** + +* `get()`: Returns the JSON string representation. +* `is_null()`: Returns whether the JSON object is null. +* `size()`: Returns the number of elements (for arrays and objects). +* `has_key(key)`: Checks if a key exists. +* `remove(key)`: Removes a key-value pair. +* `get_int(key)`, `get_double(key)`, `get_bool(key)`, `get_string(key)`: Get values with type checking. +* `set_int(key, value)`, `set_double(key, value)`, `set_bool(key, value)`: Set values with type checking. + + + +## Lua Specific Functions + +* `require(module)`: Loads a Lua module. The module file should be located within the app's directory. +* `saveTable(filename, table)`: Saves a Lua table into a json file. +* `loadTable(filename)`: Loads a Lua table from a json file. +* `launch(appName, args)`: Launches another Paxos application. `args` is an optional table of string arguments. + + +## Application Lifecycle Functions + +These functions are defined within your Lua application code and are called by the Paxos system during the app's lifecycle. + + +* `run(args)`: The main entry point for your application. Called when the app is launched. The `args` table contains any command-line arguments passed to the application. +* `background(args)`: Called when the application is sent to the background (if implemented). Useful for suspending tasks or releasing resources. +* `wakeup(args)`: Called when the application is brought back to the foreground (if `background` is implemented). +* `quit(args)`: Called when the application is being closed. Use this to clean up resources. + + + +## Error Handling + +Lua errors within your application can be handled with Lua's `pcall` or `xpcall` functions. Global error handling functions can be implemented by the developer using the `onmessageerror` callback or within the appropriate lifecycle functions. Lua errors will stop the application's execution. + + +## Permissions + +An application's access to certain modules and functionalities is controlled by its manifest file (a JSON file). The manifest specifies which permissions the app requests. The user grants these permissions during installation. If an app attempts to use a module or function it doesn't have permission for, an error will occur. + + +This documentation provides an overview of the Paxos Lua API. Explore and experiment with the API to build your own Paxos applications. diff --git a/mkdocs.yml b/mkdocs.yml index 13faef8..a689aa2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,2 +1,14 @@ site_name: Paxo Docs +repo_url: https://github.com/paxo-phone/paxo-mkdocs +nav: + - Home: 'index.md' + - Reference: + - Lua API: 'reference/lua.md' + - GUI library: 'reference/gui.md' + - Modules: + - Storage module: 'modules/storage.md' + - GSM module: 'modules/gsm.md' + - GUI module: 'modules/gui.md' + +edit_uri_template: 'blob/main/docs/{path}' theme: material