Compare commits

...

4 Commits

Author SHA1 Message Date
Alexander Popov 478b5bb6e6
V web 2023-09-03 11:43:10 +03:00
Alexander Popov e67e8c4acc
c webui examples 2023-09-03 11:42:40 +03:00
Alexander Popov 2c0c22538d
python3 unixsockets 2023-09-03 11:41:52 +03:00
Alexander Popov ffd578ad39
adrduino eeprom 2023-09-03 11:41:30 +03:00
12 changed files with 354 additions and 0 deletions

View File

@ -0,0 +1,15 @@
## Память
| Тип | Чтение из программы | Запись из программы | Очистка при перезагрузке |
|--------|---------------------|---------------------|--------------------------|
| Flash | Да, PROGMEM | Можно, но сложно | Нет |
| SRAM | Да | Да | Да |
| EEPROM | Да | Да | Нет |
EEPROM представляет собой область памяти, состоящую из элементарных ячеек
с размером в один байт (как SRAM). Объём EEPROM разный у разных моделей МК:
* ATmega328 (Arduino UNO, Nano, Pro Mini): `1 кБ`
* ATmega2560 (Arduino Mega): `4 кБ`
* ATtiny85 (Digispark): `512 Б`
* ESP8266 / ESP32: `4096 Б`

View File

@ -0,0 +1,27 @@
#include <EEPROM.h>
/**
* EEPROM.write(адрес, данные) пишет данные (только byte!) по адресу
* EEPROM.update(адрес, данные) обновляет (та же запись, но лучше) байт данных,
* находящийся по адресу. Не реализована для esp8266/32!
* EEPROM.read(адрес) читает и возвращает байт данных, находящийся по адресу
* EEPROM.put(адрес, данные) записывает (по факту обновляет, update) данные любого типа
* (типа переданной переменной) по адресу
* EEPROM.get(адрес, данные) читает данные по адресу
* и сам записывает их в данные указанную переменную
* EEPROM[] библиотека позволяет работать с EEPROM памятью
* как с обычным массивом типа byte (uint8_t)
*/
void setup() {
Serial.begin(9600);
Serial.println(EEPROM.read(10)); // выведет 255
Serial.println(EEPROM.get(10)); // выведет 255
Serial.println(EEPROM[10]); // выведет 255
}
void loop() {
//
}

View File

