summaryrefslogtreecommitdiffstats
path: root/src/zephyr.c
blob: 109742f7e0ce4c9fe21ea0da8eff48dd52817626 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <zephyr.h>
#include <main.h>

#define PI =3.14159

#include <string.h>

#ifdef LCD_PORT

static void (*_lcdUpdateFunc)(void *);
static char lcdBuffer[2][16];

void zLCDHandler(void *unused_param){
	while(1){
		lcdSetText(LCD_PORT,1,lcdBuffer[0]);
		lcdSetText(LCD_PORT,2,lcdBuffer[1]);

		if(_lcdUpdateFunc)
			_lcdUpdateFunc(unused_param);

		delay(LCD_RATE);
	}
}

void zLCDInit(void){
	lcdInit(LCD_PORT);
	lcdClear(LCD_PORT);
	lcdSetBacklight(LCD_PORT,true);
}

void zLCDStart(void){
	taskCreate(zLCDHandler,
			   TASK_DEFAULT_STACK_SIZE,
			   NULL,
			   TASK_PRIORITY_DEFAULT
			   );

	memset(&lcdBuffer,0,32);
	strcpy(lcdBuffer[0]," libZEPHYR v1.0 ");
}

void zLCDWrite(unsigned char line,const char *text,...){
	va_list args;
	char buf[16];
	va_start(args,text);
	sprintf(buf,text,args);
	va_end(args);
	strcpy(lcdBuffer[line-1],buf);
}

void zLCDSetUpdateFunc(void (*func)(void *)){
	_lcdUpdateFunc = func;
}

void zLCDClearUpdateFunc(void){
	_lcdUpdateFunc = 0;
}

#endif // LCD_PORT

#ifdef GYRO_PORT

static Gyro gyro;
static bool gyroEnabled = false;

void zGyroInit(void){
	if((gyro=gyroInit(2,0))){
		gyroEnabled = true;
	}
}

#endif // GYRO_PORT

/*
 *	A map of what's plugged into the motor ports.
 *	Expected declarations:
 *
 *	DRIVEL	-	Left drive port
 *	DRIVER	-	Right drive port
 *
*/

#define MOTOR_PORT_COUNT	10

#ifdef IME_ENABLE
#define MOTOR_IME_COUNT		7
#endif // IME_ENABLE

const char *MOTOR_PORT_MAP[MOTOR_PORT_COUNT] = {
	"Left cannon",
	"Right cannon",
	"Misc",
	"Port 4",
	"Intake",
	"Right drive",
	"Left drive",
	"Lift 1",
	"Lift 2",
	"Rotater"
};

#ifdef IME_ENABLE

const char *MOTOR_IME_MAP[MOTOR_IME_COUNT] = {
	"Right drive",
	"Left drive",
	"Rotater",
	"Lift 1",
	"Lift 2",
	"Left cannon",
	"Right cannon"
};

static unsigned int imeCount = 0;

void zIMEInit(void){
	imeCount = imeInitializeAll();
}

#endif // IME_ENABLE

void zMotorSet(const char *motor,char speed){
	for(unsigned char i=0;i<MOTOR_PORT_COUNT;i++){
		if(!strcmp(MOTOR_PORT_MAP[i],motor)){
			motorSet(i+1,speed);
			return;
		}
	}
}

char zMotorGet(const char *motor){
	for(unsigned char i=0;i<MOTOR_PORT_COUNT;i++){
		if(!strcmp(MOTOR_PORT_MAP[i],motor)){
			return motorGet(i+1);
		}
	}
	return 0;
}

#ifdef IME_ENABLE

int zMotorIMEGet(const char *motor){
	int IMEValue = 0;
	for(unsigned char i=0;i<imeCount;i++){
		if(!strcmp(MOTOR_IME_MAP[i],motor)){
			imeGet(i,&IMEValue);
			break;
		}
	}
	return IMEValue;
}

int zMotorIMEGetVelocity(const char *motor){
	int IMEValue = 0;
	for(unsigned char i=0;i<imeCount;i++){
		if(!strcmp(MOTOR_IME_MAP[i],motor)){
			imeGetVelocity(i,&IMEValue);
			break;
		}
	}
	return IMEValue;
}

#endif // IME_ENABLE

void zDriveUpdate(void){

#ifdef DRIVE_NORMAL
	char s = joystickGetAnalog(DRIVE_JOY,DRIVE_NORMAL);

	APPLY_THRESH(s,DRIVE_THRESHOLD);

	motorSet(DRIVEL,s);
	motorSet(DRIVER,s);

#else

	char l,r;

	l = joystickGetAnalog(DRIVE_JOY,DRIVE_TANK_LEFT);
	r = joystickGetAnalog(DRIVE_JOY,DRIVE_TANK_RIGHT);

	APPLY_THRESH(l,DRIVE_THRESHOLD);
	APPLY_THRESH(r,DRIVE_THRESHOLD);

	zMotorSet("Left drive",l);
	zMotorSet("Right drive",r);

#endif // DRIVE_NORMAL

}

char zGetDigitalMotorSpeed(unsigned char joy,unsigned char btn_group,unsigned char btn_pos,unsigned char btn_neg,char speed){
	return joystickGetDigital(joy,btn_group,btn_pos) ?  speed :
		   joystickGetDigital(joy,btn_group,btn_neg) ? -speed : 0;
}