diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-07-08 10:49:56 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-07-08 10:49:56 +0100 |
commit | 9bc9955e2c8436dfee063b0c1106c6a07838a961 (patch) | |
tree | 06aad2939ee224874e7ab11ca81be933e6661176 | |
parent | 85bf60d6a9bf2694a540cc00ca6e72dccde5b26b (diff) |
Add sds.rb
-rw-r--r-- | README | 2 | ||||
-rwxr-xr-x | sds.rb | 66 |
2 files changed, 68 insertions, 0 deletions
@@ -27,6 +27,8 @@ pdfbook4.rb: Page order to make (or pdfjam command to turn a pdf file into) 4-up double-sided brochure ordered pages (to make two A6 folia per A4 page) scan.sh: Batch scanning script +sds.rb: + Read streaming data from an SDS011 section.c: Like the section command on IOS slide.rb: @@ -0,0 +1,66 @@ +#!/usr/bin/env ruby + +class Rdr + def initialize(f) + @f = f + reset + end + + def valid? + @len == 10# && @csok + end + + def read! + v = @f.read(1) + @len += 1 + + case @len + when 1 + reset if v != 0xAA + when 2 + reset if v != 0xC0 + when 3 + @pm25 = v + when 4 + @pm25 += v * 256 + when 5 + @pm10 = v + when 6 + @pm10 += v * 256 + when 7 + @id = v + when 8 + @id += v * 256 + when 9 + if checksum == v + @csok = true + else + reset + end + when 10 + reset if v != 0xAB + else + reset + end + end + + def checksum + ((@pm25 / 256) + (@pm25 % 256) + + (@pm10 / 256) + (@pm10 % 256) + + (@id / 256) + (@id % 256)) % 256 + end + + def reset + @len = 0 + @pm25 = 0 + @pm10 = 0 + @id = 0 + @csok = false + end +end + +File.open("/dev/ttyUSB0", "rb") do |f| + r = Rdr.new(f) + r.read! until r.valid? + puts "Hooray" +end |