diff --git a/OneSheeld/res/anim/no_blinking_cell.xml b/OneSheeld/res/anim/no_blinking_cell.xml new file mode 100644 index 000000000..769a59d77 --- /dev/null +++ b/OneSheeld/res/anim/no_blinking_cell.xml @@ -0,0 +1,10 @@ + + + diff --git a/OneSheeld/src/com/integreight/onesheeld/shields/controller/LcdShield.java b/OneSheeld/src/com/integreight/onesheeld/shields/controller/LcdShield.java new file mode 100644 index 000000000..7a38fdf95 --- /dev/null +++ b/OneSheeld/src/com/integreight/onesheeld/shields/controller/LcdShield.java @@ -0,0 +1,287 @@ +package com.integreight.onesheeld.shields.controller; + +import android.app.Activity; + +import com.integreight.firmatabluetooth.ShieldFrame; +import com.integreight.onesheeld.Log; +import com.integreight.onesheeld.enums.UIShield; +import com.integreight.onesheeld.utils.ControllerParent; + +public class LcdShield extends ControllerParent { + private LcdEventHandler eventHandler; + // private Activity activity; + public int rows = 2; + public int columns = 16; + public char[] chars; + public int currIndx = 0; + // Method ids + private static final byte PRINT = (byte) 0x11; + private static final byte BEGIN = (byte) 0x01; + private static final byte CLEAR = (byte) 0x02; + private static final byte HOME = (byte) 0x03; + // private static final byte NO_DISPLAY = (byte) 0x05; + // private static final byte DISPLAY = (byte) 0x06; + private static final byte NO_BLINK = (byte) 0x04; + private static final byte BLINK = (byte) 0x05; + private static final byte NO_CURSOR = (byte) 0x06; + private static final byte CURSOR = (byte) 0x07; + private static final byte SCROLL_DISPLAY_LEFT = (byte) 0x08; + private static final byte SCROLL_DISPLAY_RIGHT = (byte) 0x09; + private static final byte LEFT_TO_RIGHT = (byte) 0x0A; + private static final byte RIGHT_TO_LEFT = (byte) 0x0B; + // private static final byte CREATE_CHAR = (byte) 0x0F; + private static final byte SET_CURSOR = (byte) 0x0E; + private static final byte WRITE = (byte) 0x0F; + private static final byte AUTO_SCROLL = (byte) 0x0C; + private static final byte NO_AUTO_SCROLL = (byte) 0x0D; + public int lastScrollLeft = 0; + public boolean isBlinking = false, isCursoring = false; + private boolean isAutoScroll = false; + private boolean isLeftToRight = true; + + public LcdShield() { + super(); + } + + public LcdShield(Activity activity, String tag) { + super(activity, tag); + setChars(new char[rows * columns]); + } + + @Override + public ControllerParent setTag(String tag) { + setChars(new char[columns * rows]); + return super.setTag(tag); + } + + public void setLcdEventHandler(LcdEventHandler eventHandler) { + this.eventHandler = eventHandler; + CommitInstanceTotable(); + } + + public void write(char ch) { + if (currIndx > -1 && currIndx < chars.length) { + // if (isLeftToRight) { + if (!isAutoScroll) { + chars[currIndx] = ch; + changeCursor(isLeftToRight ? currIndx + 1 : currIndx - 1); + } else { + final char[] tmp = chars; + for (int i = 0; i < currIndx; i++) { + if (i + 1 < tmp.length && i + 1 > -1) + chars[i] = tmp[i + 1]; + } + if (currIndx - 1 > -1 && currIndx - 1 < chars.length) + chars[currIndx - 1] = ch; + } + } + } + + public void changeCursor(int indx) { + if (!isAutoScroll && indx > -1 && indx < rows * columns) { + if (eventHandler != null) { + eventHandler.noBlink(); + eventHandler.noCursor(); + } + currIndx = indx; + } + } + + public synchronized void scrollDisplayLeft() { + lastScrollLeft = 1; + char[] tmp = new char[chars.length]; + for (int i = 0; i < tmp.length; i++) { + if (i + lastScrollLeft > -1 && i + lastScrollLeft < chars.length) { + tmp[i] = chars[i + lastScrollLeft]; + } + } + if (eventHandler != null) + eventHandler.noBlink(); + changeCursor(currIndx - 1); + chars = tmp; + if (eventHandler != null) { + eventHandler.updateLCD(chars); + if (isBlinking) + eventHandler.blink(); + } + Log.d("LCD", (">>>>>>> Left " + lastScrollLeft)); + } + + public synchronized void scrollDisplayRight() { + lastScrollLeft = -1; + char[] tmp = new char[chars.length]; + for (int i = 0; i < tmp.length; i++) { + if (i + lastScrollLeft > -1 && i + lastScrollLeft < chars.length) { + tmp[i] = chars[i + lastScrollLeft]; + } + } + if (eventHandler != null) + eventHandler.noBlink(); + changeCursor(currIndx + 1); + chars = tmp; + if (eventHandler != null) { + eventHandler.updateLCD(chars); + if (isBlinking) + eventHandler.blink(); + } + Log.d("LCD", (">>>>>>> Right " + lastScrollLeft)); + } + + public static interface LcdEventHandler { + public void updateLCD(char[] arrayToUpdate); + + public void blink(); + + public void noBlink(); + + public void cursor(); + + public void noCursor(); + } + + private void processInput(ShieldFrame frame) { + switch (frame.getFunctionId()) { + case CLEAR: + if (eventHandler != null) { + eventHandler.noBlink(); + eventHandler.noCursor(); + } + lastScrollLeft = 0; + chars = new char[columns * rows]; + if (eventHandler != null) + eventHandler.updateLCD(chars); + changeCursor(0); + if (isBlinking && eventHandler != null) + eventHandler.blink(); + if (isCursoring && eventHandler != null) + eventHandler.cursor(); + break; + case HOME: + if (eventHandler != null) + eventHandler.noBlink(); + if (eventHandler != null) + eventHandler.noCursor(); + changeCursor(0); + if (isBlinking && eventHandler != null) + eventHandler.blink(); + if (isCursoring && eventHandler != null) + eventHandler.cursor(); + break; + case BLINK: + if (eventHandler != null) + eventHandler.blink(); + isBlinking = true; + break; + case NO_BLINK: + if (eventHandler != null) + eventHandler.noBlink(); + isBlinking = false; + break; + + case SCROLL_DISPLAY_LEFT: + scrollDisplayLeft(); + break; + + case SCROLL_DISPLAY_RIGHT: + scrollDisplayRight(); + break; + + case BEGIN: + // if (eventHandler != null) + // eventHandler.noBlink(); + // changeCursor(0); + // if (isBlinking && eventHandler != null) + // eventHandler.blink(); + break; + case SET_CURSOR: + if (eventHandler != null) { + eventHandler.noBlink(); + eventHandler.noCursor(); + } + int row = (int) frame.getArgument(0)[0]; + int col = (int) frame.getArgument(1)[0]; + changeCursor((row * columns) + col); + if (isBlinking && eventHandler != null) + eventHandler.blink(); + if (isCursoring && eventHandler != null) + eventHandler.cursor(); + break; + case WRITE: + write(frame.getArgumentAsString(0).charAt(0)); + if (eventHandler != null) { + eventHandler.updateLCD(chars); + if (isBlinking) + eventHandler.blink(); + if (isCursoring) + eventHandler.cursor(); + } + break; + case PRINT: + lastScrollLeft = 0; + if (eventHandler != null) { + eventHandler.noBlink(); + eventHandler.noCursor(); + } + lastScrollLeft = 0; + String txt = frame.getArgumentAsString(0); + for (int i = 0; i < txt.length(); i++) { + write(txt.charAt(i)); + } + if (eventHandler != null) { + eventHandler.updateLCD(chars); + if (isBlinking) + eventHandler.blink(); + if (isCursoring) + eventHandler.cursor(); + } + break; + + case CURSOR: + if (eventHandler != null) + eventHandler.cursor(); + isCursoring = true; + break; + + case NO_CURSOR: + if (eventHandler != null) + eventHandler.noCursor(); + isCursoring = false; + break; + case AUTO_SCROLL: + isAutoScroll = true; + break; + case NO_AUTO_SCROLL: + isAutoScroll = false; + break; + case LEFT_TO_RIGHT: + isLeftToRight = true; + break; + case RIGHT_TO_LEFT: + isLeftToRight = false; + break; + default: + break; + } + } + + @Override + public void onNewShieldFrameReceived(ShieldFrame frame) { + if (frame.getShieldId() == UIShield.LCD_SHIELD.getId()) + processInput(frame); + } + + @Override + public void reset() { + // TODO Auto-generated method stub + + } + + public char[] getChars() { + return chars; + } + + public void setChars(char[] chars) { + this.chars = chars; + } + +} diff --git a/OneSheeld/src/com/integreight/onesheeld/shields/fragments/LcdFragment.java b/OneSheeld/src/com/integreight/onesheeld/shields/fragments/LcdFragment.java new file mode 100644 index 000000000..c082a951c --- /dev/null +++ b/OneSheeld/src/com/integreight/onesheeld/shields/fragments/LcdFragment.java @@ -0,0 +1,383 @@ +package com.integreight.onesheeld.shields.fragments; + +import android.graphics.Color; +import android.graphics.Typeface; +import android.os.Bundle; +import android.os.Handler; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.AnimationUtils; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.TextView; + +import com.integreight.onesheeld.R; +import com.integreight.onesheeld.shields.controller.LcdShield; +import com.integreight.onesheeld.shields.controller.LcdShield.LcdEventHandler; +import com.integreight.onesheeld.utils.ShieldFragmentParent; +import com.integreight.onesheeld.utils.customviews.RotatingTextView; + +public class LcdFragment extends ShieldFragmentParent { + View v; + private boolean drawn = false; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + if (v == null) { + v = inflater.inflate(R.layout.lcd_shield_fragment_layout, + container, false); + new Handler().postDelayed(new Runnable() { + + @Override + public void run() { + draw(0, 0, ((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).rows, + ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).columns); + drawn = true; + } + }, 0); + } else + try { + ((ViewGroup) v.getParent()).removeView(v); + } catch (Exception e) { + // TODO: handle exception + } + return v; + } + + @Override + public void onStart() { + super.onStart(); + uiHandler = new Handler(); + ((LcdShield) getApplication().getRunningShields().get( + getControllerTag())).setLcdEventHandler(lcdEventHandler); + new Handler().postDelayed(new Runnable() { + + @Override + public void run() { + redraw(((LcdShield) getApplication().getRunningShields().get( + getControllerTag())).chars); + drawn = true; + } + }, 0); + } + + @Override + public void onStop() { + try { + clear(false, false); + drawn = false; + } catch (Exception e) { + } + super.onStop(); + } + + LinearLayout verticalContainer, firstRow, secondRow; + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + verticalContainer = (LinearLayout) v + .findViewById(R.id.verticalContainer); + v.findViewById(R.id.bg).setBackgroundColor(Color.BLUE); + super.onActivityCreated(savedInstanceState); + } + + public void draw(int initRow, int initCol, int rowsEnd, int columnsEnd) { + float scale = getResources().getDisplayMetrics().density; + int height = (int) (30 * scale + .5f); + int cellMargine = (int) (scale + .5f); + verticalContainer.removeAllViews(); + Typeface tf = Typeface.createFromAsset(getAppActivity().getAssets(), + "lcd_font.ttf"); + for (int i = initRow; i < rowsEnd; i++) { + RelativeLayout rowCont = new RelativeLayout(getAppActivity()); + LinearLayout rowBG = new LinearLayout(getAppActivity()); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( + height, LinearLayout.LayoutParams.WRAP_CONTENT); + rowCont.setLayoutParams(params); + rowBG.setClickable(true); + rowCont.setClickable(true); + RelativeLayout.LayoutParams paramsCh = new RelativeLayout.LayoutParams( + height, RelativeLayout.LayoutParams.WRAP_CONTENT); + rowBG.setLayoutParams(paramsCh); + rowBG.setOrientation(LinearLayout.VERTICAL); + for (int j = initCol; j < columnsEnd; j++) { + RelativeLayout cellCont = new RelativeLayout(getAppActivity()); + LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams( + height, LinearLayout.LayoutParams.MATCH_PARENT); + cellParams.weight = 1; + cellParams.bottomMargin = cellMargine; + cellParams.topMargin = cellMargine; + cellParams.leftMargin = cellMargine; + cellParams.rightMargin = cellMargine; + cellCont.setLayoutParams(cellParams); + RelativeLayout.LayoutParams childParams = new RelativeLayout.LayoutParams( + RelativeLayout.LayoutParams.MATCH_PARENT, height); + TextView bg = new TextView(getAppActivity()); + bg.setLayoutParams(childParams); + bg.setBackgroundColor(0x99000000); + RelativeLayout.LayoutParams curParams = new RelativeLayout.LayoutParams( + height / 10, height); + // curParams.topMargin = (int) (1 * scale + .5f); + // curParams.bottomMargin = (int) (1 * scale + .5f); + TextView cur = new TextView(getAppActivity()); + cur.setLayoutParams(curParams); + cur.setBackgroundColor(0xffffffff); + cur.setVisibility(View.GONE); + cellCont.addView(bg); + cellCont.addView(cur); + cellCont.setClickable(true); + rowBG.addView(cellCont); + } + LinearLayout rowTxt = new LinearLayout(getAppActivity()); + RelativeLayout.LayoutParams paramsCh2 = new RelativeLayout.LayoutParams( + height, RelativeLayout.LayoutParams.WRAP_CONTENT); + rowTxt.setLayoutParams(paramsCh2); + rowTxt.setOrientation(LinearLayout.VERTICAL); + for (int j = 0; j < columnsEnd; j++) { + RelativeLayout cellCont = new RelativeLayout(getAppActivity()); + RotatingTextView cell = new RotatingTextView(getAppActivity()); + cell.setTypeface(tf); + LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams( + height, LinearLayout.LayoutParams.MATCH_PARENT); + cellParams.weight = 1; + cellParams.bottomMargin = cellMargine; + cellParams.topMargin = cellMargine; + cellParams.leftMargin = cellMargine; + cellParams.rightMargin = cellMargine; + cell.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); + cell.setGravity(Gravity.CENTER); + cell.setTextColor(Color.WHITE); + // cell.setText(((LcdShieldd) + // getApplication().getRunningShields() + // .get(getControllerTag())).chars[(i * columnsEnd) + j] + // + ""); + cell.setSingleLine(true); + cellCont.setLayoutParams(cellParams); + RelativeLayout.LayoutParams childParams = new RelativeLayout.LayoutParams( + RelativeLayout.LayoutParams.MATCH_PARENT, height); + cell.setLayoutParams(childParams); + cellCont.addView(cell); + cellCont.setClickable(true); + rowTxt.addView(cellCont); + } + rowCont.addView(rowBG); + rowCont.addView(rowTxt); + verticalContainer.setGravity(Gravity.CENTER); + verticalContainer.addView(rowCont); + } + } + + private synchronized ViewGroup getCellContainerBG(int curIndx) { + int row = curIndx >= ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).columns ? 1 : 0; + int col = curIndx >= ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).columns ? curIndx + - ((LcdShield) getApplication().getRunningShields().get( + getControllerTag())).columns : curIndx; + return ((ViewGroup) ((ViewGroup) ((ViewGroup) verticalContainer + .getChildAt(((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows - row - 1)) + .getChildAt(0)).getChildAt(col)); + } + + public synchronized void noCursor() { + for (int i = 0; i < ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows + * ((LcdShield) getApplication().getRunningShields().get( + getControllerTag())).columns; i++) { + final int currIndx = i; + if (currIndx > -1 + && currIndx < (((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).columns * ((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).rows)) { + getCellContainerBG(currIndx).getChildAt(1).startAnimation( + AnimationUtils.loadAnimation(getAppActivity(), + R.anim.no_blink_cell)); + getCellContainerBG(currIndx).getChildAt(1).setVisibility( + View.INVISIBLE); + } + } + } + + public synchronized void cursor() { + final int currIndx = ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).currIndx; + if (currIndx > -1 + && currIndx < ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).columns + * ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows) { + getCellContainerBG(currIndx).getChildAt(1).setVisibility( + View.VISIBLE); + getCellContainerBG(currIndx).getChildAt(1).startAnimation( + AnimationUtils.loadAnimation(getAppActivity(), + R.anim.blink_cell)); + } + } + + public synchronized void noBlink() { + for (int i = 0; i < ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows + * ((LcdShield) getApplication().getRunningShields().get( + getControllerTag())).columns; i++) { + final int currIndx = i; + if (currIndx > -1 + && currIndx < (((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).columns * ((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).rows)) { + getCellContainerBG(currIndx).getChildAt(0).startAnimation( + AnimationUtils.loadAnimation(getAppActivity(), + R.anim.no_blinking_cell)); + } + } + } + + public synchronized void blink() { + final int currIndx = ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).currIndx; + if (currIndx > -1 + && currIndx < ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).columns + * ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows) { + getCellContainerBG(currIndx).getChildAt(0).setVisibility( + View.VISIBLE); + getCellContainerBG(currIndx).getChildAt(0).startAnimation( + AnimationUtils.loadAnimation(getAppActivity(), + R.anim.blink_cell)); + } + } + + public synchronized void clear(boolean changeCursor, boolean clearView) { + for (int i = 0; i < ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows; i++) { + LinearLayout rowTxt = (LinearLayout) ((ViewGroup) verticalContainer + .getChildAt(((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).rows + - i - 1)).getChildAt(1); + for (int j = 0; j < rowTxt.getChildCount(); j++) { + ((RotatingTextView) ((ViewGroup) rowTxt.getChildAt(j)) + .getChildAt(0)).setText(""); + } + } + } + + public void blinkLCD() { + v.findViewById(R.id.bg).startAnimation( + AnimationUtils.loadAnimation(getAppActivity(), + R.anim.blink_anim_bg)); + } + + public void noBlinkLCD() { + v.findViewById(R.id.bg).startAnimation( + AnimationUtils.loadAnimation(getAppActivity(), + R.anim.no_anim_bg)); + } + + private synchronized void redraw(char[] arr) { + for (int i = 0; i < ((LcdShield) getApplication().getRunningShields() + .get(getControllerTag())).rows; i++) { + LinearLayout rowTxt = (LinearLayout) ((ViewGroup) verticalContainer + .getChildAt(((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).rows + - i - 1)).getChildAt(1); + for (int j = 0; j < rowTxt.getChildCount(); j++) { + ((RotatingTextView) ((ViewGroup) rowTxt.getChildAt(j)) + .getChildAt(0)).startAnimation(AnimationUtils + .loadAnimation(getAppActivity(), R.anim.rotate_lcd)); + ((RotatingTextView) ((ViewGroup) rowTxt.getChildAt(j)) + .getChildAt(0)) + .setText(arr[(i * ((LcdShield) getApplication() + .getRunningShields().get(getControllerTag())).columns) + + j] + + ""); + } + } + } + + private LcdEventHandler lcdEventHandler = new LcdEventHandler() { + + @Override + public void updateLCD(final char[] arrayToUpdate) { + if (canChangeUI() && drawn) + uiHandler.post(new Runnable() { + + @Override + public void run() { + redraw(arrayToUpdate); + } + }); + + } + + @Override + public void blink() { + if (canChangeUI() && drawn) + uiHandler.post(new Runnable() { + + @Override + public void run() { + LcdFragment.this.blink(); + } + }); + } + + @Override + public void noBlink() { + if (canChangeUI() && drawn) + uiHandler.post(new Runnable() { + + @Override + public void run() { + LcdFragment.this.noBlink(); + } + }); + } + + @Override + public void cursor() { + if (canChangeUI() && drawn) + uiHandler.post(new Runnable() { + + @Override + public void run() { + LcdFragment.this.cursor(); + } + }); + } + + @Override + public void noCursor() { + if (canChangeUI() && drawn) + uiHandler.post(new Runnable() { + + @Override + public void run() { + LcdFragment.this.noCursor(); + } + }); + } + + }; + + private void initializeFirmata() { + if ((getApplication().getRunningShields().get(getControllerTag())) == null) + getApplication().getRunningShields().put(getControllerTag(), + new LcdShield(getAppActivity(), getControllerTag())); + ((LcdShield) getApplication().getRunningShields().get( + getControllerTag())).setLcdEventHandler(lcdEventHandler); + } + + @Override + public void doOnServiceConnected() { + initializeFirmata(); + } + +}