277 lines
7.3 KiB
C
277 lines
7.3 KiB
C
/**
|
|
******************************************************************************
|
|
* @file main.c
|
|
* @author MCD Application Team
|
|
* @version V4.0.0
|
|
* @date 21-January-2013
|
|
* @brief Custom HID demo main file
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
|
*
|
|
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
|
* You may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at:
|
|
*
|
|
* http://www.st.com/software_license_agreement_liberty_v2
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "hw_config.h"
|
|
#include "usb_lib.h"
|
|
#include "usb_pwr.h"
|
|
#include "hardware.h"
|
|
#include "key.h"
|
|
#include "caliper.h"
|
|
#include "systick.h"
|
|
#include <string.h>
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
typedef struct
|
|
{
|
|
uint8_t modifier;
|
|
uint8_t reserved;
|
|
uint8_t keycode[6];
|
|
} keyboard_report_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t len;
|
|
uint32_t total;
|
|
uint8_t data[32]
|
|
}debug_struct_t;
|
|
/* Private define ------------------------------------------------------------*/
|
|
#define PA3 (1<<3)
|
|
#define PA4 (1<<4)
|
|
#define BUF_LEN 32
|
|
/*przeliczenie wyniku z suwmiarki na mm
|
|
Jednostka danych to 1/1224.1 mm (wyznaczona doświadczalnie)
|
|
przeliczam na mm, z dokładnością do 0,01m:
|
|
|
|
*/
|
|
#define OFFSET -7680
|
|
#define SCALE_MUL 1000
|
|
#define SCALE_DIV 12241
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
keyboard_report_t KeyRep; //raport USB HID
|
|
uint8_t measure[BUF_LEN],ByteCntr; //dane z suwmiarki
|
|
debug_struct_t DebugStruct[128];
|
|
/* Extern variables ----------------------------------------------------------*/
|
|
__IO uint8_t PrevXferComplete = 1;
|
|
__IO uint32_t Tick;
|
|
extern __IO uint32_t bDeviceState;
|
|
|
|
/* Private function prototypes ----11-------------------------------------------*/
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
void Wait(uint32_t period)
|
|
{
|
|
uint32_t k;
|
|
|
|
k=Tick+period;
|
|
while(Tick<=k);
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : USB_AaciiToHid.
|
|
* Description : Konwertuje kod klawisza ASCII na HID.
|
|
*******************************************************************************/
|
|
|
|
uint8_t USB_AsciiToHid(uint8_t key)
|
|
{
|
|
|
|
if (key >= 'A' && key <= 'Z')
|
|
{
|
|
return 4 + key - 'A';
|
|
}
|
|
else if (key >= 'a' && key <= 'z')
|
|
{
|
|
return 4 + key - 'a';
|
|
}
|
|
else if (key >= '0' && key <= '9')
|
|
{
|
|
if (key == '0')
|
|
{
|
|
return 0x27;
|
|
}
|
|
else
|
|
{
|
|
return 30 + key - '1';
|
|
}
|
|
}
|
|
else if (key==10)
|
|
return 40;
|
|
|
|
return 0; // return nothing if not alpha-numeric
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : USB_Send.
|
|
* Description : Wysyła wynik pomiaru po USB.
|
|
*******************************************************************************/
|
|
|
|
void USB_SendText(uint8_t *text,uint8_t length)
|
|
{
|
|
uint32_t i,j,k;
|
|
for(i=0;i<length;i++)
|
|
{
|
|
KeyRep.keycode[0]=USB_AsciiToHid(text[i]);
|
|
USB_SIL_Write(EP1_IN,(uint8_t*)&KeyRep,8);
|
|
SetEPTxValid(ENDP1);
|
|
Wait(20);
|
|
KeyRep.keycode[0]=0;
|
|
USB_SIL_Write(EP1_IN,(uint8_t*)&KeyRep,8);
|
|
SetEPTxValid(ENDP1);
|
|
Wait(20);
|
|
}
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : main.
|
|
* Description : main routine.
|
|
* Input : None.
|
|
* Output : None.
|
|
* Return : None.
|
|
*******************************************************************************/
|
|
int main(void)
|
|
{
|
|
int32_t i,j,k,tmp,AbsValue,RelValue,ValueInMM;
|
|
uint8_t TextBuf[32];
|
|
uint8_t key_code,Caliper_DataValid;
|
|
GPIO_InitTypeDef GPIO_Conf;
|
|
|
|
//SysTick co 1ms
|
|
SYSTICK_Init(1);
|
|
NVIC_SetPriority (SysTick_IRQn,1); //systick na wyższy priorytet niż EXTI9_5_IRQHandler
|
|
|
|
//inicjalizacja USB
|
|
Set_System();
|
|
USB_Interrupts_Config();
|
|
Set_USBClock();
|
|
USB_Init();
|
|
// KeyRep - inicjalizacja
|
|
KeyRep.modifier = 0;
|
|
KeyRep.reserved = 0;
|
|
KeyRep.keycode[0] = 0;
|
|
KeyRep.keycode[1] = 0;
|
|
KeyRep.keycode[2] = 0;
|
|
KeyRep.keycode[3] = 0;
|
|
KeyRep.keycode[4] = 0;
|
|
KeyRep.keycode[5] = 0;
|
|
//teskt do wysyłania :)
|
|
sprintf((char*)TextBuf,"dupa\n");
|
|
|
|
//inicjalizacja Ledów
|
|
/* Zegar GPIOA */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
//LED1 LED2 wyjscie
|
|
GPIO_Conf.GPIO_Pin=(LED1|LED2);
|
|
GPIO_Conf.GPIO_Mode=GPIO_Mode_Out_PP;
|
|
GPIO_Conf.GPIO_Speed=GPIO_Speed_2MHz;
|
|
GPIO_Init(GPIOA,&GPIO_Conf);
|
|
|
|
//inicjalizacja przycisków
|
|
Key_Initialize();
|
|
|
|
//inicjalizacja suwmiarki
|
|
Caliper_Initialize();
|
|
|
|
//zmienne - wartości początkowe
|
|
i=0;
|
|
j=0;
|
|
Caliper_DataValid=0;
|
|
|
|
while (1)
|
|
{
|
|
//Sprawdź suwmiarke
|
|
if (Caliper_GetState()&CALIPER_CONNECTED)
|
|
{
|
|
//stan diody Suwmiarki - zapal
|
|
//GPIO_SetBits(GPIOA,LED2);
|
|
//odbierz dane
|
|
tmp=Caliper_GetAbsVal();
|
|
AbsValue=tmp;
|
|
tmp=Caliper_GetRelVal();
|
|
RelValue=tmp;
|
|
ValueInMM=-(AbsValue-OFFSET)*SCALE_MUL/SCALE_DIV;
|
|
//dane gotowe do wysłania
|
|
Caliper_DataValid=1;
|
|
}
|
|
else
|
|
{
|
|
//stan diody Suwmiarki - zgaś
|
|
//GPIO_ResetBits(GPIOA,LED2);
|
|
//prak danych
|
|
Caliper_DataValid=0;
|
|
}
|
|
|
|
|
|
//sprawdź klawiaturę
|
|
if(key_code=Key_Poll())
|
|
{
|
|
switch(key_code)
|
|
{
|
|
case KEY_MEAS:
|
|
//jeżeli USB jest podłączone, wyślij tekst
|
|
if ( (bDeviceState == CONFIGURED) && Caliper_DataValid)
|
|
{
|
|
USB_SendText(TextBuf,k);
|
|
}
|
|
break;
|
|
|
|
case KEY_2:
|
|
//brak funkcji, póki co
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
//Stan diody USB
|
|
if (bDeviceState == CONFIGURED)
|
|
GPIO_SetBits(GPIOA,LED1);
|
|
else
|
|
GPIO_ResetBits(GPIOA,LED1);
|
|
|
|
//czekaj do następnego taktu zegara
|
|
Wait(5);
|
|
}
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
/*******************************************************************************
|
|
* Function Name : assert_failed
|
|
* Description : Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* Input : - file: pointer to the source file name
|
|
* - line: assert_param error line source number
|
|
* Output : None
|
|
* Return : None
|
|
*******************************************************************************/
|
|
void assert_failed(uint8_t* file, uint32_t line)
|
|
{
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
|
|
/* Infinite loop */
|
|
while(1)
|
|
{
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|