Hardware hackers rejoice!

Brian Fahrlander brian at fahrlander.net
Tue May 29 16:20:57 UTC 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Derek Broughton wrote:
> Brian Fahrlander wrote:
> 
>>     Funny, how easy it is to learn a 'new' language like Perl, when you
>> actually have a purpose for it, ya know?  I've tried to warm up to Perl
>> for years, and I kept forgetting _everything_ I ever learned.  Now, it
>> feels like I've been in it for years (until you ask about hashes...)
> 
> LOL.  I know I made a rude comment about Perl, but I _have_ been forced to
> use it in the last couple of weeks, and it really isn't _quite_ as bad as I
> implied :-)  Of course, it's much easier now that we have Google.  Last
> time I tried, I could never find a reference for what I wanted when I
> needed it.  Last week I googled "perl iterate associative array" and got
> exactly the code I wanted on the first hit (OK, it helped to know that what
> I wanted to do was iterate over an associative array).

    Well, so far I've been able to learn the ins-and-outs of Perl in a
more simple way...a way that doesn't involve a lot of complications, and
it's working well.

    Here's what I've got so far...this code will evolve a great deal
before it's done...if it ever is. It runs, displaying the conditions,
and if I kill it, it gives a nice little message, and behaves itself
quite well:

#!/usr/bin/perl

# Graceful cleanup signal
$SIG{'KILL'} = 'endprogram';
$SIG{'INT' } = 'endprogram';
$SIG{'QUIT'} = 'endprogram';
$SIG{'HUP' } = 'endprogram';
$SIG{'TRAP'} = 'endprogram';
$SIG{'ABRT'} = 'endprogram';
$SIG{'STOP'} = 'endprogram';

# Defining the devices is useful
# Maybe one day it'll auto-seek. Not that necessary, now
$DEV_EQ_FAN=   "/var/1wire/1F.38C704000000/main/05.880032000000/";
$DEV_EQ_TEMP=
"/var/1wire/1F.1DBC04000000/main/26.AFBBA8000000/temperature";
$DEV_HUMID_INT="/var/1wire/1F.1DBC04000000/main/26.AFBBA8000000/HTM1735/humidity";
$DEV_LCD=      "/var/1wire/1F.B9C604000000/main/29.128703000000/";
$DEV_SOLAR=    "/var/1wire/1F.1DBC04000000/main/26.AFBBA8000000/vis";
$DEV_VOLTS12=  "/var/1wire/1F.1DBC04000000/main/20.E74009000000/volt.B";

################################################################
# Retrieve variables from files, sensor data from devices
################################################################
sub getvar {
  open(FILE, $_[0]);
  $filevalue = <FILE>;
  close(FILE);
  $filevalue = $filevalue + 0;
}

################################################################
# Put variables into files (file, value)
################################################################
sub putvar {
  open(WFILE, ">$_[0]");
  print WFILE $_[1];
  close(WFILE);
}

################################################################
# Cycle the equipment-room fan
################################################################
sub vent_fan {
  # Given 0, the fan is off. 1, the fan is on.
  putvar($DEV_EQ_FAN . "/PIO",$_[0]);
}

################################################################
# Clearing the LCD
################################################################
sub clear_lcd {
  putvar($DEV_LCD ."/LCD_H/clear","1");
}

################################################################
# Gather data
################################################################
sub poll () {

  # Various setpoints (not sensors), high and low

  # Interior temp range
  $comfort_high =
&getvar("/var/run/countermoon/climate/interior/comfort_high");
  $comfort_low  =
&getvar("/var/run/countermoon/climate/interior/comfort_low");

  # "Comm Closet" maximum temp before venting
  $equip_max_temp = &getvar("/var/run/countermoon/climate/closet/temp_max");


  # Polling sensors

  # "Comm Closet" equipment temp
  $eq_temp = &getvar($DEV_EQ_TEMP);

  # Interior ambient relative humidity
  $humidity = &getvar($DEV_HUMID_INT);
  $solar = &getvar($DEV_SOLAR);
  $volts12 = &getvar($DEV_VOLTS12)/ .0854;

}

################################################################
# Display to LCD
################################################################
sub display {
  # Four entries in the array we're passed are going to the
  # LCD, but we have a special order for them.

  # Hardware quirk: must present first line, third line, second line,
and then fourth line
  # to make it look proper.  All lines are 20 characters long, padded here

  # Assemble string
  $message=sprintf("%-20s", substr($line[1],0,20)) .
           sprintf("%-20s", substr($line[3],0,20)) .
           sprintf("%-20s", substr($line[2],0,20)) .
           sprintf("%-20s", substr($line[4],0,20));

  putvar($DEV_LCD . "/LCD_H/screen", $message);
}

################################################################
# How we start
################################################################
clear_lcd;
  $line[1]="  System starting";
  $line[2]=" ";
  $line[3]=" ";
  $line[4]="   Please Stand By...";
  display( @line);

# Turn off fans
vent_fan(0);

# Delay a tad
sleep 1;

################################################################
# How we exit
################################################################
sub endprogram {
  # Clear the screen
  clear_lcd;

  vent_fan(0);

  # Print a little "we're done".
  $line[1]="  Polling Ended.";
  $line[2]="";
  $line[3]="";
  $line[4]="";

  display( @line);

  # Boogie
  exit(1);
}

################################################################
# Main Loop
################################################################
clear_lcd;

while (1) {

  # Get the sensor values
  poll();

  # Calculations and status-setting

  # Power Management is first
  if ($volts12 > 12.0) {
    # We're probably docked: green
  }

  # Is equipment room hot?
  if ($eq_temp > $equip_max_temp) {
  vent_fan(1);
  } else {
  vent_fan(0);
}

  # Set up the lines to display
  $line[1]=sprintf("Equip Room:   %3.2fF", $eq_temp);
  $line[2]=sprintf("Humidity:     %3.2f%%", $humidity);
  $line[3]=sprintf("Solar:        %-1.4f", $solar);
  $line[4]=sprintf("12V supply:  %3.3fV", $volts12);

  # Do it
  display ( @line);

  # Delay an arbitrary time and start again
  sleep (3);
}

- --
 ------------------------------------------------------------------------
 Brian Fahrländer                 Christian, Conservative, and Technomad
 Evansville, IN                              http://Fahrlander.net/brian
 ICQ: 5119262                         AOL/Yahoo/GoogleTalk: WheelDweller
 ------------------------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGXFLp6PLtRzZbdhYRAo65AJ96l2hCVEDLHTtdmCRZFsahDJshSgCcClOX
tJldIQbcAlzMSnC8fh29aiQ=
=zV1I
-----END PGP SIGNATURE-----




More information about the ubuntu-users mailing list