Skip to content

Instantly share code, notes, and snippets.

View bathoryr's full-sized avatar

Radovan Bathory bathoryr

View GitHub Profile
@bathoryr
bathoryr / gist:158a306319ef9687e3ee86c3eab99918
Last active February 2, 2023 21:45
Windows console iostream positioning manipulator std::pos(x, y)
#include <Windows.h>
#include <iostream>
namespace std
{
struct _pos
{
_pos(short x, short y) : _x(x), _y(y)
{}
template<typename Elem, typename Traits>
@bathoryr
bathoryr / loopcall.cpp
Last active November 14, 2020 17:30
Macro to simplify periodic function call by regular interval in Arduino loop()
#define CALL_LOOP(TIME, FUNC) {static unsigned long cnt_##FUNC_##TIME_counter;\
if (millis() - cnt_##FUNC_##TIME_counter > (TIME)) {\
cnt_##FUNC_##TIME_counter = millis();\
FUNC();}\
}
void TestFunc()
{
}
@bathoryr
bathoryr / func-ptr.cpp
Created January 20, 2019 09:06
Function pointer template, C++ 11
template <typename RET, typename... ARGS>
using FunctionPtr = RET(*)(ARGS...);
std::string func1(int a, int b)
{
}
int main()
{
FunctionPtr<std::string, int, int> fPtr = func1;
@bathoryr
bathoryr / SendWithACK.cpp
Last active January 20, 2019 09:01
MySensors send message with ACK and wait for confirmation message
bool magAck;
MyMessage msgBtn(PIN_BUTTON_1, V_TRIPPED);
void loop()
{
int8_t btn = sleep(0, CHANGE, 1, CHANGE, 3600000);
if (btn >= 0)
{
delay(20);
msgBtn.sensor = PIN_BUTTON_1 + btn;
@bathoryr
bathoryr / rvalue.cpp
Created November 17, 2018 20:13
Rvalue references
// How to convert lvalue to rvalue, because the compiler treats a named rvalue reference as an lvalue
// and an unnamed rvalue reference as an rvalue
// https://docs.microsoft.com/en-us/cpp/cpp/rvalue-reference-declarator-amp-amp?view=vs-2017#additional-properties-of-rvalue-references
std::vector<std::thread> thread_list;
for (int i = 0; i < 5; i++)
{
std::thread t(thread_proc, dist(engine));
// convert to rvalue explicitly
thread_list.push_back(static_cast<std::thread&&>(t));
@bathoryr
bathoryr / string_format.h
Created June 12, 2018 20:19
Format string in modern C++
#pragma once
#include <memory>
#include <iostream>
#include <string>
#include <cstdio>
template<typename ... Args>
std::wstring string_format(const std::wstring& format, Args ... args)
{
size_t size = std::swprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0'
@bathoryr
bathoryr / Enable-ICS.ps1
Last active June 25, 2024 21:22
Powershell script to enable/disable ICS
## Restart ICS when specified MAC address not present in ARP cache (DHCP not working)
#
# Schedule on system startup:
# schtasks /create /SC ONSTART /DELAY 0005:00 /TN RB\TerminalICS /RU SYSTEM /RL HIGHEST /TR "powershell -file c:\temp\Enable-ICS.ps1 >> c:\temp\enable-ics.log"
function global:Enable-ICS
{
Param (
[String]
[ValidateSet('status','enable','disable')]
@bathoryr
bathoryr / client-node.ino
Created February 3, 2017 11:54
MySensors - Get value from other node
// Client side (requesting node).
#define SERVER_NODE 99
void setup()
{
// Request for value from other node, response will be precessed by receive()
requests(CHILD_ID_LIGHT, V_PERCENTAGE, SERVER_NODE);
}
void receive(const MyMessage &message)
{