Skip to content

Instantly share code, notes, and snippets.

@tomazas
tomazas / gps_cheksum.py
Created September 27, 2015 10:44
GPS checksum validation using Python
def chksum(inp): # GPS message checksum verification
if not inp.startswith("$"): return False
if not inp[-3:].startswith("*"): return False
payload = inp[1:-3]
checksum = 0
for i in xrange(len(payload)):
checksum = checksum ^ ord(payload[i])
return ("%02X" % checksum) == inp[-2:]
# do simple test
@tomazas
tomazas / nmea_ublox.py
Created September 27, 2015 10:51
uBlox 6M GPS NMEA message parsing using Python
# Script connects to uBlox 6M GPS receiver through serial interface,
# reads received NMEA messages, parses and verifies
# them and finally prints parsed information to console (logfile).
#
# NOTE: change below configuration based on your device parameters
# configuration
port = "COM15"
baud = 9600
@tomazas
tomazas / ubx_checksum.py
Last active October 8, 2023 17:59
uBlox UBX checksum calculator
# CFG-RATE (0x06,0x08) packet (without header 0xB5,0x62)
# payload length - 6 (little endian format), update rate 200ms, cycles - 1, reference - UTC (0)
packet = [0x06,0x08, 0x06,0x00, 0x00,0xC8, 0x00,0x01, 0x00,0x00]
CK_A,CK_B = 0, 0
for i in range(len(packet)):
CK_A = CK_A + packet[i]
CK_B = CK_B + CK_A
# ensure unsigned byte range
@tomazas
tomazas / build.bat
Last active March 5, 2016 17:04
LED blinky example for STM8 using SDCC compiler
@echo off
:: configuration
SET CC=sdcc
SET CFLAGS=-mstm8
SET INCLUDEPATH=C:/st_toolset/STM8S_StdPeriph_Lib/Libraries/STM8S_StdPeriph_Driver/inc
:: mcu type defined in stm8s.h file
SET DEFINES=STM8S103
SET SOURCE=leds
SET OUTPUT_DIR=build
@tomazas
tomazas / luhn.py
Last active April 7, 2017 08:49
Luhn python implementation
# Luhn python implementation: http://www.ee.unb.ca/tervo/ee4253/luhn.shtml
def luhn(input):
sum = 0
for i,c in enumerate(input):
num = (2-(i % 2)) * int(c)
sum += int(num/10) + (num % 10)
#print sum
return ((sum % 10) == 0)
@tomazas
tomazas / arduino_bandgap_vcc.ino
Last active July 31, 2019 20:54
Example of measuring atmega VCC using the bandgap method (no external hardware or voltage divider needed).
/*
Example of measuring atmega VCC using the bandgap method (no external hardware or voltage divider needed).
Based on: https://jeelabs.org/2012/05/04/measuring-vcc-via-the-bandgap/
*/
// Arduino Nano / Atmega328 tested
static int vccRead () {
ADMUX = bit(REFS0) | 14; // use Vref=VCC and measure internal bandgap channel 1.1V
delayMicroseconds(250); // wait for bandgap to stabilize
@tomazas
tomazas / digispark_revival.ino
Created June 30, 2020 19:30
Revive Digispark ATTiny85 sketch
// Fixes the Digispark board brown out (USB re-enumeration) issue
//
// 1. Install drivers: https://github.com/digistump/DigistumpArduino/releases/download/1.6.7/Digistump.Drivers.zip
// 2. Install Digispark boards: https://raw.githubusercontent.com/digistump/arduino-boards-index/master/package_digistump_index.json
// 3. Choose Tool > Board > Digispark (Default - 16.5mhz)
// 4. Upload this sketch
// 5. Plugin the board in USB
void setup() {
// initialize the digital pin as an output.
@tomazas
tomazas / java_jar_version_checker.py
Created September 16, 2020 07:26
Java JAR compiler version checker
# script detects Java compiler version for a provided input JAR filename
# usage: python script <input.jar>
import zipfile
import sys
import os
import shutil
import struct
if len(sys.argv) < 2:
@tomazas
tomazas / python_wave_sample.py
Created January 27, 2021 10:51
Generate 440Hz sine signal-audio with Python wave library
import wave, struct, math, random
sampleRate = 44100 # samples/sec (Fs)
duration = 10 # seconds
frequency = 440 # hertz
obj = wave.open('sound.wav','w')
obj.setnchannels(1) # mono
obj.setsampwidth(2) # 16-bit
obj.setframerate(sampleRate)
# python 2 HTTP GET single-threaded server
# input: HTTP XML payload loded from payload.txt file
#
import SimpleHTTPServer
import SocketServer
hostName = "localhost"
serverPort = 8000
payload = ""