Skip to content
Snippets Groups Projects
Select Git revision
  • 543d5f4979679c680b6f557b0dd93bec85c7eeb2
  • master default protected
2 results

charger.cpp

Blame
  • charger.cpp 6.84 KiB
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <linux/i2c-dev.h>
    #include <arpa/inet.h>
    #include <sys/ioctl.h>
    #include "MFRC522.h"
    
    #define I2C_ADDR 0x23
    #define LCD_CHR 1
    #define LCD_CMD 0
    #define LINE1 0x80
    #define LINE2 0xC0
    #define LCD_BACKLIGHT 0x08
    #define ENABLE 0b00000100
    #define BUFFER_SIZE 1024
    
    #define SERVER_IP "127.0.0.1"
    #define SERVER_PORT 12345
    
    int lcd_fd;
    int client_socket;
    int battery_percentage = 50;
    int charging = 0;
    int fire_detected = 0;
    int car_parked = 0;
    
    void lcd_init(int fd);
    void lcd_byte(int fd, int bits, int mode);
    void lcd_toggle_enable(int fd, int bits);
    void lcd_loc(int fd, int line);
    void lcd_clear(int fd);
    void typeln(int fd, const char *s);
    void *listen_to_server(void *arg);
    void *update_battery_status(void *arg);
    void *rfid_tag_listener(void *arg);
    
    int main() {
        pthread_t server_thread, battery_thread, rfid_thread;
    
        // Initialize LCD
        const char *i2c_device = "/dev/i2c-1";
        lcd_fd = open(i2c_device, O_RDWR);
        if (lcd_fd < 0) {
            perror("Failed to open I2C device");
            return 1;
        }
    
        if (ioctl(lcd_fd, I2C_SLAVE, I2C_ADDR) < 0) {
            perror("Failed to acquire bus access or talk to slave");
            close(lcd_fd);
            return 1;
        }
    
        lcd_init(lcd_fd);
    
        client_socket = socket(AF_INET, SOCK_STREAM, 0);
        if (client_socket < 0) {
            perror("Socket creation failed");
            return 1;
        }
    
        struct sockaddr_in server_address;
        server_address.sin_family = AF_INET;
        server_address.sin_port = htons(SERVER_PORT);
        inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);