#!/usr/bin/env ruby require "al_worker" require "al_worker_timer" # 追加 require "i2c" ADRS = 0x5c # LPS25H NODE = "/dev/i2c-1" # I2C device node def to_int16( b1, b2 ) return (b1 << 8 | b2) - ((b1 & 0x80) << 9) end def to_uint24( b1, b2, b3 ) return (b1 << 16) | (b2 << 8) | b3 end class Worker1 < AlWorker def initialize2() @i2c = I2C.create(NODE) @i2c.write(ADRS, 0x20, 0x90) @timer = Timer.periodic( 60, 1 ) # 追加 @timer.run() { d = get_data() send_data( d ) } end def get_data() ret = {} buf = @i2c.read(ADRS, 5, 0xa8) p_cnt = to_uint24(buf.getbyte(2), buf.getbyte(1), buf.getbyte(0)) ret[:pressure] = p_cnt.to_f / 4096 t_cnt = to_int16(buf.getbyte(4), buf.getbyte(3)) ret[:temperature] = 42.5 + t_cnt.to_f / 480 return ret end def send_data( data ) # 追加 AlWorker.log( data ) end end worker1 = Worker1.new() worker1.daemon() # 雛形どおりに戻す