├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── README.txt
├── apk
└── ArduinoColor.apk
├── gen
└── com
│ └── trevorshp
│ └── arduinocolor
│ ├── BuildConfig.java
│ └── R.java
├── libs
├── android-support-v4.jar
└── usb-serial-for-android-v010.jar
├── lint.xml
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-ldpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── layout
│ └── activity_main.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
├── values
│ ├── strings.xml
│ └── styles.xml
└── xml
│ └── device_filter.xml
└── src
└── com
└── trevorshp
└── arduinocolor
├── ColorPickerView.java
└── MainActivity.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ArduinoColor
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
14 |
15 |
19 |
24 |
25 |
26 |
27 |
28 |
29 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/README.txt:
--------------------------------------------------------------------------------
1 | These are the source files for an Android App called Arduino Color.
2 | For more information on starting to develop Android apps, visit http://developer.android.com/training/index.html
3 |
4 | 1. Store all these files in a root directory called ArduinoColor.
5 |
6 | 2. In Eclipse (or other IDE) select Import > Existing Projects into Workspace.
7 |
8 | 3. Select the ArduinoColor folder.
9 |
10 | 4. Run the project as an Android Application with your phone connected to your computer
11 |
--------------------------------------------------------------------------------
/apk/ArduinoColor.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/apk/ArduinoColor.apk
--------------------------------------------------------------------------------
/gen/com/trevorshp/arduinocolor/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.trevorshp.arduinocolor;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/com/trevorshp/arduinocolor/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.trevorshp.arduinocolor;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class drawable {
14 | public static final int ic_launcher=0x7f020000;
15 | }
16 | public static final class id {
17 | public static final int colorFragLayout=0x7f070002;
18 | public static final int color_picker_layout=0x7f070005;
19 | public static final int color_picker_slider_text=0x7f070006;
20 | public static final int fragLayout=0x7f070001;
21 | public static final int main_layout=0x7f070000;
22 | public static final int randomButton=0x7f070008;
23 | public static final int result1_textview=0x7f070004;
24 | public static final int results_holder_layout=0x7f070003;
25 | public static final int seekBar1=0x7f070007;
26 | }
27 | public static final class layout {
28 | public static final int activity_main=0x7f030000;
29 | }
30 | public static final class string {
31 | public static final int app_name=0x7f050000;
32 | public static final int button_random=0x7f050004;
33 | public static final int color_picker_slider_text=0x7f050002;
34 | public static final int color_picker_slider_text_short=0x7f050003;
35 | public static final int title_activity_main=0x7f050001;
36 | }
37 | public static final class style {
38 | /**
39 | Base application theme, dependent on API level. This theme is replaced
40 | by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
41 |
42 |
43 | Theme customizations available in newer API levels can go in
44 | res/values-vXX/styles.xml, while customizations related to
45 | backward-compatibility can go here.
46 |
47 |
48 | Base application theme for API 11+. This theme completely replaces
49 | AppBaseTheme from res/values/styles.xml on API 11+ devices.
50 |
51 | API 11 theme customizations can go here.
52 |
53 | Base application theme for API 14+. This theme completely replaces
54 | AppBaseTheme from BOTH res/values/styles.xml and
55 | res/values-v11/styles.xml on API 14+ devices.
56 |
57 | API 14 theme customizations can go here.
58 | */
59 | public static final int AppBaseTheme=0x7f060000;
60 | /** Application theme.
61 | All customizations that are NOT specific to a particular API-level can go here.
62 | */
63 | public static final int AppTheme=0x7f060001;
64 | }
65 | public static final class xml {
66 | public static final int device_filter=0x7f040000;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/libs/usb-serial-for-android-v010.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/libs/usb-serial-for-android-v010.jar
--------------------------------------------------------------------------------
/lint.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-17
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trevorshannon/ArduinoColor/a59bd0abbca9a827533a855e3feadd422fcc4c5e/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
14 |
15 |
20 |
21 |
26 |
27 |
39 |
40 |
41 |
45 |
46 |
52 |
53 |
61 |
62 |
69 |
70 |
76 |
77 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Arduino Color
5 | Arduino Color
6 | Use the slider or pinch the color picker to add or subtract blue for all possible hex colors.
7 | Use the slider to add or subtract blue, creating all possible hex colors.
8 | Random
9 |
10 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
--------------------------------------------------------------------------------
/res/xml/device_filter.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/com/trevorshp/arduinocolor/ColorPickerView.java:
--------------------------------------------------------------------------------
1 | package com.trevorshp.arduinocolor;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Paint;
8 | import android.graphics.Rect;
9 | import android.graphics.drawable.ShapeDrawable;
10 | import android.graphics.drawable.shapes.OvalShape;
11 | import android.util.TypedValue;
12 | import android.view.View;
13 |
14 | public class ColorPickerView extends View {
15 |
16 | // for the thumbnail
17 | private ShapeDrawable thumb;
18 | private ShapeDrawable thumb2;
19 | private int thumbRadius = 51; //value in dp
20 | private int thumbEdge = 1; //value in dp
21 | private boolean thumbIsVisible=false;
22 |
23 | // for the bitmap
24 | private Bitmap bitty;
25 | private static final int bwidth = 256;
26 | private static final int bheight = 256;
27 | private int[] pixels = new int[bwidth*bheight];
28 | private double factor = 3.1;
29 | public int paddingx = 0;
30 | public int paddingy = 0;
31 |
32 | public int size;
33 |
34 | // coordinates of the currently selected pixel (0-255)
35 | private int xp;
36 | private int yp;
37 |
38 | // value for the z part of the color graph
39 | private float kPrev = 150;
40 |
41 | private Rect r1 = new Rect(0,0,bwidth,bheight);
42 | private Rect r2;
43 |
44 | public ColorPickerView(Context context, int blue, int density) {
45 | super(context);
46 | Bitmap.Config config = Bitmap.Config.ARGB_8888;
47 | bitty = Bitmap.createBitmap(bwidth,bheight,config);
48 | updateBitmap(blue);
49 | thumbRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, thumbRadius, getResources().getDisplayMetrics());
50 | thumbEdge = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, thumbEdge, getResources().getDisplayMetrics());
51 | createThumb(paddingx,paddingy,Color.BLACK);
52 | }
53 |
54 | @Override
55 | protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
56 | int width = this.getWidth();
57 | int height = this.getHeight();
58 | if (height != 0 && changed == true) {
59 | if (width > height) {
60 | paddingx = (width-height+1)/2;
61 | size = height;
62 | } else if (width < height) {
63 | paddingy = (height-width+1)/2;
64 | size = width;
65 | }
66 | factor = (1.0*size-2.0*Math.min(paddingx, paddingy))/(1.0*bwidth);
67 | r2 = new Rect(paddingx,paddingy,(int) width-paddingx,(int) height-paddingy);
68 | }
69 | }
70 |
71 | protected void onDraw(Canvas canvas) {
72 | canvas.drawBitmap(bitty, r1, r2, null);
73 | if (thumbIsVisible) {
74 | thumb.draw(canvas);
75 | thumb2.draw(canvas);
76 | }
77 | }
78 |
79 | // gets the color at a certain point
80 | public int getColor(float xf, float yf, boolean wasTouched) {
81 | int x = (int) xf;
82 | int y = (int) yf;
83 |
84 | if (!thumbIsVisible && wasTouched) {
85 | thumbIsVisible=true;
86 | }
87 |
88 | if (x >= r2.right) x = r2.right - 1;
89 | else if (x < r2.left) x = r2.left;
90 | if (y >= r2.bottom) y = r2.bottom - 1;
91 | else if (y < r2.top) y = r2.top;
92 |
93 | //xp and yp are in the scaled bitmap from 0-255
94 | xp = (int) ((1.0*(x-paddingx))/factor);
95 | yp = (int) ((1.0*(y-paddingy))/factor);
96 |
97 | moveThumb(x,y);
98 |
99 | int pixel = bitty.getPixel(xp, yp);
100 | thumb.getPaint().setColor(pixel);
101 | return pixel;
102 | }
103 |
104 | private void moveThumb(int x, int y) {
105 | thumb.setBounds(x-thumbRadius,y-thumbRadius,x+thumbRadius,y+thumbRadius);
106 | thumb2.setBounds(x-thumbRadius-thumbEdge,y-thumbRadius-thumbEdge,x+thumbRadius+thumbEdge,y+thumbRadius+thumbEdge);
107 | }
108 |
109 | public void noColor() {
110 | thumbIsVisible = false;
111 | }
112 |
113 | // called at multitouch events
114 | public int updateShade(double scale) {
115 | float k = ((float)scale-1)*100+kPrev;
116 | if (k > 255) k = 255; // if hsv, set this to 360
117 | if (k < 0) k = 0; // can't let it go to 0 or we never scale it back again
118 | updateBitmap((int)k);
119 | if (thumbIsVisible) {
120 | thumbIsVisible=false;
121 | }
122 | int pixel = bitty.getPixel(xp, yp);
123 | thumb.getPaint().setColor(pixel);
124 | return pixel;
125 | }
126 |
127 | // called at seekbar events
128 | public int updateShade(int shade) {
129 | updateBitmap(shade);
130 | if (!thumbIsVisible) {
131 | thumbIsVisible=true;
132 | }
133 | int pixel = bitty.getPixel(xp, yp);
134 | thumb.getPaint().setColor(pixel);
135 | return pixel;
136 | }
137 |
138 | // sets the colorPicker's color to a certain r,g,b indicated by x,y,z
139 | public void setColor(int red, int green, int blue) {
140 | xp = red;
141 | yp = green;
142 | moveThumb((int)(xp*factor+paddingx),(int)(yp*factor+paddingy));
143 | }
144 |
145 |
146 | // creates the thumbnail viewer
147 | private void createThumb(int x, int y, int color) {
148 | thumb = new ShapeDrawable(new OvalShape());
149 | thumb.getPaint().setColor(color);
150 |
151 | thumb2 = new ShapeDrawable(new OvalShape());
152 | thumb2.getPaint().setColor(0x55FFFFFF);
153 | thumb2.getPaint().setStyle(Paint.Style.STROKE);
154 | thumb2.getPaint().setStrokeWidth(5);
155 |
156 | thumb.getPaint().setAntiAlias(true);
157 | thumb2.getPaint().setAntiAlias(true);
158 | }
159 |
160 | // updates the bitmap based on a blue factor
161 | private void updateBitmap(int k) {
162 | for (int i = 0; i < bwidth; i++) {
163 | for (int j = 0; j < bheight; j++) {
164 | pixels[i*bheight+j] = Color.rgb(j, i, k);
165 | }
166 | }
167 | kPrev = k;
168 | bitty.setPixels(pixels, 0, bwidth, 0, 0, bwidth, bheight);
169 | }
170 | }
--------------------------------------------------------------------------------
/src/com/trevorshp/arduinocolor/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.trevorshp.arduinocolor;
2 |
3 |
4 | import java.io.IOException;
5 |
6 | import android.content.Context;
7 | import android.graphics.Color;
8 | import android.hardware.usb.UsbManager;
9 | import android.os.Bundle;
10 | import android.support.v4.app.FragmentActivity;
11 | import android.util.DisplayMetrics;
12 | import android.util.Log;
13 | import android.view.MotionEvent;
14 | import android.view.View;
15 | import android.view.View.OnTouchListener;
16 | import android.widget.LinearLayout;
17 | import android.widget.SeekBar;
18 | import android.widget.SeekBar.OnSeekBarChangeListener;
19 | import android.widget.TextView;
20 |
21 | import com.hoho.android.usbserial.driver.UsbSerialDriver;
22 | import com.hoho.android.usbserial.driver.UsbSerialProber;
23 |
24 |
25 |
26 | public class MainActivity extends FragmentActivity implements OnTouchListener, OnSeekBarChangeListener {
27 | public final static String TAG = "AndroidColor";
28 | public ColorPickerView colorPicker;
29 | private TextView text1;
30 | private static final int blueStart = 100;
31 |
32 | private UsbManager usbManager;
33 | private UsbSerialDriver device;
34 |
35 | @Override
36 | protected void onCreate(Bundle savedInstanceState) {
37 | super.onCreate(savedInstanceState);
38 | setContentView(R.layout.activity_main);
39 | LinearLayout layout = (LinearLayout) findViewById(R.id.color_picker_layout);
40 | final int width = layout.getWidth();
41 | //get the display density
42 | DisplayMetrics metrics = new DisplayMetrics();
43 | getWindowManager().getDefaultDisplay().getMetrics(metrics);
44 | colorPicker = new ColorPickerView(this,blueStart,metrics.densityDpi);
45 | layout.setMinimumHeight(width);
46 | layout.addView(colorPicker);
47 | layout.setOnTouchListener(this);
48 |
49 | text1 = (TextView) findViewById(R.id.result1_textview);
50 | text1.setText("Tap a color!");
51 |
52 | SeekBar seek = (SeekBar) findViewById(R.id.seekBar1);
53 | seek.setProgress(blueStart);
54 | seek.setMax(255);
55 | seek.setOnSeekBarChangeListener(this);
56 |
57 | // Get UsbManager from Android.
58 | usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
59 | }
60 |
61 | @Override
62 | protected void onPause() {
63 | super.onPause();
64 | //check if the device is already closed
65 | if (device != null) {
66 | try {
67 | device.close();
68 | } catch (IOException e) {
69 | //we couldn't close the device, but there's nothing we can do about it!
70 | }
71 | //remove the reference to the device
72 | device = null;
73 | }
74 | }
75 |
76 | @Override
77 | protected void onResume() {
78 | super.onResume();
79 | //get a USB to Serial device object
80 | device = UsbSerialProber.acquire(usbManager);
81 | if (device == null) {
82 | //there is no device connected!
83 | Log.d(TAG, "No USB serial device connected.");
84 | } else {
85 | try {
86 | //open the device
87 | device.open();
88 | //set the communication speed
89 | device.setBaudRate(115200); //make sure this matches your device's setting!
90 | } catch (IOException err) {
91 | Log.e(TAG, "Error setting up USB device: " + err.getMessage(), err);
92 | try {
93 | //something failed, so try closing the device
94 | device.close();
95 | } catch (IOException err2) {
96 | //couldn't close, but there's nothing more to do!
97 | }
98 | device = null;
99 | return;
100 | }
101 | }
102 | }
103 |
104 | @Override
105 | public void onDestroy() {
106 | super.onDestroy();
107 | }
108 |
109 | // sends color data to a Serial device as {R, G, B, 0x0A}
110 | private void sendToArduino(int color){
111 | byte[] dataToSend = {(byte)Color.red(color),(byte)Color.green(color),(byte)Color.blue(color), 0x0A};
112 | //remove spurious line endings from color bytes so the serial device doesn't get confused
113 | for (int i=0; i 150) return false;
146 | return true;
147 | }
148 |
149 | @Override
150 | //called when the user touches the color palette
151 | public boolean onTouch(View view, MotionEvent event) {
152 | int color = 0;
153 | color = colorPicker.getColor(event.getX(),event.getY(),true);
154 | colorPicker.invalidate();
155 | //re-draw the selected colors text
156 | updateTextAreas(color);
157 | //send data to arduino
158 | sendToArduino(color);
159 | return true;
160 | }
161 |
162 | @Override
163 | public void onProgressChanged(SeekBar seek, int progress, boolean fromUser) {
164 | int amt = seek.getProgress();
165 | int col = colorPicker.updateShade(amt);
166 | updateTextAreas(col);
167 | sendToArduino(col);
168 | colorPicker.invalidate();
169 | }
170 |
171 | @Override
172 | public void onStartTrackingTouch(SeekBar arg0) {
173 |
174 | }
175 |
176 | @Override
177 | public void onStopTrackingTouch(SeekBar arg0) {
178 |
179 | }
180 |
181 | // generate a random hex color & display it
182 | public void randomColor(View v) {
183 | int z = (int) (Math.random()*255);
184 | int x = (int) (Math.random()*255);
185 | int y = (int) (Math.random()*255);
186 | colorPicker.setColor(x,y,z);
187 | SeekBar seek = (SeekBar) findViewById(R.id.seekBar1);
188 | seek.setProgress(z);
189 | }
190 | }
191 |
--------------------------------------------------------------------------------