#!/usr/bin/perl -w # # ortho_ui.pm - User Interface for Ortho of Borg # # # package ortho_ui; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(ui_open ui_close ui_get_menu_choice ui_get_number ui_set_x ui_set_y ui_x_inches ui_y_inches ui_toggle_repeat_mode ui_repeat_mode ui_set_max_output ui_max_output); use POSIX; use strict; use ssc; my $clear_string; # user input for desired positions my $x_inches = 20; my $y_inches = 20; my $repeat_mode = 0; my $max_output = 127; sub ui_open() { $clear_string = `clear`; system("stty", "-icanon"); } sub ui_close() { system("stty", "icanon"); } sub ui_get_menu_choice() { my $char; print ("Press a key...\a"); getc(); do { # print $clear_string; print "\nORTHO OF BORG MENU\n\n"; print "\tl: Line Following\n"; print "\tq: Quick Trip\n"; print "\tt: T-Time\n"; print "\tc: Can-Can\n"; print "\ts: Square/rectangle pattern\n\n"; print "\to: set maximum output (currently $max_output)\n"; print "\tr: toggle Repeat mode (currently ", $repeat_mode ? "ON" : "OFF", ")\n"; print "\tx: set X (sideways) distance (currently $x_inches) inches\n"; print "\ty: set Y (forward) distance (currently $y_inches) inches\n\n"; print "\t+: change direction to X\n"; print "\t-: change direction to Y\n\n"; print "\t4: jog drive motor negative by X / Y inches\n"; print "\t6: jog drive motor positive by X / Y inches\n"; print "\tm: motor test menu\n\n"; print "\ti: read IR sensors\n\n"; print "\te: Exit\n\n"; print "Select an option: \a"; $char = getc(); if ($char =~ "m") { test_drive_motor(); } elsif ($char =~ "i") { read_ir_sensors(); } } until ($char =~ /[lqtcsorxy+-46e]/); print "\nYour selection: $char\n"; return $char; } sub ui_get_number($) { my $return_value = 0; my $char; print shift; for (;;) { $char = getc(); if ($char =~ /[0-9]/) { $return_value = $return_value * 10 + $char; } elsif ($char =~ /[\n]/) { print "\nNew value = $return_value\n"; return $return_value; } } } sub ui_set_x() { $x_inches = ui_get_number("Enter desired X distance in inches: "); return 1; } sub ui_set_y() { $y_inches = ui_get_number("Enter desired Y distance in inches: "); return 1; } sub ui_x_inches() { return $x_inches; } sub ui_y_inches() { return $y_inches; } sub ui_toggle_repeat_mode() { $repeat_mode = 1 - $repeat_mode; return 1; } sub ui_repeat_mode() { return $repeat_mode; } sub ui_set_max_output() { $max_output = ui_get_number("Enter maximum output (0-127): "); if ($max_output > 127) { $max_output = 127; } elsif ($max_output < 0) { $max_output = 0; } return 1; } sub ui_max_output() { return $max_output; } sub test_drive_motor() { my $current_output0 = 128; my $current_output1 = 128; my $char; my $current_motor = 0; ssc_set_servo(0, $current_output0); ssc_set_servo(1, $current_output1); do { ssc_set_servo(0, $current_output0); ssc_set_servo(1, $current_output1); print $clear_string; print "\nMOTOR TEST MENU\n\n"; print "\tCurrent motor = $current_motor\n"; print "\tCurrent outputs = $current_output0 $current_output1\n\n"; print "\td: Select Drive motor (#0)\n"; print "\tc: Select Change motor (#1)\n\n"; print "\t+: Increase output\n"; print "\t-: Decrease output\n"; print "\tf: Set output to full Forward (255)\n"; print "\tr: Set output to full Reverse (0)\n"; print "\ts: Stop motor\n\n"; print "\te: Exit\n\n"; print "Select an option: \a"; $char = getc(); if ($char =~ /d/) { $current_motor = 0; } elsif ($char =~ /c/) { $current_motor = 1; } elsif ($char =~ /\+/) { if ($current_motor == 0) { $current_output0++ } else { $current_output1++ } } elsif ($char =~ /\-/) { if ($current_motor == 0) { $current_output0-- } else { $current_output1-- } } elsif ($char =~ /f/) { if ($current_motor == 0) { $current_output0 = 255 } else { $current_output1 = 255 } } elsif ($char =~ /r/) { if ($current_motor == 0) { $current_output0 = 0 } else { $current_output1 = 0 } } elsif ($char =~ /s/) { $current_output0 = 128; $current_output1 = 128; } } until ($char =~ /e/); ssc_set_servo(0, 128); ssc_set_servo(1, 128); } sub read_ir_sensors() { my $i; my $reading; for ($i = 1; $i <= 4; $i++) { if (defined($reading = ir_read($i))) { print "IR sensor $i = $reading\n"; } else { print "UNABLE TO READ IR $i\n"; } } print "Press a key to continue..."; getc(); } return 1;