Skip to content

Instantly share code, notes, and snippets.

@mr5z
Created August 25, 2015 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/e76b4e16c66353c77e12 to your computer and use it in GitHub Desktop.
Save mr5z/e76b4e16c66353c77e12 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<com.nkraft.mcusimulator.PanelView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_emulator_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.nkraft.mcusimulator.LcdModule
android:id="@+id/activity_emulator_module_lcd"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.nkraft.mcusimulator.DigitModule
android:id="@+id/activity_emulator_module_digit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.nkraft.mcusimulator.KeypadModule
android:id="@+id/activity_emulator_module_keypad"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.nkraft.mcusimulator.McuModule
android:id="@+id/activity_emulator_module_mcu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.nkraft.mcusimulator.PanelView>
package com.nkraft.mcusimulator;
import com.nkraft.mcusimulator.KeypadModule.OnKeypadClickListener;
import com.nkraft.nkraftlibs.Debug;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class PanelView extends LinearLayout implements View.OnTouchListener {
private Processor mProcessor;
private LcdModule mLcdModule;
private DigitModule mDigitModule;
private KeypadModule mKeypadModule;
private McuModule mMcuModule;
private Paint mPaint = new Paint();
private boolean mTestMode = false;
int scaled;
int beepDelay;
int[] posLed = { 27, 654, 67, 774, 27, 87, 151, 216, 278, 341, 406, 471, 568, 630, 695, 758, 823, 884, 947, 1012, 1215 };
int[] posReset = {29, 1305, 135, 1387};
int[] digitDef = {63, 3, 109, 103, 83, 118, 126, 35, 127, 119, 123, 94, 60, 79, 124, 120, 0};
int[] posIrq = {9, 105, 58, 195};
public PanelView(Context context) {
super(context);
initialize(context);
}
public PanelView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public PanelView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
public void initialize(Context context) {
LayoutInflater.from(context).inflate(R.layout.activity_emulator, this, true);
mLcdModule = (LcdModule) findViewById(R.id.activity_emulator_module_lcd);
mDigitModule = (DigitModule) findViewById(R.id.activity_emulator_module_digit);
mKeypadModule = (KeypadModule) findViewById(R.id.activity_emulator_module_keypad);
mMcuModule = (McuModule) findViewById(R.id.activity_emulator_module_mcu);
setOnTouchListener(this);
// Create new CPU instance
mProcessor = new Processor(this);
// mKeypadModule.setOnKeypadClickListener( new OnKeypadClickListener() {
// @Override
// public void onClick(int key) {
// // Register key into CPU register
// if ( mProcessor.isAutoRun() ) {
// mProcessor.setAddress(12, key);
// }
// }
// });
// scaleMatrix();
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Read x,y coordinates
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
Debug.log("position(%d, %d", x, y);
int action = motionEvent.getAction();
switch ( action ) {
case MotionEvent.ACTION_UP:
// traceMode release
if (y < 200) {
mProcessor.mPause = false;
}
handleResetSw(x, y);
handleIrq(x, y);
break;
case MotionEvent.ACTION_DOWN:
view.performClick();
break;
}
if (mTestMode) {
test(x, y);
}
invalidate();
return true;
}
@Override
public void invalidate() {
super.invalidate();
invalidateLcdModule();
}
private void scaleMatrix() {
if (scaled == 1) return;
scaled = 1;
int mScreenWidth = 0;
int mScreenHeight = 0;
int shift = 100;
int xmax = 1080;
int ymax = 1920 - shift;
int vsize = mScreenHeight - ((shift * mScreenHeight) / ymax);
// scale matrices
// Buttons / LEDs
posReset[0] = (posReset[0] * mScreenWidth) / xmax;
posReset[1] = ((posReset[1] * vsize) / ymax);
posReset[2] = (posReset[2] * mScreenWidth) / xmax;
posReset[3] = ((posReset[3] * vsize) / ymax);
// Led bargraph
posLed[0] = (posLed[0] * mScreenWidth) / xmax;
posLed[1] = ((posLed[1] * vsize) / ymax);
posLed[2] = (posLed[2] * mScreenWidth) / xmax;
posLed[3] = ((posLed[3] * vsize) / ymax);
for (int i = 4; i < 20; i++) {
posLed[i] = (posLed[i] * mScreenWidth) / xmax;
}
}
private void test(int x, int y) {
// Write something on LCD
getLcd().setData("TEST x:" + x + " y:" + y);
// write something on Digits
x = x & 15;
y = y & 15;
mDigitModule.segments[0] = digitDef[x];
mDigitModule.segments[1] = digitDef[y];
mDigitModule.segments[2] = digitDef[15 - x];
mDigitModule.segments[3] = digitDef[15 - y];
mDigitModule.segments[4] = digitDef[x & y];
mDigitModule.segments[5] = digitDef[mKeypadModule.getKey()];
}
private void handleResetSw(int x, int y) {
if (x > posReset[0] && x < posReset[2] && y > posReset[1] && y < posReset[3]) {
mProcessor.reset();
mProcessor.resetHardware();
mProcessor.setAutoRun(true);
}
}
private void handleIrq(int x, int y) {
if (x > posIrq[0] && x < posIrq[2] && y > posIrq[1] && y < posIrq[3]) {
// check if mInterrupted flag in control reg is enabled
mProcessor.mInterrupted = ((mProcessor.getAddress(11) & 2) > 0);
}
}
public Processor getProcessor() {
return mProcessor;
}
public boolean isTestMode() {
return mTestMode;
}
public void beep() {
if (beepDelay == 0) {
ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
beepDelay = 5;
} else beepDelay--;
}
public void drawLeds(Canvas canvas) {
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(1);
int hlen = posLed[2] - posLed[0];
int vlen = posLed[3] - posLed[1];
mPaint.setColor(Color.LTGRAY);
for (int x = 4; x < 20; x++) {
canvas.drawRect(posLed[x], posLed[1], posLed[x] + hlen, posLed[1] + vlen, mPaint);
}
mPaint.setColor(Color.RED);
for (int x = 4, y = 7; x < 12; x++) {
if (((mDigitModule.getLedBar1() >> y) & 1) != 0) {
canvas.drawRect(posLed[x], posLed[1], posLed[x] + hlen, posLed[1] + vlen, mPaint);
}
y--;
}
for (int x = 12, y = 7; x < 20; x++) {
if (((mDigitModule.getLedBar2() >> y) & 1) != 0) {
canvas.drawRect(posLed[x], posLed[1], posLed[x] + hlen, posLed[1] + vlen, mPaint);
}
y--;
}
}
public void writeDigit(int select, int bits) {
mDigitModule.segments[select] = bits;
}
public void reset() {
mLcdModule.setData("");
mLcdModule.showData(true);
mDigitModule.resetSegments();
mDigitModule.setLedBar1(0);
mDigitModule.setLedBar2(0);
}
public void invalidateLcdModule() {
mLcdModule.invalidate();
}
public LcdModule getLcd() {
return mLcdModule;
}
public DigitModule getDigit() {
return mDigitModule;
}
public KeypadModule getKeypad() {
return mKeypadModule;
}
public McuModule getMcu() {
return mMcuModule;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment