Skip to content

Instantly share code, notes, and snippets.

@mattsan
mattsan / rv32sim.exs
Created September 26, 2022 04:43
RISC-V subset simulator in Elixir
defmodule RV32Sim.Register do
def new do
<<0::32-unit(32)>>
end
def get(_, 0), do: 0
def get(registers, number) do
<<_::size(number)-unit(32), result::32, _::bytes>> = registers
result
@mattsan
mattsan / index.html
Last active May 13, 2022 02:31
抵抗器のカラーコードは虹の色
<!DOCTYPE html>
<html lang="ja">
<head>
<title>COLOR CODE</title>
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<style>
body {
display: flex;
justify-content: space-evenly;
@mattsan
mattsan / Makefile
Created November 29, 2020 03:43
C++のクラスをNIFでElixirにバインドしてみた
PREFIX = $(MIX_APP_PATH)/priv
BUILD = $(MIX_APP_PATH)/obj
NIF = $(PREFIX)/counter_nif.so
CFLAGS = -std=c++11 -fpic
LDFLAGS = -lpthread -dynamiclib -undefined dynamic_lookup
ERL_CFLAGS = -I$(ERL_EI_INCLUDE_DIR)
ERL_LDFLAGS = -L$(ERL_EI_LIBDIR) -lei
@mattsan
mattsan / ec2_stat
Created August 22, 2020 09:25
AWS EC2 インスタンスの状態を調べる
#!/usr/bin/env ruby
require 'aws-sdk-ec2'
require 'thor'
class Ec2Stat < Thor
default_command :stat
desc :stat, 'show statuses of ec2 instances'
option :profile, aliases: '-p', default: 'default'
@mattsan
mattsan / events
Created August 22, 2020 09:24
AWS Events の内容を一覧する
#!/bin/sh
if [ "$1" = "production" ]; then
STAGE=production
elif [ "$1" = "staging" ] || [ "$1" = "" ]; then
STAGE=staging
else
cat <<EOS
usage:
@mattsan
mattsan / harndler.rb
Created March 14, 2020 12:09
Ruby で記述した AWS Lambda function を使って idobata に定期的にメッセージを post するサンプル
require 'json'
require 'net/https'
def hello(event:, context:)
uri = URI.parse(ENV['IDOBATA_HOOK_URL'])
source = event['source']
Net::HTTP.post_form(uri, {source: source})
end
@mattsan
mattsan / ads7846.ex
Created July 27, 2019 09:09
ADS7846 Touchscreen 用 Scenic.ViewPort.Driver の実装
defmodule ADS7846 do
use Scenic.ViewPort.Driver
alias Scenic.ViewPort
require Logger
defguard is_pos(x, y) when is_number(x) and is_number(y)
@init_retry_ms 400
@device "ADS7846 Touchscreen"
@mattsan
mattsan / serverless.yml
Created July 24, 2019 08:18
Serverless で layers に gems を配置する
service: emattsan
provider:
name: aws
runtime: ruby2.5
stage: ${opt:stage, 'development'}
region: ap-northeast-1
package:
exclude:
@mattsan
mattsan / Gemfile
Created July 6, 2019 22:09
A sample of Sinatra + Slim
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'sinatra'
gem 'sinatra-contrib'
gem 'slim'
@mattsan
mattsan / bits.exs
Created June 9, 2019 12:45
Elixir の Stream.unfold/2 を使って整数をビット列に分解する例
defmodule Bits do
require Integer
import Integer
import Bitwise
@doc """
A integer value to a bit list.
## example