Skip to content

Instantly share code, notes, and snippets.

View h4cc's full-sized avatar

Julius Beckmann h4cc

  • ZENNER IoT Solutions
  • Hamburg, Germany
View GitHub Profile
@h4cc
h4cc / Makefile
Created April 25, 2022 12:48
Ubuntu 22.04 Jammy Jellyfish - Basic packages i usually install
#
# Ubuntu 22.04 (Jammy Jellyfish)
#
# Basic packages i usually install.
#
# Author: Julius Beckmann <github@h4cc.de>
#
# Upgraded Script from 18.04: https://gist.github.com/h4cc/c54d3944cb555f32ffdf25a5fa1f2602
# Upgraded Script from 17.04: https://gist.github.com/h4cc/09b7fe843bb737c8039ac62d831f244e
defmodule MyBinaryParser do
# Parsing a 2 byte binary to float16
# according to: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
# Float16 is build in OTP 24.
def parse_float16(<<sign::1, exponent::5, significand::10>>) do
case {sign, exponent, significand} do
{0b0, 0b00000, 0b0000000000} ->
@h4cc
h4cc / ecto_paginate_query.ex
Created February 6, 2019 09:31
Elixir Ecto paginate Query with callback
defmodule MyEcto do
# Helper to paginate a query
def paginate_query(%Ecto.Query{} = query, per_page, current_page, callback) when per_page > 0 and current_page >= 0 and is_function(callback) do
import Ecto.Query
offset = per_page * current_page
query
|> limit(^per_page)
|> offset(^offset)
|> LongRepo.all()
@h4cc
h4cc / helper.ex
Created April 11, 2018 12:18
Helper function for put_in for populating data in deep maps.
defmodule Helper do
# Helper function for setting values deep in a map.
# Thanks @michalmuskala!
# https://elixirforum.com/t/put-update-deep-inside-nested-maps-and-auto-create-intermediate-keys/7993/8
@doc """
Will set value at for keys deep inside data.
iex> Helper.put_in_deep(%{a: %{}}, [:a, :b, :c], 42)
@h4cc
h4cc / Makefile
Last active July 11, 2021 10:32
Ubuntu 18.04 Bionic Beaver - Basic packages i usually install
#
# Ubuntu 18.04 (Bionic Beaver)
#
# Basic packages i usually install.
#
# Author: Julius Beckmann <github@h4cc.de>
#
# Upgraded Script from 17.04: https://gist.github.com/h4cc/09b7fe843bb737c8039ac62d831f244e
# Upgraded Script from 16.04: https://gist.github.com/h4cc/fe48ed9d85bfff3008704919062f5c9b
defmodule InlineLookup do
# Examples how to handle a static lookup table in Elixir.
def example1(x) do
%{1 => "a", 2 => "b", 3 => "c"}[x]
# Could also be a attribute
# Should be the same as:
Map.get(%{1 => "a", 2 => "b", 3 => "c"}, x, nil)
@h4cc
h4cc / ambipi.txt
Created December 21, 2017 21:09
ambipi.txt
AmbiPI:/ # cat /storage/hyperion-black-screen.sh
#!/bin/bash
if [ `service hyperion status | grep running | wc -l` = 1 ]
then
hyperion-remote -c black
sleep 3
sudo service hyperion stop
else
sudo service hyperion start
fi
@h4cc
h4cc / update_ubuntu_lts.sh
Created November 25, 2017 11:07
Updating a Ubuntu LTS version to the next available. Used for Noez.de vServer with a broken locale setting, LOL.
# Updating a Ubuntu 14.04 LTS to 16.04 LTS
# by Julius Beckmann
# Prepare and Fix Locales.
apt-get update; apt-get install -y dialog language-pack-en-base language-pack-de-base; update-locale LANG=de_DE.UTF-8; dpkg-reconfigure locales; apt-get upgrade -y ; apt-get install -y update-manager-core
# Perform Dist Upgrade if presets ok
cat /etc/update-manager/release-upgrades | grep Prompt=lts && do-release-upgrade --check-dist-upgrade-only && do-release-upgrade -m server
# Answer questions, mostly with yes "y"
@h4cc
h4cc / struct_matching.exs
Created July 28, 2017 07:34
Example how matching on elixir structs will make code more robust and refactorable with less possible errors at runtime.
# This is our struct with some fields.
defmodule User do
defstruct name: "julius", role: :user
end
defmodule UserManager do
# Matching on %User{} will ensure we will get a user.
@h4cc
h4cc / install_tl_wn722n.sh
Created July 3, 2017 08:53
Installing a TP-Link TL-WN722N V2 on Ubuntu 16.04 with Kernel 4.4
# Source: https://askubuntu.com/questions/912498/tl-wn722n-is-not-recognized
sudo apt-get install git dkms git make build-essential
cd /usr/src
sudo git clone https://github.com/lwfinger/rtl8188eu.git
sudo dkms add ./rtl8188eu
sudo dkms build 8188eu/1.0
sudo dkms install 8188eu/1.0
sudo modprobe 8188eu