RT-AICHIP-sample
UserInterface.c
[詳解]
1 
9 #include "LPC13xx.h"
10 #include "core_cm3.h"
11 #include "timer.h"
12 #include "type.h"
13 #include "pwm.h"
14 #include "UserInterface.h"
15 #include "ad.h"
16 
17 //LEDが点滅状態か常時点灯状態かのフラグ 1:点滅  0:常時点灯(消灯も含む)
18 volatile static uint8_t flag_flash_redLED = 0;
19 volatile static uint8_t flag_flash_greenLED = 0;
20 //SWの押下状態のカウント
21 volatile static uint16_t leftSW_count = 0;
22 volatile static uint16_t rightSW_count = 0;
23 //LEDの点滅用カウント
24 volatile static uint16_t redLED_count = 0;
25 volatile static uint16_t greenLED_count = 0;
26 //経過時間のカウント
27 volatile static uint32_t elapsed_time_count = 0;
28 //LEDの点滅時間のon/off時間のカウント
29 volatile static uint16_t on_count_redLED = 0;
30 volatile static uint16_t off_count_redLED = 1;
31 volatile static uint16_t on_count_greenLED = 0;
32 volatile static uint16_t off_count_greenLED = 1;
33 //電池の電圧値[V]
34 volatile static float voltageLipo = 0.0;
35 volatile static float voltageMotor = 0.0;
36 
37 
58 void initUI(void)
59 {
61 }
62 
63 /*----------------------------------------------------------------------------
64  Input void
65  Output state 1:on 0:off
66 
67  SW(Left)の状態取得
68  *---------------------------------------------------------------------------*/
70 {
71  uint32_t state;
72  state = LPC_GPIO2->DATA & 0x1;
73  state = 1 - state;
74  return state;
75 }
76 
77 /*----------------------------------------------------------------------------
78  Input void
79  Output state 1:on 0:off
80 
81  SW(Right)の状態取得
82  *---------------------------------------------------------------------------*/
84 {
85  uint32_t state;
86  state = (LPC_GPIO0->DATA & 0x2)>>1;
87  state = 1 - state;
88  return state;
89 }
90 
91 /*----------------------------------------------------------------------------
92  Input void
93  Output count
94 
95  SW(Left)を押下している時間を返す.
96  count * 1mSecの間SWは押下
97  *---------------------------------------------------------------------------*/
99 {
100  return leftSW_count;
101 }
102 
103 /*----------------------------------------------------------------------------
104  Input void
105  Output state 1:on 0:off
106 
107  SW(Right)を押下している時間を返す.
108  count * 1mSecの間SWは押下
109 
110  *---------------------------------------------------------------------------*/
112 {
113  return rightSW_count;
114 }
115 
116 /*----------------------------------------------------------------------------
117  Input void
118  Output elapsed_time_count
119  initUI()を呼んでからの経過時間を返す.
120  elapsed_time_count * 1msec
121  *---------------------------------------------------------------------------*/
123 {
124  return elapsed_time_count;
125 }
126 
127 /*----------------------------------------------------------------------------
128  Input void
129  Output void
130  elapsed_time_countをリセットする.
131  *---------------------------------------------------------------------------*/
133 {
134  elapsed_time_count = 0;
135 }
136 
137 
138 /*----------------------------------------------------------------------------
139  Input state 1:on 0:off
140  Output void
141 
142  緑LEDの点灯と消灯 LED1
143  *---------------------------------------------------------------------------*/
144 static void setStateGreenLED(uint8_t state)
145 {
146  if(state == 1)
147  {
148  LPC_GPIO1->DATA |= 0x0100; //LED1 on
149  }
150  else
151  {
152  LPC_GPIO1->DATA &= ~0x0100; //LED1 off
153  }
154 }
155 
156 /*----------------------------------------------------------------------------
157  Input state 1:on 0:off
158  Output void
159 
160  赤LEDの点灯と消灯 LED2
161  *---------------------------------------------------------------------------*/
162 static void setStateRedLED(uint8_t state)
163 {
164  if(state == 1)
165  {
166  LPC_GPIO1->DATA |= 0x0008; //LED2 on
167  }
168  else
169  {
170  LPC_GPIO1->DATA &= ~0x0008; //LED2 off
171  }
172 }
173 
174 /*----------------------------------------------------------------------------
175  Input state 1:on 0:off
176  Output void
177 
178  赤LEDの点灯と消灯. LEDが点滅状態で
179  呼んだ場合は常時点灯(消灯)状態になる.
180  *---------------------------------------------------------------------------*/
181 void turnRedLED(uint8_t state)
182 {
183  flag_flash_redLED = 0;
184  setStateRedLED(state);
185 }
186 
187 /*----------------------------------------------------------------------------
188  Input state 1:on 0:off
189  Output void
190 
191  緑LEDの点灯と消灯. LEDが点滅状態で
192  呼んだ場合は常時点灯(消灯)状態になる.
193  *---------------------------------------------------------------------------*/
194 
196 {
197  flag_flash_greenLED = 0;
198  setStateGreenLED(state);
199 }
200 
201 
202 
203 /*----------------------------------------------------------------------------
204  Input on_count msec
205  off_count msec
206  Output void
207 
208  緑LEDを指定した時間間隔で点滅させる
209  例.LEDを100msecの間on, 50msecの間offで点滅させるには
210  on_count = 100, off_count = 50
211  LEDを常時点灯する場合は
212  turnGreenLED(1)
213  LEDを消灯する場合は
214  turnGreenLED(0)
215  *---------------------------------------------------------------------------*/
216 void flashGreenLED(uint16_t on_count, uint16_t off_count)
217 {
218  flag_flash_greenLED = 1;
219  on_count_greenLED = on_count;
220  off_count_greenLED = off_count;
221 }
222 
223 /*----------------------------------------------------------------------------
224  Input on_count msec
225  off_count msec
226  Output void
227 
228  赤LEDを指定した時間間隔で点滅させる
229  例.LEDを100msecの間on, 50msecの間offで点滅させるには
230  on_count = 100, off_count = 50
231 
232  LEDを常時点灯する場合は
233  turnRedLED(1)
234  LEDを消灯する場合は
235  turnRedLED(0)
236  *---------------------------------------------------------------------------*/
237 void flashRedLED(uint16_t on_count, uint16_t off_count)
238 {
239  flag_flash_redLED = 1;
240  on_count_redLED = on_count;
241  off_count_redLED = off_count;
242 }
243 
244 /*----------------------------------------------------------------------------
245  Input void
246  Output Voltage_Lipo
247 
248  Lipoバッテリーの電圧取得
249  *---------------------------------------------------------------------------*/
250 float getLipoVoltage(void)
251 {
252  return voltageLipo;
253 }
254 
255 /*----------------------------------------------------------------------------
256  Input void
257  Output Voltage_MOT
258 
259  モーターバッテリーの電圧取得
260  *---------------------------------------------------------------------------*/
261 float getMotorVoltage(void)
262 {
263  return voltageMotor;
264 }
265 
266 /*----------------------------------------------------------------------------
267  Input void
268  Output void
269 
270   32bitタイマー0の割り込み関数
271  各種countのインクリメントとAD変換を実行
272  *---------------------------------------------------------------------------*/
274 {
275  int16_t ad_val[8];
276  LPC_TMR32B0->IR=0x08; //clear interrupt flag
277 
278  if(flag_flash_redLED == 1)
279  {
280  //赤LEDの点滅
281  if(redLED_count < on_count_redLED ) setStateRedLED(1);
282  else setStateRedLED(0);
283  if( (on_count_redLED + off_count_redLED) < redLED_count ) redLED_count = 0;
284  redLED_count ++;
285  }
286 
287  //緑LEDの点滅
288  if(flag_flash_greenLED == 1)
289  {
290  if(greenLED_count < on_count_greenLED ) setStateGreenLED(1);
291  else setStateGreenLED(0);
292  if( (on_count_greenLED + off_count_greenLED) < greenLED_count ) greenLED_count = 0;
293  greenLED_count ++;
294  }
295  //時間経過
296  elapsed_time_count ++;
297  //左スイッチの押下
298  if(getStateLeftSW() == 1) leftSW_count ++;
299  else leftSW_count = 0;
300 
301  //右スイッチの押下
302  if(getStateRightSW() == 1) rightSW_count ++;
303  else rightSW_count = 0;
304 
305  //AD変換と電池の電圧計算
306  storeAD2Array(&ad_val[0]);
307  voltageLipo = (float)(ad_val[1])/1024.0 * 3.0 * 2.0;
308  voltageMotor = (float)(ad_val[0])/1024.0 * 3.0 * 2.0;
309 
310 }
311 
312 
313 /*----------------------------------------------------------------------------
314  Input void
315  Output void
316 
317   UI関係の関数の出力値をUARTで表示
318  *---------------------------------------------------------------------------*/
319 void debugUI(void)
320 {
321  myPrintfUART("#######debug UI####### \n");
322  myPrintfUART("RSW state %d \n", getStateRightSW());
323  myPrintfUART("RSW count %d msec \n", getRightSWcount());
324  myPrintfUART("LSW state %d \n", getStateLeftSW());
325  myPrintfUART("LSW count %d msec \n", getLeftSWcount());
326  myPrintfUART("Elap Time %d msec \n", getElapsedTime());
327  myPrintfUART("V Motor %f V \n", getMotorVoltage());
328  myPrintfUART("V Lipo %f V \n", getLipoVoltage());
329 }
330 
331 /******************************************************************************
332 ** End Of File
333 ******************************************************************************/
uint8_t getStateLeftSW(void)
Definition: UserInterface.c:69
float getLipoVoltage(void)
float getMotorVoltage(void)
void resetElapsedTime(void)
int myPrintfUART(const char *fmt,...)
Definition: debug.c:42
void flashGreenLED(uint16_t on_count, uint16_t off_count)
CMSIS Cortex-M3 Core Peripheral Access Layer Header File.
void turnRedLED(uint8_t state)
uint8_t getStateRightSW(void)
Definition: UserInterface.c:83
uint16_t getRightSWcount(void)
void turnGreenLED(uint8_t state)
unsigned char uint8_t
Definition: type.h:27
void TIMER32_0_IRQHandler(void)
void flashRedLED(uint16_t on_count, uint16_t off_count)
uint32_t getElapsedTime(void)
void startTimer32_0(void)
Definition: timer.c:35
void storeAD2Array(int16_t *ad_value)
Definition: ad.c:70
signed short int int16_t
Definition: type.h:22
unsigned short int uint16_t
Definition: type.h:28
void initUI(void)
Definition: UserInterface.c:58
void debugUI(void)
uint16_t getLeftSWcount(void)
Definition: UserInterface.c:98
unsigned int uint32_t
Definition: type.h:29