@ -0,0 +1,50 @@
void setup() {
Serial.begin(9600);
size_t size_variable;
int example_int;
unsigned int example_uint;
signed int example_sint;
unsigned long example_ulint;
float example_float;
double example_double;
char example_char;
char * example_string;
size_variable = sizeof(example_int);
Serial.print("int bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_uint);
Serial.print("unsigned int bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_sint);
Serial.print("signed int bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_ulint);
Serial.print("unsigned long bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_float);
Serial.print("float bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_double);
Serial.print("double bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_char);
Serial.print("char bytes size: " );
Serial.println(size_variable);
size_variable = sizeof(example_string);
Serial.print("char * bytes size: " );
Serial.println(size_variable);
}
void loop() {
//
}

View File

@ -0,0 +1,2 @@
[{*.c,*.h}]
indent_size = unset

View File

@ -0,0 +1,2 @@
[{*.c,*.h}]
indent_size = unset

View File

@ -0,0 +1,2 @@
[{*.c,*.h}]
indent_size = unset

View File

@ -0,0 +1,129 @@
// Call C from JavaScript Example
#include "webui.h"
void my_function_string(webui_event_t* e) {
// JavaScript:
// webui_fn('MyID_One', 'Hello');
const char* str = webui_get_string(e);
printf("my_function_string: %s\n", str); // Hello
// Need Multiple Arguments?
//
// WebUI support only one argument. To get multiple arguments
// you can send a JSON string from JavaScript then decode it.
// Example:
//
// my_json = my_json_decoder(str);
// foo = my_json[0];
// bar = my_json[1];
}
void my_function_integer(webui_event_t* e) {
// JavaScript:
// webui_fn('MyID_Two', 123456789);
long long number = webui_get_int(e);
printf("my_function_integer: %lld\n", number); // 123456789
}
void my_function_boolean(webui_event_t* e) {
// JavaScript:
// webui_fn('MyID_Three', true);
bool status = webui_get_bool(e); // True
if(status)
printf("my_function_boolean: True\n");
else
printf("my_function_boolean: False\n");
}
void my_function_with_response(webui_event_t* e) {
// JavaScript:
// const result = webui_fn('MyID_Four', number);
long long number = webui_get_int(e);
number = number * 2;
printf("my_function_with_response: %lld\n", number);
// Send back the response to JavaScript
webui_return_int(e, number);
}
int main() {
// HTML
const char* my_html =
"<html>"
" <head>"
" <title>Call C from JavaScript Example</title>"
" <style>"
" body {"
" color: white;"
" background: #0F2027;"
" text-align: center;"
" font-size: 16px;"
" font-family: sans-serif;"
" }"
" </style>"
" </head>"
" <body>"
" <h2>WebUI - Call C from JavaScript Example</h2>"
" <p>Call C function with argument (<em>See the logs in your terminal</em>)</p>"
" <br>"
" <button onclick=\"webui_fn('MyID_One', 'Hello');\">Call my_function_string()</button>"
" <br>"
" <br>"
" <button onclick=\"webui_fn('MyID_Two', 123456789);\">Call my_function_integer()</button>"
" <br>"
" <br>"
" <button onclick=\"webui_fn('MyID_Three', true);\">Call my_function_boolean()</button>"
" <br>"
" <br>"
" <p>Call C function and wait for the response</p>"
" <br>"
" <button onclick=\"MyJS();\">Call my_function_with_response()</button>"
" <br>"
" <br>"
" <input type=\"text\" id=\"MyInputID\" value=\"2\">"
" <script>"
" function MyJS() {"
" const MyInput = document.getElementById('MyInputID');"
" const number = MyInput.value;"
" webui_fn('MyID_Four', number).then((response) => {"
" MyInput.value = response;"
" });"
" }"
" </script>"
" <script src=\"/webui.js\"></script>"
" </body>"
"</html>";
// Create a window
size_t my_window = webui_new_window();
// Bind HTML elements with C functions
webui_bind(my_window, "MyID_One", my_function_string);
webui_bind(my_window, "MyID_Two", my_function_integer);
webui_bind(my_window, "MyID_Three", my_function_boolean);
webui_bind(my_window, "MyID_Four", my_function_with_response);
// Show the window
webui_show(my_window, my_html); // webui_show_browser(my_window, my_html, Chrome);
// Wait until all windows get closed
webui_wait();
return 0;
}
#if defined(_MSC_VER)
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) {
return main();
}
#endif

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ololo</title>
<script src="/webui.js"></script>
</head>
<body>
<button onclick="abc('string');">ssf</button>
<br>
<button id="clck">Click</button>
<script type="text/javascript">
window.onload = function () {
window.resizeTo(500, 500);
}
function abc(text) {
webui_fn('MyID', text).then((response) => { console.log(response); });
}
</script>
</body>
</html>

View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include "webui.h"
void fn_one(webui_event_t *e) {
const char *str = webui_get_string(e);
printf("Data from JavaScript: %s\n", str);
webui_return_string(e, "Message from C");
}
void fn_two(webui_event_t *e) { puts("Click!"); }
int main() {
size_t my_window = webui_new_window();
webui_bind(my_window, "MyID", fn_one);
webui_bind(my_window, "clck", fn_two);
webui_show(my_window, "index.html");
webui_run(my_window, "alert('Fast!');");
webui_wait();
return 0;
}

View File

@ -0,0 +1,22 @@
import socket
import os
# Set the path for the Unix socket
socket_path = '/tmp/myapp.sock'
# Create the Unix socket client
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Connect to the server
client.connect(socket_path)
# Send a message to the server
message = 'Hello from the client!'
client.sendall(message.encode())
# Receive a response from the server
response = client.recv(1024)
print(f'Received response: {response.decode()}')
# Close the connection
client.close()

View File

@ -0,0 +1,49 @@
import socket
import sys
import os
import base64
def prep():
srv_sock = '/tmp/myapp.sock'
try:
os.unlink(srv_sock)
except:
if os.path.exists(srv_sock):
raise
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
print('Setting up server socket on %s\n' % srv_sock)
sock.bind(srv_sock)
sock.listen(1)
print('Success, listening on unix domain socket %s\n' % srv_sock)
return(sock)
except Exception as e:
print(e)
return False
def main():
sock = prep()
if not sock:
return False
while True:
connection, client_address = sock.accept()
try:
buffer = ''
rcvd_bytes = 0
while True:
data = connection.recv(1024)
rcvd_bytes = rcvd_bytes + len(data)
if not data:
print('Finished receiving data\n')
break
buffer = buffer + data.decode()
buffer = buffer[:-1]
# print(base64.b64encode(buffer.rstrip('\x00')))
print('Total bytes received bytes: %s\n' % len(buffer))
finally:
print('Closing the connection\n')
connection.close()
print('Connection closed\n')
if __name__=="__main__":
main()

View File

@ -0,0 +1,9 @@
import vweb
struct App {
vweb.Context
}
fn main() {
vweb.run(&App{}, 8080)
}