Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Lcd #32

Closed
wants to merge 2 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 25, 2013
  1. @supherman
  2. @supherman

    Add the LCD component

    supherman authored
This page is out of date. Refresh to see the latest.
View
BIN  examples/lcd/lcd.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
12 examples/lcd/lcd.rb
@@ -0,0 +1,12 @@
+#
+# This example writes "Hello World!" in the display
+#
+require 'bundler/setup'
+require 'dino'
+
+board = Dino::Board.new(Dino::TxRx.new)
+lcd = Dino::Components::LCD.new(board: board)
+
+lcd.begin(16,2)
+lcd.puts("Hello World!")
+sleep
View
1  lib/dino/components.rb
@@ -8,5 +8,6 @@ module Components
autoload :Servo, 'dino/components/servo'
autoload :Stepper, 'dino/components/stepper'
autoload :IrReceiver, 'dino/components/ir_receiver'
+ autoload :LCD, 'dino/components/lcd'
end
end
View
100 lib/dino/components/lcd.rb
@@ -0,0 +1,100 @@
+module Dino
+ module Components
+ class LCD < BaseComponent
+
+ # Overwriting initialize, we don't need to specify pins this time
+ def initialize(options={})
+ self.board = options[:board]
+ raise 'board is required for a display' if self.board.nil?
+ after_initialize(options)
+ end
+
+ def begin(cols, rows)
+ cols = normalize(cols, 2)
+ rows = normalize(rows, 2)
+ board.write("0501#{cols}#{rows}")
+ sleep 2
+ end
+
+ def clear
+ board.write('0502')
+ end
+
+ def home
+ board.write('0503')
+ end
+
+ def set_cursor(col, row)
+ col = normalize(col, 2)
+ row = normalize(row, 2)
+ board.write("0504#{col}#{row}")
+ end
+
+ def write(value)
+ value = normalize(value, 3)
+ board.write("0505#{value}")
+ end
+
+ def puts(string)
+ string.bytes.each do |byte|
+ write(byte)
+ end
+ end
+
+ def show_cursor
+ board.write('0506')
+ end
+
+ def hide_cursor
+ board.write('0507')
+ end
+
+ def blink
+ board.write('0508')
+ end
+
+ def no_blink
+ board.write('0509')
+ end
+
+ def on
+ board.write('0510')
+ end
+
+ def off
+ board.write('0511')
+ end
+
+ def scroll_left
+ board.write('0512')
+ end
+
+ def scroll_right
+ board.write('0513')
+ end
+
+ def enable_autoscroll
+ board.write('0514')
+ end
+
+ def disable_autoscroll
+ board.write('0515')
+ end
+
+ def left_to_right
+ board.write('0516')
+ end
+
+ def right_to_left
+ board.write('0517')
+ end
+
+ private
+
+ def normalize(value, spaces)
+ value.to_s.rjust(spaces, '0')
+ end
+ end
+ end
+end
+
View
146 spec/lib/components/lcd_spec.rb
@@ -0,0 +1,146 @@
+require 'spec_helper'
+
+module Dino
+ module Components
+ describe LCD do
+ let(:board) { mock(:board, digital_write: true, set_pin_mode: true) }
+ subject { LCD.new board: board }
+
+ describe '#initialize' do
+ it 'should raise if it does not receive a board' do
+ expect {
+ LCD.new()
+ }.to raise_exception
+ end
+ end
+
+ describe '#begin' do
+ it 'should initialize the display sending the command 05011602 to the board' do
+ board.should_receive(:write).with '05011602'
+ subject.should_receive(:sleep).with 2
+ subject.begin(16,2)
+ end
+ end
+
+ describe '#clear' do
+ it 'clears the display sending the command 0502 to the board' do
+ board.should_receive(:write).with '0502'
+ subject.clear
+ end
+ end
+
+ describe '#home' do
+ it 'Moves the cursor to the first position with the command 0503' do
+ board.should_receive(:write).with '0503'
+ subject.home
+ end
+ end
+
+ describe '#set_cursor' do
+ it 'moves the cursor to the given position sending the command 0504' do
+ board.should_receive(:write).with '05040001'
+ subject.set_cursor(0,1)
+ end
+ end
+
+ describe '#write' do
+ it 'writes a char with its byte value with the command 0505' do
+ board.should_receive(:write).with '0505065'
+ subject.write(65)
+ end
+ end
+
+ describe '#puts' do
+ it 'prints a string in the display' do
+ board.should_receive(:write).with '0505065'
+ board.should_receive(:write).with '0505066'
+ subject.puts("AB")
+ end
+ end
+
+ describe '#show_cursor' do
+ it 'shows the cursor with the command 0506' do
+ board.should_receive(:write).with '0506'
+ subject.show_cursor
+ end
+ end
+
+ describe '#hide_cursor' do
+ it 'hides the cursor with the command 0507' do
+ board.should_receive(:write).with '0507'
+ subject.hide_cursor
+ end
+ end
+
+ describe '#blink' do
+ it 'shows a blinking cursor with the command 0508' do
+ board.should_receive(:write).with '0508'
+ subject.blink
+ end
+ end
+
+ describe '#no_blink' do
+ it 'stops a blinking cursor with the command 0509' do
+ board.should_receive(:write).with '0509'
+ subject.no_blink
+ end
+ end
+
+ describe '#on' do
+ it 'turn on the display with the command 0510' do
+ board.should_receive(:write).with '0510'
+ subject.on
+ end
+ end
+
+ describe '#off' do
+ it 'turn off the display with the command 0511' do
+ board.should_receive(:write).with '0511'
+ subject.off
+ end
+ end
+
+ describe '#scroll_left' do
+ it 'move the text in the display one position to the left the command 0512' do
+ board.should_receive(:write).with '0512'
+ subject.scroll_left
+ end
+ end
+
+ describe '#scroll_right' do
+ it 'move the text in the display one position to the right the command 0513' do
+ board.should_receive(:write).with '0513'
+ subject.scroll_right
+ end
+ end
+
+ describe '#enable_autoscroll' do
+ it 'enable autoscroll with the command 0514' do
+ board.should_receive(:write).with '0514'
+ subject.enable_autoscroll
+ end
+ end
+
+ describe '#disable_autoscroll' do
+ it 'disable autoscroll with the command 0515' do
+ board.should_receive(:write).with '0515'
+ subject.disable_autoscroll
+ end
+ end
+
+ describe '#left_to_right' do
+ it 'set the display writing to start from the left with the command 0516' do
+ board.should_receive(:write).with '0516'
+ subject.left_to_right
+ end
+ end
+
+ describe '#right_to_left' do
+ it 'set the display writing to start from the right with the command 0517' do
+ board.should_receive(:write).with '0517'
+ subject.right_to_left
+ end
+ end
+ end
+ end
+end
View
60 src/DinoDisplay.h
@@ -0,0 +1,60 @@
+/*
+ DinoDisplay.h - LCD Display handling for Dino.
+ Created by Herman Moreno, March 24, 2013.
+ Released into the public domain.
+*/
+
+#include <Arduino.h>
+#include <LiquidCrystal.h>
+
+void beginLCD(LiquidCrystal &lcd, char *message) {
+ char cols[3];
+ char rows[3];
+ strncpy(cols, message + 4, 2);
+ strncpy(rows, message + 6, 2);
+ cols[2] = '\0';
+ rows[2] = '\0';
+ lcd.begin(atoi(cols), atoi(rows));
+}
+
+void setLCDCursor(LiquidCrystal &lcd, char *message) {
+ char col[3];
+ char row[3];
+ strncpy(col, message + 4, 2);
+ strncpy(row, message + 6, 2);
+ col[2] = '\0';
+ row[2] = '\0';
+ lcd.setCursor(atoi(col), atoi(row));
+}
+
+void writeLCD(LiquidCrystal &lcd, char *message) {
+ char val[4];
+ strncpy(val, message + 4, 3);
+ val[3] = '\0';
+ lcd.write(atoi(val));
+}
+
+void handleDisplay(LiquidCrystal &lcd, char *message) {
+ char cmd[2];
+ strncpy(cmd, message + 2, 2);
+ switch(atoi(cmd)) {
+ case 1: beginLCD(lcd, message); break;
+ case 2: lcd.clear(); break;
+ case 3: lcd.home(); break;
+ case 4: setLCDCursor(lcd, message); break;
+ case 5: writeLCD(lcd, message); break;
+ case 6: lcd.cursor(); break;
+ case 7: lcd.noCursor(); break;
+ case 8: lcd.blink(); break;
+ case 9: lcd.noBlink(); break;
+ case 10: lcd.display(); break;
+ case 11: lcd.noDisplay(); break;
+ case 12: lcd.scrollDisplayLeft(); break;
+ case 13: lcd.scrollDisplayRight(); break;
+ case 14: lcd.autoscroll(); break;
+ case 15: lcd.noAutoscroll(); break;
+ case 16: lcd.leftToRight(); break;
+ case 17: lcd.rightToLeft(); break;
+ default: break;
+ }
+}
View
6 src/du.ino
@@ -1,4 +1,6 @@
#include <Servo.h>
+#include <LiquidCrystal.h>
+#include "DinoDisplay.h"
bool debug = false;
@@ -11,6 +13,7 @@ char val[4];
char aux[4];
Servo servo;
+LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(115200);
@@ -64,6 +67,7 @@ void process() {
case 2: dr(pin,val); break;
case 3: aw(pin,val); break;
case 4: ar(pin,val); break;
+ case 5: handleDisplay(lcd, messageBuffer); break;
case 97: handlePing(pin,val,aux); break;
case 98: handleServo(pin,val,aux); break;
case 99: toggleDebug(val); break;
@@ -248,4 +252,4 @@ void handleServo(char *pin, char *val, char *aux) {
sprintf(m, "%s::read::%03d", pin, sval);
Serial.println(m);
}
-}
+}
Something went wrong with that request. Please try again.