aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README2
-rwxr-xr-xsds.rb66
2 files changed, 68 insertions, 0 deletions
diff --git a/README b/README
index 7b963dc..4eb937a 100644
--- a/README
+++ b/README
@@ -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:
diff --git a/sds.rb b/sds.rb
new file mode 100755
index 0000000..c28d508
--- /dev/null
+++ b/sds.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