Skip to content

Instantly share code, notes, and snippets.

View chemicstry's full-sized avatar
🦀
RIIR Task Force

Jurgis chemicstry

🦀
RIIR Task Force
  • Lithuania
View GitHub Profile
@chemicstry
chemicstry / cookieState.tsx
Last active August 27, 2023 19:53
Next.js 13 SSR useCookieState
import { getCookie, setCookie } from "cookies-next";
import { useState } from "react";
const isSsr = (): boolean => typeof window === 'undefined';
const ssrCookies = isSsr() ? require("next/headers").cookies : undefined;
export function useCookieState<T>(key: string, initialValue?: T): [T, (newVal: T) => void] {
const [item, setItem] = useState<T>(() => {
try {
if (ssrCookies) {
@chemicstry
chemicstry / libcamera_reconfigure_video.rs
Created June 5, 2023 09:18
libcamera-rs reconfigure video size
use std::{fs::OpenOptions, io::Write, process::exit, time::Duration};
use libcamera::{
camera::CameraConfigurationStatus,
camera_manager::CameraManager,
framebuffer::AsFrameBuffer,
framebuffer_allocator::{FrameBuffer, FrameBufferAllocator},
framebuffer_map::MemoryMappedFrameBuffer,
geometry::Size,
pixel_format::PixelFormat,
@chemicstry
chemicstry / bxcan.rs
Created March 21, 2023 13:12
Async bxcan
use core::mem::MaybeUninit;
use core::ops::{Deref, DerefMut};
use core::task::Poll;
use bxcan;
use embassy_cortex_m::interrupt::InterruptExt;
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
use embassy_stm32::can::{BxcanInstance, RxPin, TxPin};
use embassy_stm32::gpio::low_level::AFType;
use embassy_stm32::interrupt::Interrupt;
/// Computes bxcan BTR value from peripheral clock and target can bitrate
///
/// Based on <https://github.com/OpenCyphal-Garage/platform_specific_components/blob/master/stm32/libcanard/bxcan/src/bxcan.c>
pub fn calc_bxcan_timings(periph_clock: Hertz, can_bitrate: u32) -> Option<u32> {
const BS1_MAX: u8 = 16;
const BS2_MAX: u8 = 8;
const MAX_SAMPLE_POINT_PERMILL: u16 = 900;
let periph_clock = periph_clock.0;
use core::mem::MaybeUninit;
use core::sync::atomic::{AtomicBool, Ordering};
use rtt_target::rtt::{RttChannel, RttHeader};
use rtt_target::{ChannelMode, UpChannel};
const UP_CHANNELS: usize = 1;
const DOWN_CHANNELS: usize = 0;
const TERMINAL_CHANNEL_SIZE: usize = 512;
use core::mem::MaybeUninit;
use rtt_target::{rtt::{RttHeader, RttChannel}, set_print_channel, ChannelMode, UpChannel};
const UP_CHANNELS: usize = 1;
const DOWN_CHANNELS: usize = 0;
const TERMINAL_CHANNEL_SIZE: usize = 512;
#[repr(C)]
@chemicstry
chemicstry / channels.rs
Created June 3, 2022 13:12
futures_intrusive in embassy
use core::sync::atomic::{AtomicBool, Ordering};
use futures_intrusive::{
buffer::ArrayBuf,
channel::{
GenericChannel, GenericOneshotBroadcastChannel, GenericOneshotChannel,
GenericStateBroadcastChannel,
},
};
use lock_api::{GuardNoSend, RawMutex};
@chemicstry
chemicstry / mod.rs
Created September 23, 2020 17:49
Rust async smoltcp
use smoltcp::socket::SocketSet;
use alloc::vec;
use crate::executor::{Mutex, MutexGuard};
pub mod tcp;
pub struct SocketContext {
pub set: SocketSet<'static, 'static, 'static>,
}
@chemicstry
chemicstry / rts_camera.rs
Created August 12, 2020 13:04
Bevy RTS camera
use bevy::{
window::CursorMoved,
render::camera::Camera,
prelude::*,
};
// const KEY_PAN_SPEED: f32 = 500.0;
// const KEY_ZOOM_SPEED: f32 = 200.0;
const MOUSE_PAN_SPEED: f32 = 100.0;
// const MOUSE_ZOOM_SPEED: f32 = 1000.0;
@chemicstry
chemicstry / system.rs
Created May 5, 2020 17:03
System building in legion
/// System that polls the window events and pushes them to appropriate event channels.
///
/// This system must be active for any `GameState` to receive
/// any `StateEvent::Window` event into it's `handle_event` method.
pub fn build_events_loop_system(mut events_loop: EventsLoop) ->
Box<dyn FnOnce(&mut World, &mut Resources) -> Box<dyn Runnable>>
{
let mut events = Vec::with_capacity(128);
Box::new(|_world, _resources| {
SystemBuilder::<()>::new("EventsLoopSystem")