Skip to content

Instantly share code, notes, and snippets.

View Gedweb's full-sized avatar
🕊️

Andrey Gridchin Gedweb

🕊️
View GitHub Profile
@Gedweb
Gedweb / main.cpp
Created June 18, 2024 13:55
ADXL345 over SPI with Arduino (ESP32)
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
// ADXL345 Register Addresses
#define BW_RATE 0x2C // Data rate and power mode control
#define POWER_CTL 0x2D // Power Control Register
#define DATA_FORMAT 0x31 // Data format control
#define DATAX0 0x32 // X-Axis Data 0
@Gedweb
Gedweb / resolve_host_to_ip.php
Created June 1, 2023 19:39
Replace URI host to resolved IP address from DNS A record
<?php
function replaceHostToIp(string $dsn): string
{
$url = parse_url($dsn);
$recordList = dns_get_record($url['host'], DNS_A) ?: [];
$ipAddr = array_reduce($recordList, static fn ($carry, $record) => $carry ?: $record['ip'] ?? null);
if (null === $ipAddr) {
return $dsn;
@Gedweb
Gedweb / autoexec.be
Last active April 6, 2023 21:17
Tasmota chandelier with additional power supply and controlling by switcher
# Power1 internal power supply
# Power2 overall "virtual" pin for toggling
# Power3-5 PWM chanlles
def forward2all(value, trigger)
for i: 3..6
tasmota.cmd("Power" + str(i) + " " + str(value))
end
end
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
#define CONFIG_ESP32_APPTRACE_ENABLE true
#include "secrets.h"
#include <Arduino.h>
#include <WiFi.h>
#include <MQTTClient.h>
#include "dto.h"
@Gedweb
Gedweb / qsort_ffi.rs
Last active August 13, 2019 19:55
Rust quick sort from C through FFI
use std::mem;
fn main() {
let mut num: Vec<isize> = vec![5, 12, 4, 15, 7, -1, 0, 6, 4, 3, -3, 16];
unsafe {
qsort(
num.as_mut_ptr() as *mut libc::c_void,
num.len(),
mem::size_of::<isize>(),
@Gedweb
Gedweb / var_export_pp.php
Last active June 28, 2019 08:45
This method do same the var_export function, but with pretty print
<?php
class ExportToArray
{
private const IDENT_SPACES = 4;
public static function exportPhp(array $data, $depth = 1): string
{
$i = 0;
$result = [];
<?php
function parseCSV(string $filePath, array $head)
{
$resource = fopen($filePath, 'rb');
if ($resource === false) {
throw new \RuntimeException('Unable to read CSV file');
}
<?php
function generateUUIDv4(): string
{
$uuid = unpack('h8time_low/S3octet/h12node', random_bytes(16));
return sprintf(
"%'08s-%'04x-%'04x-%'04x-%'012s",
$uuid['time_low'],
$uuid['octet1'],