Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit PN532examplesapduToBlackBerryBlackBerry7srcmypackageMyScreen.java

163 lines · 5.3 KB · Java

1/**
2 Copyright (c) 2011, 2012, 2013 Research In Motion Limited.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15**/
16package mypackage;
17
18import net.rim.device.api.io.nfc.NFCException;
19import net.rim.device.api.io.nfc.emulation.VirtualISO14443Part4TargetCallback;
20import net.rim.device.api.io.nfc.emulation.VirtualISO14443Part4TypeATarget;
21import net.rim.device.api.ui.Field;
22import net.rim.device.api.ui.FieldChangeListener;
23import net.rim.device.api.ui.component.CheckboxField;
24import net.rim.device.api.ui.component.LabelField;
25import net.rim.device.api.ui.container.MainScreen;
26import net.rim.device.api.ui.decor.BackgroundFactory;
27
28/**
29 * A class extending the MainScreen class, which provides default standard
30 * behavior for BlackBerry GUI applications.
31 */
32public final class MyScreen extends MainScreen implements
33 VirtualISO14443Part4TargetCallback, FieldChangeListener {
34
35 private int color = 0x7f7f7f;
36
37 private boolean colorQueued = false;
38 private CheckboxField colorQueueLocked;
39 private VirtualISO14443Part4TypeATarget emulated;
40 private ColorMixer mixer;
41
42 private LabelField status;
43
44 /**
45 * Creates a new MyScreen object
46 */
47 public MyScreen() {
48 // Set the displayed title of the screen
49 setTitle("Colour Control Setter");
50 setBackground(BackgroundFactory.createSolidBackground(0x3f3f3f));
51
52 add(new LabelField(""));
53 add(colorQueueLocked = new CheckboxField("Lock Color", false));
54 colorQueueLocked.setChangeListener(this);
55
56 add(mixer = new ColorMixer(this));
57 mixer.setColor(0x7f7f7f);
58
59 add(status = new LabelField(""));
60
61 try {
62 emulated = new VirtualISO14443Part4TypeATarget(this, "10451045",
63 null);
64 log("created");
65 emulated.startEmulation();
66 log("emulating");
67 } catch (NFCException e) {
68 log("Failed to listen");
69 status.setText(e.getClass().getName() + " " + e.getMessage());
70 e.printStackTrace();
71 }
72 }
73
74 // private TextField textField;
75
76 public void fieldChanged(Field field, int context) {
77 if (colorQueueLocked.getChecked()) {
78 setColor(color);
79 } else {
80 colorQueued = false;
81 status.setText("");
82 }
83 }
84
85 private boolean isColorQueueLocked() {
86 return colorQueueLocked.getChecked();
87 }
88
89 protected void log(String message) {
90 System.out.println("SUPERDUPER: " + message);
91 // textField.setText(textField.getText() + "\n" + message);
92 }
93
94 public void onVirtualTargetEvent(int targetEvent) {
95 log("Target event: " + targetEvent);
96 }
97
98 public byte[] processCommand(byte[] command) {
99 byte[] toReturn = processCommandInternal(command);
100 String message = "[" + toString(command) + "," + toString(toReturn)
101 + "]";
102 // status.setText(message);
103 // log(message);
104 return toReturn;
105 }
106
107 private byte[] processCommandInternal(byte[] command) {
108 if (command == null) {
109 return new byte[0];
110 }
111 int messageType = command[0] & 0xff;
112 switch (messageType) {
113 case 1:
114 if (command.length == 4) {
115 int colorIn = (command[1] & 0xff) << 16;
116 colorIn |= (command[2] & 0xff) << 8;
117 colorIn |= (command[3] & 0xff);
118
119 if (colorIn != color) {
120 if (colorQueued) {
121
122 byte[] toReturn = new byte[4];
123 toReturn[0] = 1;
124 toReturn[1] = (byte) (color >> 16);
125 toReturn[2] = (byte) (color >> 8);
126 toReturn[3] = (byte) color;
127
128 if (!isColorQueueLocked()) {
129 colorQueued = false;
130 status.setText("");
131 }
132
133 return toReturn;
134 } else {
135 this.color = colorIn;
136 mixer.setColor(colorIn);
137 }
138 }
139 }
140 break;
141 }
142 return new byte[] { 0 };
143 }
144
145 public void setColor(int color) {
146 colorQueued = true;
147 this.color = color;
148 status.setText("#" + toHex(color));
149 }
150
151 private String toHex(int i) {
152 String toReturn = "000000" + Integer.toString(i, 16);
153 return toReturn.substring(toReturn.length() - 6);
154 }
155
156 private String toString(byte[] command) {
157 String toReturn = "";
158 for (int i = 0; i < command.length; ++i) {
159 toReturn += Integer.toString(command[i] & 0xff, 16) + " ";
160 }
161 return toReturn;
162 }
163}