Skip to content

Instantly share code, notes, and snippets.

View platisd's full-sized avatar
:octocat:
(づ。◕‿‿◕。)づ

Dimitris Platis platisd

:octocat:
(づ。◕‿‿◕。)づ
View GitHub Profile
@platisd
platisd / main.cpp
Last active March 16, 2024 11:30
Pointer frenzy
#include <iostream>
void getPointer1(int* p) {
*p = 10; // Change the "thing" the pointer `p` is pointing to
}
void getPointer2(int** p) {
*p = new int(20); // Replace pointer `p` with different pointer to another location
}
@platisd
platisd / openai_pizza_chatbot.py
Last active December 5, 2023 00:18
Code snippets for my OpenAI API workshop
import os
import openai
import json
import sys
def place_order(name, size, take_away=False):
place = "take away" if take_away else "eat in"
print(f"Placing order for {name} pizza, {size} to {place}")
@platisd
platisd / ..Dynamic-Memory-And-Safety-Critical-Apps.md
Last active April 25, 2023 19:11
Dynamic memory and safety critical apps

Dynamic memory and safety critical apps

This material is to be used exclusively by the participants of the grcpp meetup during the workshop on dynamic memory and safety critical apps.

All rights reserved.

How to compile and run

  • Clone the gist as dynamic-memory-grcpp
  • git clone https://gist.github.com/537c7e6874aa801b06ed329e93afb7b3.git dynamic-memory-grcpp

You're given a DataHeader struct that represents a package comprised of two sets of bytes, a DataView that can be seen as a non-owning container of some data and a copyViewData function that copies the contents of a DataView to a buffer.

You also get a validateHeader function which determines whether a DataHeader is valid. Depending on the product variant, the function has different implementations but the signature always remains the same.

Your goal is to create a parseData function which receives two DataView objects, copies them into a DataHeader object and validates the DataHeader object. The DataView objects may be empty, which depending on the project can be fine. The validateHeader function will simply return false if the given header is not as expected.

Assuming no other constraints, which of the two parseData versions would you prefer and why?

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
if (Serial.available()) {
const auto number = static_cast<unsigned long>(Serial.readStringUntil('\n').toInt());
// Don't use pow, it returns a double with smaller range
#include <memory>
struct Foo {
};
struct Bar {
Bar(Foo *foo) : mFooPtr{foo} {}
Bar(std::unique_ptr<Foo> foo) : mFooSmart{std::move(foo)};
@platisd
platisd / Debounce.ino
Last active July 24, 2020 11:12
Debounce with interrupt
const int kButton = 2; // HIGH when pressed
const unsigned long kInterval = 200; // milliseconds
volatile bool buttonPressed = false;
void buttonHandler() {
static unsigned long previousButtonPress = 0;
const auto currentTime = millis();
if (currentTime + kInterval > previousButtonPress){
previousButtonPress = currentTime;
buttonPressed = true;
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
std::optional<std::string> readFile(const std::string& filePath)
{
std::ifstream fileToRead(filePath.c_str());
if (fileToRead)
#include <Smartcar.h>
ArduinoRuntime arduinoRuntime;
BrushedMotor leftMotor(arduinoRuntime, 13, 12, A6 /* Invalid enable pin */);
BrushedMotor rightMotor(arduinoRuntime, 11, 10, A6 /* Invalid enable pin */);
DifferentialControl control(leftMotor, rightMotor);
SimpleCar car(control);
SR04 sensor(arduinoRuntime, 8, 9);
#include <array>
#include <iostream>
int main()
{
std::array<int, 4> ar{1, 2, 3, 4};
ar.at(0) = 124123;
for (auto i : ar)
{