Skip to content

Instantly share code, notes, and snippets.

@FLamparski
FLamparski / index.html
Created Sep 4, 2014
Reactivity in vanilla JS? Yes you can!
View index.html
<!DOCTYPE html>
<html>
<head>
<title>Object.observe</title>
</head>
<body>
<h1 reactive>Title here</h1>
<p reactive>Text here</p>
<hr />
<p><b>Hey!</b> Open up the console and change the <tt>title</tt>
@FLamparski
FLamparski / simple-sds011.py
Created Jun 5, 2019
Simplest SDS-011 Python program
View simple-sds011.py
import serial
import struct
from datetime import datetime
# Change this to the right port - /dev/tty* on Linux and Mac and COM* on Windows
PORT = 'COM5'
UNPACK_PAT = '<ccHHHcc'
with serial.Serial(PORT, 9600, bytesize=8, parity='N', stopbits=1) as ser:
@FLamparski
FLamparski / pm-basic.ino
Created Jun 5, 2019
Simple ESP32/Arduino program for reading the SDS-011 output in continuous mode
View pm-basic.ino
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, -1, -1, false);
}
void loop() {
if (Serial2.available()) {
byte pm_buf[10];
Serial2.readBytes(pm_buf, 10);
@FLamparski
FLamparski / plot.py
Created Jun 5, 2019
Example programs for the SDS-011
View plot.py
import serial
import struct
from collections import deque
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.ticker import FuncFormatter
# Change this to the right port - /dev/tty* on Linux and Mac and COM* on Windows
PORT = 'COM5'
@FLamparski
FLamparski / pre-commit.fish
Last active Nov 8, 2019
Git pre-commit hook for Fish to check if you're committing Jasmine tests that contain fdescribe or fit
View pre-commit.fish
#!/usr/local/bin/fish
# should be 0 at the end of the file
set num_errors 0
# is the file a JS test?
function is_test -a file
echo $file | egrep '(src\/test\/js|\.spec\.js$)' > /dev/null
return $status
end
@FLamparski
FLamparski / pm_sensor_wifi_rec.ino
Last active Jun 22, 2019
A program for collecting data from an SDS-011 PM 2.5 sensor
View pm_sensor_wifi_rec.ino
#include <ArduinoJson.h>
#include <WiFi.h>
#include <SdsDustSensor.h>
#include <PubSubClient.h>
// v1.0 - 2019-06-16 - Initial public release of script
// v1.1 - 2019-06-17 - Turn off sensor if resetting due to lack of connectivity
// v1.2 - 2019-06-22 - Increase WiFi connection timeout
// XXX Change these to your WiFi details
View nova-sds-checksum.ino
/**
* Computes the checksum of a SDS011 message.
*
* This checksum is the low 8 bits of the sum of the data bytes.
* The sensor will not react to commands with invalid checksum.
*/
byte sds011_checksum(byte* msg, size_t msg_size) {
int sum = 0;
for (int i = 2; i < msg_size - 2; i++) {
sum += msg[i];
@FLamparski
FLamparski / README.md
Last active May 28, 2019
A JS program for playing with different, but related, search algorithms
View README.md

This is an extension of the previous gist - while that gist explored a particular question, this one tries to compare some algorithms which have very similar implementations, but different requirements and performance characteristics. It generates a random but somewhat believable geography on a plane and then tries to get from Q to W (noting that there may be more than one connection between two cities, with different costs). Each run is different.

An example result:

====================================================================================
Algorithm A*:
Expanded Q; Agenda = [(E, 308), (P, 190), (D, 289), (A, 497)]
Expanded E; Agenda = [(P, 190), (W, 412), (D, 289), (I, 512), (A, 497)]
Expanded P; Agenda = [(W, 412), (D, 289), (L, 338), (M, 385), (I, 512), (G, 420), (A, 497), (V, 528)]
@FLamparski
FLamparski / README.md
Last active May 28, 2019
A JS program that replicates the mark scheme solution to the Uniform Cost Search question on ECS629 Artificial Intelligence past exam from 2015
View README.md

If you run agendaSearch.js you should get the same output as output.txt

I reverse-engineered the optimisations made in the mark scheme solution. They were:

  1. If a cheaper path exists to a node in the agenda already, don't add the more expensive path to the agenda. If you find a cheaper path to a node than you already have in the agenda, remove the more expensive path.
  2. Don't expand nodes which were already expanded, even if you reach them through a different path. In UCS, the path to every node expanded so far should already be optimal.

The algorithm works even without those two optimisations, but the agenda size gets quite big - not an issue for a computer on such a small problem, possibly a problem for a computer on a much larger problem, definitely a problem for someone doing this in an exam.

To turn off the simplifying assumptions:

View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>foreach or for(i)</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
You can’t perform that action at this time.