-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProject 21.js
104 lines (95 loc) · 3.44 KB
/
Project 21.js
1
var b = require('bonescript');var my_string = "BeagleBone Black Evil Genius";var lcd_pin_D4 = "P8_11";var lcd_pin_D5 = "P8_12";var lcd_pin_D6 = "P8_13";var lcd_pin_D7 = "P8_14";var lcd_rs = "P8_15";var lcd_e = "P8_16";b.pinMode(lcd_pin_D4, b.OUTPUT);b.pinMode(lcd_pin_D5, b.OUTPUT);b.pinMode(lcd_pin_D6, b.OUTPUT);b.pinMode(lcd_pin_D7, b.OUTPUT);b.pinMode(lcd_rs, b.OUTPUT);b.pinMode(lcd_e, b.OUTPUT);b.digitalWrite(lcd_pin_D4, b.LOW);b.digitalWrite(lcd_pin_D5, b.LOW);b.digitalWrite(lcd_pin_D6, b.LOW);b.digitalWrite(lcd_pin_D7, b.LOW);b.digitalWrite(lcd_rs, b.LOW);b.digitalWrite(lcd_e, b.LOW);LCD_init( function () { write_string_to_lcd(my_string, function() {} ); } );function LCD_init(callback) { b.digitalWrite(lcd_e, b.LOW); var i = 0; var steps = [ function(){ setTimeout(next, 50); }, function(){ write4bits(0x03, next); }, function(){ setTimeout(next, 5); }, function(){ write4bits(0x03, next); }, function(){ setTimeout(next, 5); }, function(){ write4bits(0x03, next); }, function(){ setTimeout(next, 2); }, function(){ write4bits(0x02, next); }, function(){ write_bits_to_lcd(0x28, true, next); }, function(){ write_bits_to_lcd(0x0C, true, next); }, function(){ write_bits_to_lcd(0x06, true, next); }, function(){ write_bits_to_lcd(0x01, true, next); }, function(){ write_string_to_lcd('\x7escreen init', next ); }, function(){ setTimeout(next, 800); }, function(){ write_bits_to_lcd(0x01, true, callback); } ]; next(); function next() { i++; steps[i-1](); }}function write_bits_to_lcd(value, command_or_character, callback) { var value_left = (value >> 4); if (command_or_character === true) b.digitalWrite(lcd_rs, b.LOW); else b.digitalWrite(lcd_rs, b.HIGH); write4bits(value_left, function() { write4bits(value, callback); } );}function write4bits(value, callback) { if((value >> 3) & 0x01) b.digitalWrite(lcd_pin_D7, b.HIGH); else b.digitalWrite(lcd_pin_D7, b.LOW); if((value >> 2) & 0x01) b.digitalWrite(lcd_pin_D6, b.HIGH); else b.digitalWrite(lcd_pin_D6, b.LOW); if((value >> 1) & 0x01) b.digitalWrite(lcd_pin_D5, b.HIGH); else b.digitalWrite(lcd_pin_D5, b.LOW); if(value & 0x01) b.digitalWrite(lcd_pin_D4, b.HIGH); else b.digitalWrite(lcd_pin_D4, b.LOW); b.digitalWrite(lcd_e, b.HIGH); b.digitalWrite(lcd_e, b.LOW); callback();}function write_string_to_lcd(s, callback) { var s_length = s.length; if (s_length > 32) { write_string_to_lcd_two_lines('ERROR:', 'String too long', callback); return; } if (s_length < 17) { write_bits_to_lcd(0x80, true, function () { helper_lcd_display_string(s, callback ); } ); } else { write_string_to_lcd_two_lines(s.slice(0,16), s.slice(16, s_length), callback); }}function write_string_to_lcd_two_lines(line1, line2, callback) { write_bits_to_lcd(0x80, true, function () { helper_lcd_display_string(line1, function() {second_line();} ); } ); function second_line() { write_bits_to_lcd(0xC0, true, function () { helper_lcd_display_string(line2, callback ); } ); }}function helper_lcd_display_string(s, callback) { if (s.length === 0) { callback(); } else { write_bits_to_lcd(s.charCodeAt(0), false, function() {helper_lcd_display_string(s.slice(1), callback);} ); }}