source: rtos_arduino/trunk/arduino_lib/libraries/Robot_Control/examples/explore/R09_Picture_Browser/R09_Picture_Browser.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 3.9 KB
Line 
1/* Picture Browser
2
3 You can make your own gallery/picture show with the
4 Robot. Put some pictures on the SD card, start the
5 sketch, they will diplay on the screen.
6
7 Use the left/right buttons to navigate through the
8 previous and next images.
9
10 Press up or down to enter a mode where you change
11 the pictures by rotating the robot.
12
13 You can add your own pictures onto the SD card, and
14 view them in the Robot's gallery!
15
16 Pictures must be uncompressed BMP, 24-bit color depth,
17 160 pixels wide, and 128 pixels tall.
18
19 They should be named as "picN.bmp". Replace 'N' with a
20 number between 0 and 9.
21
22 The current code only supports 10 pictures. How would you
23 improve it to handle more?
24
25 Circuit:
26 * Arduino Robot
27
28 created 1 May 2013
29 by X. Yang
30 modified 12 May 2013
31 by D. Cuartielles
32
33 This example is in the public domain
34 */
35
36#include <ArduinoRobot.h> // include the robot library
37#include <Wire.h>
38#include <SPI.h>
39
40const int NUM_PICS = 4; //Total number of pictures in Gallery
41
42// name the modes
43const int CONTROL_MODE_KEY = 0;
44const int CONTROL_MODE_COMPASS = 1;
45
46char buffer[] = "pic1.bmp"; // current file name
47int i = 1; // Current gallery sequence counter
48int mode = 0; // Current mode
49
50// text to display on screen
51char modeNames[][9] = { "keyboard", "tilt " };
52
53void setup() {
54 // initialize the Robot, SD card, display, and speaker
55 Robot.beginSD();
56 Robot.beginTFT();
57 Robot.begin();
58
59 // draw "lg0.bmp" and "lg1.bmp" on the screen
60 Robot.displayLogos();
61
62 // draw init3.bmp from the SD card on the screen
63 Robot.drawBMP("init3.bmp", 0, 0);
64
65 // display instructions
66 Robot.stroke(0, 0, 0);
67 Robot.text("The gallery\n\n has 2 modes, in\n keyboard mode, L/R\n key for switching\n pictures, U/D key\n for changing modes", 5, 5);
68 delay(6000);
69 Robot.clearScreen();
70 Robot.drawBMP("pb.bmp", 0, 0);
71 Robot.text("In tilt mode,\n quickly tilt the\n robot to switch\n pictures", 5, 5);
72 delay(4000);
73}
74
75void loop() {
76 buffer[3] = '0' + i; // change filename of the img to be displayed
77 Robot.drawBMP(buffer, 0, 0); // draw the file on the screen
78 // change control modes
79 switch (mode) {
80 case CONTROL_MODE_COMPASS:
81 compassControl(3);
82 break;
83 case CONTROL_MODE_KEY:
84 keyboardControl();
85 break;
86 }
87 delay(200);
88}
89
90void keyboardControl() {
91 //Use buttons to control the gallery
92 while (true) {
93 int keyPressed = Robot.keyboardRead(); // read the button values
94 switch (keyPressed) {
95 case BUTTON_LEFT: // display previous picture
96 if (--i < 1) i = NUM_PICS;
97 return;
98 case BUTTON_MIDDLE: // do nothing
99 case BUTTON_RIGHT: // display next picture
100 if (++i > NUM_PICS) i = 1;
101 return;
102 case BUTTON_UP: // change mode
103 changeMode(-1);
104 return;
105 case BUTTON_DOWN: // change mode
106 changeMode(1);
107 return;
108 }
109 }
110}
111
112// if controlling by the compass
113void compassControl(int change) {
114 // Rotate the robot to change the pictures
115 while (true) {
116 // read the value of the compass
117 int oldV = Robot.compassRead();
118
119 //get the change of angle
120 int diff = Robot.compassRead() - oldV;
121 if (diff > 180) diff -= 360;
122 else if (diff < -180) diff += 360;
123
124 if (abs(diff) > change) {
125 if (++i > NUM_PICS) i = 1;
126 return;
127 }
128
129 // chage modes, if buttons are pressed
130 int keyPressed = Robot.keyboardRead();
131 switch (keyPressed) {
132 case BUTTON_UP:
133 changeMode(-1);
134 return;
135 case BUTTON_DOWN:
136 changeMode(1);
137 return;
138 }
139 delay(10);
140 }
141}
142
143// Change the control mode and display it on the LCD
144void changeMode(int changeDir) {
145 // alternate modes
146 mode += changeDir;
147 if (mode < 0) {
148 mode = 1;
149 } else if (mode > 1)
150 mode = 0;
151
152 // display the mode on screen
153 Robot.fill(255, 255, 255);
154 Robot.stroke(255, 255, 255);
155 Robot.rect(0, 0, 128, 12);
156 Robot.stroke(0, 0, 0);
157 Robot.text("Control:", 2, 2);
158 Robot.text(modeNames[mode], 52, 2);
159 delay(1000);
160}
161
Note: See TracBrowser for help on using the repository browser.