-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_autotune.c
More file actions
169 lines (152 loc) · 5.08 KB
/
function_autotune.c
File metadata and controls
169 lines (152 loc) · 5.08 KB
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
/*
* function_autotune.c
*
* Created: 13/03/2016 23:30:53
* Author: Tyler
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdio.h>
#include "hardware.h"
#include "peripherals.h"
#include "error_codes.h"
#include "error_buffer.h"
#include "driver_usb_uart.h"
#include "string_decoder.h"
#include "stream_processor.h"
#include "function_autotune.h"
#include "usb_console.h"
#include "head_codes.h"
#include "body_codes.h"
#include "button_control.h"
void AutotuneFunctionHandler (void* command)
{
console_status_t *command_status = (console_status_t*) command;
char* cmdptr; //create cmdptr that can pass through the command string
cmdptr=command_status->command_string; //point cmdptr at the start of the string
cmdptr+=command_status->command_arg_marker; //advance the string pointer to start of arguments
if(*cmdptr==' ') //check if we have arguments
{
cmdptr++; //advance onwards
}
else
{
DriverUSBUartPutString("No frequency provided\r\n");
return;
}
int Mhz;
int Khz;
if(!AutotuneProcessFrequency(cmdptr,&Mhz,&Khz))
{
char str[16];
sprintf(str,"FREQ= %d.%d\r\n",Mhz,Khz);
DriverUSBUartPutString(str);
}
else
{
DriverUSBUartPutString("Invalid Frequency Format\r\n");
return;
}
char memory[6]; //store memory location
decode_string(bodyInput.array,LEFT_MEMORY,memory);
if(!strncmp(" ",memory,3)) //check if in memory mode
{
DriverUSBUartPutString("not in memory mode\r\n");
}
else
{
DriverUSBUartPutString("in memory mode\r\n");
ButtonControlPress((head_button_t){KEY1_LOC,KEY1_VM,0}); //go to vfo mode
}
int digit1=(Mhz/100)%10;
int digit2=(Mhz/10)%10;
int digit3=(Mhz)%10;
int digit4=(Khz/100)%10;
int digit5=(Khz/10)%10;
int digit6=(Khz)%10;
//type in frequency
ButtonControlPress2((head_button_t){MIC_48_SW2_LOC,KEYPAD_MAP[digit1][1],0},(head_button_t){MIC_48_SW1_LOC,KEYPAD_MAP[digit1][0],0});
ButtonControlPress2((head_button_t){MIC_48_SW2_LOC,KEYPAD_MAP[digit2][1],0},(head_button_t){MIC_48_SW1_LOC,KEYPAD_MAP[digit2][0],0});
ButtonControlPress2((head_button_t){MIC_48_SW2_LOC,KEYPAD_MAP[digit3][1],0},(head_button_t){MIC_48_SW1_LOC,KEYPAD_MAP[digit3][0],0});
ButtonControlPress2((head_button_t){MIC_48_SW2_LOC,KEYPAD_MAP[digit4][1],0},(head_button_t){MIC_48_SW1_LOC,KEYPAD_MAP[digit4][0],0});
ButtonControlPress2((head_button_t){MIC_48_SW2_LOC,KEYPAD_MAP[digit5][1],0},(head_button_t){MIC_48_SW1_LOC,KEYPAD_MAP[digit5][0],0});
ButtonControlPress2((head_button_t){MIC_48_SW2_LOC,KEYPAD_MAP[digit6][1],0},(head_button_t){MIC_48_SW1_LOC,KEYPAD_MAP[digit6][0],0});
}
/*!
* Extracts a frequency in the form Mhz.Khz from a character string.
*
* \param frequency character pointer to the string containing the frequency to be decoded
* \param Mhz in pointer to store decoded Mhz element of the frequency
* \param Khz in pointer to store decoded Khz element of the frequency
* \retval
*/
int AutotuneProcessFrequency(char* frequency,int* Mhz,int* Khz)
{
*Mhz =0; //clear input frequencies
*Khz =0;
uint8_t KhzDigits=0; //need to receive at least 3 digits
int FrequencyProcessState=0; //state of processing
while(FrequencyProcessState!=3) //wait until frequency has been extracted
{
switch(FrequencyProcessState)
{
//skip initial spaces if provided
case 0:
if (*frequency=='\0') //check if we have a null pointer
{
return(ERR_AUTOTUNE_FREQUENCY_NULL_POINTER); //report that we have a null string
}
if (*frequency!=' ') //check that we have received a character other than space
{
FrequencyProcessState=1; //move to Mhz processing
continue; //we still want to remain on this character
}
break;
//decode mhz element
case 1:
if (*frequency>='0'&&*frequency<='9') //if we have been provided a number
{
*Mhz *= 10; //shift the current number up
*Mhz += *frequency-'0'; //add the new number to the Mhz element
}
else if(*frequency=='.' && *Mhz>0) //mhz separator received and a Mhz value has been provided
{
FrequencyProcessState=2; //move onto Khz processing
}
else
{
return(ERR_AUTOTUNE_FREQUENCY_INVALID); //not expecting any other characters here
}
break;
case 2:
if (*frequency>='0'&&*frequency<='9') //if we have been provided a number
{
*Khz *= 10; //shift the current number up
*Khz += *frequency-'0'; //add the new number to the Khz element
KhzDigits+=1; //increment digit count
if (KhzDigits==3) //check if we have received enough digits
{
return(SUCCESS); //we are done
}
}
else if(*frequency==' ' || *frequency=='\0') //have received and end separator so finished processing
{
while(KhzDigits<3)
{
*Khz *= 10; //shift by non provided digits
KhzDigits++;
}
return(SUCCESS);
}
else
{
return(ERR_AUTOTUNE_FREQUENCY_INVALID); //not expecting other characters here
}
break;
}
frequency++; //advance to next character
}
//todo might want a better error here
return(ERR_AUTOTUNE_FREQUENCY_INVALID); //frequency extraction completed
}