Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions smartlamp-kernel-module/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
obj-m += SUBISTITUA_PELO_NOME_DO_MODULO.o
obj-m += probe.o serial.o
PWD := $(CURDIR)

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Empty file.
1 change: 1 addition & 0 deletions smartlamp-kernel-module/modules.order
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/devtitans-1/Hands-On-Linux/smartlamp-kernel-module/probe.ko
6 changes: 3 additions & 3 deletions smartlamp-kernel-module/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ static uint usb_in, usb_out; // Endereços das portas de e
static char *usb_in_buffer, *usb_out_buffer; // Buffers de entrada e saída da USB
static int usb_max_size; // Tamanho máximo de uma mensagem USB

#define VENDOR_ID SUBSTITUA_PELO_VENDORID /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID SUBSTITUA_PELO_PRODUCTID /* Encontre o ProductID do smartlamp */
#define VENDOR_ID 0x10c4 /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID 0xea60 /* Encontre o ProductID do smartlamp */
static const struct usb_device_id id_table[] = { { USB_DEVICE(VENDOR_ID, PRODUCT_ID) }, {} };

static int usb_probe(struct usb_interface *ifce, const struct usb_device_id *id); // Executado quando o dispositivo é conectado na USB
Expand Down Expand Up @@ -56,4 +56,4 @@ static void usb_disconnect(struct usb_interface *interface) {
printk(KERN_INFO "SmartLamp: Dispositivo desconectado.\n");
kfree(usb_in_buffer); // Desaloca buffers
kfree(usb_out_buffer);
}
}
Binary file added smartlamp-kernel-module/probe.ko
Binary file not shown.
2 changes: 2 additions & 0 deletions smartlamp-kernel-module/probe.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/home/devtitans-1/Hands-On-Linux/smartlamp-kernel-module/probe.o

46 changes: 46 additions & 0 deletions smartlamp-kernel-module/probe.mod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <linux/module.h>
#define INCLUDE_VERMAGIC
#include <linux/build-salt.h>
#include <linux/elfnote-lto.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>

BUILD_SALT;
BUILD_LTO_INFO;

MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);

__visible struct module __this_module
__section(".gnu.linkonce.this_module") = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};

#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif

static const struct modversion_info ____versions[]
__used __section("__versions") = {
{ 0xdc658e53, "module_layout" },
{ 0xaad01b89, "usb_deregister" },
{ 0xd670cf94, "usb_register_driver" },
{ 0xd0da656b, "__stack_chk_fail" },
{ 0xeb233a45, "__kmalloc" },
{ 0x93c7edeb, "usb_find_common_endpoints" },
{ 0x5b8239ca, "__x86_return_thunk" },
{ 0x37a0cba, "kfree" },
{ 0x92997ed8, "_printk" },
{ 0xbdfb6dbb, "__fentry__" },
};

MODULE_INFO(depends, "");

MODULE_ALIAS("usb:v10C4pEA60d*dc*dsc*dp*ic*isc*ip*in*");

MODULE_INFO(srcversion, "C8D964DC7A4CC1F840A27AB");
Binary file added smartlamp-kernel-module/probe.mod.o
Binary file not shown.
Binary file added smartlamp-kernel-module/probe.o
Binary file not shown.
6 changes: 3 additions & 3 deletions smartlamp-kernel-module/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ static uint usb_in, usb_out; // Endereços das portas de e
static char *usb_in_buffer, *usb_out_buffer; // Buffers de entrada e saída da USB
static int usb_max_size; // Tamanho máximo de uma mensagem USB

#define VENDOR_ID SUBSTITUA_PELO_VENDORID /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID SUBSTITUA_PELO_PRODUCTID /* Encontre o ProductID do smartlamp */
#define VENDOR_ID 0x10c4 /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID 0xea60 /* Encontre o ProductID do smartlamp */
static const struct usb_device_id id_table[] = { { USB_DEVICE(VENDOR_ID, PRODUCT_ID) }, {} };

static int usb_probe(struct usb_interface *ifce, const struct usb_device_id *id); // Executado quando o dispositivo é conectado na USB
Expand Down Expand Up @@ -86,4 +86,4 @@ static int usb_read_serial() {
}

return -1;
}
}
44 changes: 0 additions & 44 deletions smartlamp.ino

This file was deleted.

