forked from sheabunge/LockCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path! Startup.nxc
178 lines (165 loc) · 5.04 KB
/
! Startup.nxc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* LockCode 1.0.2
*
* Rename to "! Startup.nxc" and download to protect your NXT at startup.
* When NXT is turned on it will prompt for a code.
* The first time the program is run it will ask you to set a code.
* If you enter the wrong code, the NXT will flash a message, otherwise,
* the program will end, allowing you to access your NXT.
* The dark grey button will turn off the NXT instead of ending the program.
* If you forget the code and want to set a new one, simply delete the file
* 'LockCode.dat' from the NXT using something like NeXT Explorer or NeXT Commander.
*
* Created by 'bungeshea' on 10th October 2012
* The NXT Generation Robotics - http://robotics.bungeshea.com
*/
#define FILE "LockCode.dat" // Name of the file to store the code in
#define FILESIZE 8
#define CLEAR_SCREEN DRAW_OPT_CLEAR_WHOLE_SCREEN
/* Define the LockCode data type. It will hold four chars */
struct LockCode{
char a;
char b;
char c;
char d;
};
LockCode code;
/**
* This function allows the user to
* enter single number using the NXT buttons
*
* @return int value
*/
int GetNum(int x, int y = LCD_LINE5, int value = 0) {
while(!ButtonPressed(BTNCENTER,0)) // Poll for button presses until the center button is pressed
{
NumOut(x, y, value); // Draw the number on the screen
RectOut(x-3, y-2, 10, 12); // Draw a rectangle around the number
if(ButtonPressed(BTNRIGHT,0)) // If the user presses the right arrow...
{
while(ButtonPressed(BTNRIGHT,0)); // Wait for the button to be released
value++; // Increase the value
}
if(ButtonPressed(BTNLEFT,0)) // If the user presses the left arrow...
{
while(ButtonPressed(BTNLEFT,0)); // Wait for the button to be released
value--; // Decrease the value
}
if (value < 0) value = 9; // If the value is below 0, set it to 9
if (value > 9) value = 0; // If the value is above 9, set it to 0
}
while(ButtonPressed(BTNCENTER,0)); // Wait for the center button to be released
RectOut(x-3, y-2, 10, 12, DRAW_OPT_CLEAR); // Clear the rectangle
return value;
}
/**
* This function allows the user to
* enter a four-digit passcode
* using the NXT buttons
*
* @return LockCode code
*/
LockCode GetCode()
{
LockCode code;
// Draw placeholder numbers on the screen
NumOut(15, LCD_LINE5, 0);
NumOut(35, LCD_LINE5, 0);
NumOut(55, LCD_LINE5, 0);
NumOut(75, LCD_LINE5, 0);
// Prompt the user for each digit of the code
code.a = GetNum(15);
code.b = GetNum(35);
code.c = GetNum(55);
code.d = GetNum(75);
return code;
}
/**
* This method will write the
* code variable to the NXT's memory
*/
void WriteCode()
{
byte handle; unsigned int result, fsize;
DeleteFile(FILE); // Delete the existing file
CreateFile(FILE, FILESIZE, handle); // Create a new file
Write(handle, code); // Write the code to the file
CloseFile(handle); // Close the file
}
/**
* This method will read the passcode
* from the NXT's internal memory and store it
* in a LockCode varaible.
*/
void ReadCode()
{
byte handle; unsigned int result, fsize;
result = OpenFileRead(FILE, fsize, handle); // Attempt to open the file
if(result == LDR_SUCCESS) // If file exists...
{
Read(handle, code); // Read the data to the Code variable
CloseFile(handle); // And close the file
}
else // If the file does not exist, prompt to create a new code
{
TextOut(12, LCD_LINE2, "Set passcode:", CLEAR_SCREEN); // Show a message
code = GetCode(); // Prompt the user to enter a new code
WriteCode(); // Store the code in a file
}
}
/**
* This task will stop the dark grey button
* from stopping the program, and instead
* use it to turn off the NXT
*/
task turnOff()
{
// only pass this point if the NXT is running the enhanced firmware
#ifdef __ENHANCED_FIRMWARE__
SetAbortFlag(BTNSTATE_NONE); // disable the exit button
Wait(SEC_10); // wait 10 seconds to prevent the NXT from locking up
while(true)
{
if(ButtonPressed(BTNEXIT, 0)) // if the exit button is pressed...
ForceOff(true); // turn off the NXT
}
#endif
}
/**
* This task will handle the actual entering and validating
* the code part of our program, and display the on-screen prompts
*/
task lockCode()
{
ReadCode(); // read the code stored in the NXT's memory
while(true) // keep on prompting the user to enter a code
{
// show a message
TextOut(7, LCD_LINE2, "Enter passcode:", CLEAR_SCREEN);
// wait a bit to prevent accidental button presses
Wait(10);
// prompt the user to enter a code
LockCode input = GetCode();
// If the code that we've just entered matches the one stored on file...
if(input == code) {
Stop(true); // stop the program, allowing the user to access the NXT
}
else {
// Else, flash some text and try again
TextOut(7, LCD_LINE2, "Wrong passcode!");
Wait(1500);
}
}
}
/**
* The main task for our program
* This will run when the program starts.
* It will play a startup sound file at the volume
* specified in the Settings menu on the NXT
* and then start the lockCode and turnOff tasks
*/
task main()
{
PlayFileEx("! Startup.rso", Volume(), 0); // Play the startup sound
Precedes(turnOff, lockCode);
}