Skip to content

Instantly share code, notes, and snippets.

View bieli's full-sized avatar

Marcin Bielak bieli

View GitHub Profile
#include <stdio.h>
#include <stdint.h>
// Philips Sonicare NFC Head Password calculation by @atc1441 Video manual: https://www.youtube.com/watch?v=EPytrn8i8sc
uint16_t CRC16(uint16_t crc, uint8_t *buffer, int len) // Default CRC16 Algo
{
while(len--)
{
crc ^= *buffer++ << 8;
int bits = 0;
do
@darka
darka / png_out.py
Last active August 31, 2024 01:01
Generating a PNG in Python
import struct
import zlib
from typing import BinaryIO, List, Tuple
Pixel = Tuple[int, int, int]
Image = List[List[Pixel]]
BLACK_PIXEL: Pixel = (0, 0, 0)
WHITE_PIXEL: Pixel = (255, 255, 255)
@msloth
msloth / client-lighting-example.c
Last active December 6, 2018 19:49
When light is on, also turns on a GPIO (DIO7). Light output via PWM on DIO5. Has a default schedule (see code).
#include "thsq.h"
#include "ti-lib.h"
#include "gpio-interrupt.h"
#include "lib/sensors.h"
#include "batmon-sensor.h"
#include "dev/leds-arch.h"
#include "dev/cc26xx-uart.h"
/*---------------------------------------------------------------------------*/
#include "thsq.h"
#include "ti-lib.h"
/*---------------------------------------------------------------------------*/
/*
* Thingsquare lighting switch with slider.
*
* This client uses a linear potentiometer to set the dim level of the lights
* in the network. To save power, we only power the potentiometer when we will
* sample it, and we only send light control if it has moved enough.
*
@nimasmi
nimasmi / import_export_views.py
Last active November 4, 2022 16:28
Django Import Export admin functionality in a Class Based View
from django.views.generic import FormView
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from import_export.formats import base_formats
from import_export.forms import ImportForm, ConfirmImportForm
from import_export.resources import modelresource_factory
from django.http import HttpResponseRedirect
from import_export.tmp_storages import TempFolderStorage
try:
@sgillies
sgillies / benchmark.py
Last active February 7, 2019 01:02
GeoJSON: json vs msgpack vs shapely.wkb vs geobuf
# Comparing serialization of complex GeoJSON geometries using:
#
# - standard lib json, marshal, pickle, cPickle
# - umsgpack
# - shapely.wkb
# - geobuf (protobuf)
#
# The test case is a nearly circular polygon with 128 vertices.
#
# Python 2.7 because geobuf isn't possible on Python 3 (because
@WayneSan
WayneSan / authentication.py
Last active January 7, 2024 20:34
PyJWT + Django REST framework 2
import jwt
from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import exceptions
from rest_framework.authentication import TokenAuthentication
class JSONWebTokenAuthentication(TokenAuthentication):
@pathikrit
pathikrit / SudokuSolver.scala
Last active April 12, 2024 15:00
Sudoku Solver in Scala
val n = 9
val s = Math.sqrt(n).toInt
type Board = IndexedSeq[IndexedSeq[Int]]
def solve(board: Board, cell: Int = 0): Option[Board] = (cell%n, cell/n) match {
case (r, `n`) => Some(board)
case (r, c) if board(r)(c) > 0 => solve(board, cell + 1)
case (r, c) =>
def guess(x: Int) = solve(board.updated(r, board(r).updated(c, x)), cell + 1)
val used = board.indices.flatMap(i => Seq(board(r)(i), board(i)(c), board(s*(r/s) + i/s)(s*(c/s) + i%s)))
@delimitry
delimitry / compiled_file_python_version.py
Last active May 20, 2024 04:22
Get the version of Python by which the file was compiled
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A script to get the version of Python by which the file was compiled
"""
from __future__ import print_function
import binascii
import os
@neilalbrock
neilalbrock / couchbase_sessions.py
Last active December 11, 2018 10:39
Flask Server-side Sessions with Couchbase. Adapted from http://flask.pocoo.org/snippets/75/
# -*- coding: utf-8 -*-
from uuid import uuid4
from datetime import timedelta
from couchbase import Couchbase
from couchbase import FMT_PICKLE
from couchbase.exceptions import NotFoundError