140 changes: 140 additions & 0 deletions smartlamp/smartlamp.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Defina os pinos de LED e LDR
// Defina uma variável com valor máximo do LDR (4000)
// Defina uma variável para guardar o valor atual do LED (10)


/*
https://gist.github.com/aferreira44/6bbe8fab792e24afc6c1389da714aa99
https://embarcados.com.br/como-programar-o-esp32-na-arduino-ide/
https://kb.deec.uc.pt/books/deec/page/programa-led-a-piscar
https://portal.vidadesilicio.com.br/led-com-esp32-curso-esp32-basico/
https://portal.vidadesilicio.com.br/como-utilizar-o-led-rgb-com-arduino/
https://www.paulotrentin.com.br/programacao/dicas/lendo-uma-string-com-arduino-via-serial/
https://www.youtube.com/watch?v=GDxwDsphyuk&ab_channel=BrincandocomIdeias
https://blogmasterwalkershop.com.br/arduino/arduino-exibindo-e-lendo-dados-da-serial
https://forum.arduino.cc/t/test-if-a-string-contains-a-string/478927/4
https://www.blogdarobotica.com/2022/06/30/controlando-o-brilho-do-led-usando-potenciometro-e-arduino/
https://www.tutorialspoint.com/find-if-a-substring-exists-within-a-string-in-arduino#:~:text=In%20order%20to%20check%20if,searching%20for%20within%20another%20string.
https://forum.arduino.cc/t/duvida-com-substring/1045145
https://www.clubedohardware.com.br/forums/topic/1360900-como-utilizar-o-parseint-arduino/
sudo chmod a+rw /dev/ttyUSB0

*/


int PINO_LED = 23;
int PINO_LDR = 36;
int ledValue = 10;
int ldrMax;

void setup() {
pinMode(PINO_LED, OUTPUT);
pinMode(PINO_LDR, OUTPUT);
analogWrite(PINO_LED,ledValue);
Serial.begin(9600);
}

void loop() {
// Se receber algo pela serial
if (Serial.available() > 0) {
// Lê toda string recebida
String teclado = leStringSerial();
// chama a funcao de processar string para comando no circuito
processCommand(teclado);
}
}


void processCommand(String command) {
// Verifica se o comando passado contém SET_LED e apos o espaco seja um valor numericoó
String valorAposComando = command.substring(8, command.length());
if (command.indexOf("SET_LED ") >= 0 && isNumero(valorAposComando)) {
//caso seja verdadeiro, transforma o valor da string após o espaco
ledValue = valorAposComando.toInt();
//verifica se esse valor esta no range de 0 a 100
if (ledValue >= 0 && ledValue <= 100 ) {
// valor maior que 0 , liga led
if (ledValue > 0 ) {
ledUpdate();
Serial.println("RES SET_LED 1");
} else {
// valor igual a 0 desliga o led
ledUpdate();
Serial.println("RES SET_LED 1");
}

} else
Serial.println("RES SET_LED-1");
// caso o range passado seja invalido

} else if (command.indexOf("GET_LED") >= 0)
// retorna o valor do led atual
Serial.printf("RES GET_LED %d", ledGetValue());
else if (command.indexOf("GET_LDR") >= 0)
Serial.printf("RES GET_LDR %d", ldrGetValue());
else
Serial.println("ERR Unknown command.");

}


// Função para atualizar o valor do LED
void ledUpdate() {
// Normalize o valor do LED antes de enviar para a porta correspondente
int val = map(ledValue, 0, 1023, 0, 255);
// val = map(ledValue, 0, 1023, 0, 255);
analogWrite(PINO_LED, val);

}


// Função para ler o valor do LDR
int ldrGetValue() {
int sensorValue = analogRead(PINO_LDR);
return sensorValue;
// Leia o sensor LDR e retorne o valor normalizado

}



// Função para ler o valor do LED
int ledGetValue() {
return ledValue;
}



/**
Função que lê uma string da Serial
*/

String leStringSerial() {
String conteudo = "";
char caractere;
// Enquanto receber algo pela serial
while (Serial.available() > 0) {
// Lê byte da serial
caractere = Serial.read();
// Ignora caractere de quebra de linha
if (caractere != '\n') {
// Concatena valores
conteudo.concat(caractere);
}
// Aguarda buffer serial ler próximo caractere
delay(20);
}
return conteudo;
}



bool isNumero(String str) {

for (int i = 0; i < str.length(); i++) {
if (!isdigit(str.charAt(i)))
return false;
}
return true;

}