Suwmiarka/Soft/Suwmiarka/main.c

248 lines
6.5 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>&copy; 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 "spi.h"
/* Private typedef -----------------------------------------------------------*/
typedef struct {
uint8_t modifier;
uint8_t reserved;
uint8_t keycode[6];
} keyboard_report_t;
/* Private define ------------------------------------------------------------*/
#define PA3 (1<<3)
#define PA4 (1<<4)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
keyboard_report_t KeyRep;
/* Extern variables ----------------------------------------------------------*/
__IO uint8_t PrevXferComplete = 1;
__IO uint32_t Tick;
extern __IO uint32_t bDeviceState;
/* Private function prototypes -----------------------------------------------*/
/* 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';
}
}
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,j=0;i<length;i++,j++)
{
//kopiuj kody klawiszy
if(j<6)
KeyRep.keycode[j]=USB_AsciiToKeycode(text[i]);
//mamy 6 kodów
else
{
//mamy komplet kodów, wyślij
USB_SIL_Write(EP1_IN,(uint8_t*)&KeyRep,8);
SetEPTxValid(ENDP1);
j=0;
Wait(10);
//klawisze puszczone
for(k=0;k<6;k++)
KeyRep.keycode[0]=0;
USB_SIL_Write(EP1_IN,(uint8_t*)&KeyRep,8);
SetEPTxValid(ENDP1);
Wait(10);
}
}
}
/*******************************************************************************
* Function Name : main.
* Description : main routine.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
int main(void)
{
int32_t i,k,length;
uint8_t TextBuf[32],measure[32];
GPIO_InitTypeDef GPIO_Conf;
//inicjalizacja USB
Set_System();
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
//SysTick co 1ms
SYSTICK_Init(1);
// 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;
//inicjalizacja Ledów
//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_Init();
//inicjalizacja SPI
SPI_Initilize();
//czekaj na USB
while (1)
{
//Sprawdź SPI
if(SPI_Poll(&measure,length)==SPI_FRAME_READY)
{
//przelicz dane z Suwmiarki na mm, z dokłądnością 0,01mm
length=MeasureToMM(measure);
//konwertuj na tekst
sprintf(TextBuf,("%d,%d\n\r",length/100,length%100);
}
//sprawdź klawiaturę
if(Key_Poll(&key_code)==KEY_PRESSED)
{
switch(key_code)
{
case KEY_MEAS:
//jeżeli uSB jest podłączone, wyślij tekst
if (bDeviceState == CONFIGURED)
USB_SendText(TextBuf,strlen(TextBuf));
break
case KEY_2:
//brak funkcji, póki co
break;
default:
break;
}
}
//Stan diody USB
if (bDeviceState == CONFIGURED)
GPIO_SetBits(GPIOA,PA3);
else
GPIO_ResetBits(GPIOA,PA3);
//stan diody Suwmiarki
if (SPI_GetState()==SPI_CONNECTED)
GPIO_SetBits(GPIOA,PA4);
else
GPIO_ResetBits(GPIOA,PA4);
//czekaj do następnego taktu zegara
Wait(0);
}
}
#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****/