Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************************************
- * Value read from BNC is written to the OLED display (nothing graphical
- * yet only value).
- *
- * Copyright(C) 2010, Embedded Artists AB
- * All rights reserved.
- *
- ******************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include "lpc17xx_pinsel.h"
- #include "lpc17xx_gpio.h"
- #include "lpc17xx_i2c.h"
- #include "lpc17xx_ssp.h"
- #include "lpc17xx_adc.h"
- #include "lpc17xx_timer.h"
- #include "oled.h"
- #include "LPC17xx.h"
- #include "lpc_types.h"
- #include "joystick.h"
- #define WIDTH (8 * sizeof(crc))
- #define TOPBIT (1 << (WIDTH - 1))
- #define POLYNOMIAL 0xD8 /* 11011 followed by 0's */
- uint8_t crcTable[256];
- typedef uint8_t crc;
- uint8_t crcNaive(uint8_t const message)
- {
- uint8_t remainder;
- /*
- * Initially, the dividend is the remainder.
- */
- remainder = message;
- uint8_t bit;
- /*
- * For each bit position in the message....
- */
- for (bit = 8; bit > 0; --bit)
- {
- /*
- * If the uppermost bit is a 1...
- */
- if (remainder & 0x80)
- {
- /*
- * XOR the previous remainder with the divisor.
- */
- remainder ^= POLYNOMIAL;
- }
- /*
- * Shift the next bit of the message into the remainder.
- */
- remainder = (remainder << 1);
- }
- /*
- * Return only the relevant bits of the remainder as CRC.
- */
- return (remainder >> 4);
- } /* crcNaive() */
- crc crcTable[256];
- void
- crcInit(void)
- {
- crc remainder;
- int dividend;
- /*
- * Compute the remainder of each possible dividend.
- */
- for (dividend = 0; dividend < 256; ++dividend)
- {
- /*
- * Start with the dividend followed by zeros.
- */
- remainder = dividend << (WIDTH - 8);
- uint8_t bit;
- /*
- * Perform modulo-2 division, a bit at a time.
- */
- for (bit = 8; bit > 0; --bit)
- {
- /*
- * Try to divide the current data bit.
- */
- if (remainder & TOPBIT)
- {
- remainder = (remainder << 1) ^ POLYNOMIAL;
- }
- else
- {
- remainder = (remainder << 1);
- }
- }
- /*
- * Store the result into the table.
- */
- crcTable[dividend] = remainder;
- }
- } /* crcInit() */
- typedef struct {
- int zeroTimeMs;
- int oneTimeMs;
- int waitTimeMs;
- int startStatusTimeMs;
- int pin;
- int port;
- } App_config;
- App_config appCfg;
- typedef struct {
- char msg[100];
- char currentChar;
- char lastChar;
- char cursorChar;
- int charOledPos;
- int charOledRow;
- int charOledLastPos;
- int charOledLastRow;
- int cursorPosition;
- int memAlocated;
- int sending;
- } Comunication_data;
- Comunication_data comunicationData;
- typedef struct {
- char enterMsg[15];
- char sendingMsg[15];
- char finishedSendingMsg[15];
- } Status_messages;
- Status_messages statusMsg;
- static void init_app()
- {
- appCfg.zeroTimeMs = 200;
- appCfg.oneTimeMs = 400;
- appCfg.waitTimeMs = 200;
- appCfg.startStatusTimeMs = 1000;
- appCfg.pin = 6;
- appCfg.port = 2;
- GPIO_SetDir(2, 1 << 6, 1);
- }
- void init_com_data(){
- comunicationData.currentChar = 'a';
- comunicationData.lastChar = comunicationData.currentChar;
- comunicationData.cursorChar = '\0';
- comunicationData.charOledPos = 1;
- comunicationData.charOledRow = 1;
- comunicationData.cursorPosition = 0;
- comunicationData.memAlocated = 0;
- comunicationData.sending = 0;
- }
- void init_status_msg(){
- strcpy(statusMsg.enterMsg, "Vnesi poraka");
- strcpy(statusMsg.sendingMsg, "Se ispraka");
- strcpy(statusMsg.finishedSendingMsg, "Ispratno");
- }
- void set_status_msg(int msgType){
- oled_fillRect(0, 9*6, 6*15, 9*6, OLED_COLOR_WHITE);
- printf("%s\n",statusMsg.enterMsg);
- if(msgType == 1){
- oled_putString(1, 9*6, (uint8_t*)statusMsg.enterMsg, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
- } else if(msgType == 2) {
- oled_putString(1, 9*6, (uint8_t*)statusMsg.sendingMsg, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
- } else if(msgType == 3) {
- oled_putString(1, 9*6, (uint8_t*)statusMsg.finishedSendingMsg, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
- }
- }
- void increse_char(){
- comunicationData.currentChar +=1;
- comunicationData.cursorChar = comunicationData.currentChar;
- }
- void decrese_char(){
- comunicationData.currentChar -=1;
- comunicationData.cursorChar = comunicationData.currentChar;
- }
- void put_char_to_oled(){
- oled_putChar(comunicationData.charOledPos, comunicationData.charOledRow,
- (uint8_t*)comunicationData.currentChar, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
- }
- void erase_char_on_oled(){
- oled_fillRect(comunicationData.charOledPos, comunicationData.charOledRow,
- comunicationData.charOledPos + 6, comunicationData.charOledRow +7, OLED_COLOR_WHITE);
- }
- void draw_cursor_on_oled(){
- oled_line(comunicationData.charOledLastPos, comunicationData.charOledLastRow + 9,
- comunicationData.charOledLastPos + 6, comunicationData.charOledLastRow + 9, OLED_COLOR_WHITE);
- oled_line(comunicationData.charOledPos, comunicationData.charOledRow + 9,
- comunicationData.charOledPos+6, comunicationData.charOledRow + 9, OLED_COLOR_BLACK);
- }
- void move_cursor_pos_forward(){
- comunicationData.charOledLastPos = comunicationData.charOledPos;
- comunicationData.charOledLastRow = comunicationData.charOledRow;
- comunicationData.charOledPos += 6;
- comunicationData.charOledRow += ((comunicationData.charOledPos + 6) / OLED_DISPLAY_WIDTH) * 9;
- comunicationData.charOledPos %= (OLED_DISPLAY_WIDTH - 6);
- if(comunicationData.cursorChar == '\0') {
- comunicationData.msg[comunicationData.cursorPosition] = ' ';
- }
- else
- comunicationData.msg[comunicationData.cursorPosition] = comunicationData.currentChar;
- if(comunicationData.cursorPosition<100){
- comunicationData.cursorPosition++;
- comunicationData.msg[comunicationData.cursorPosition] = '\0';
- comunicationData.cursorChar = '\0';
- }
- printf("%s\n",comunicationData.msg);
- }
- void move_cursor_pos_backward(){
- if(comunicationData.cursorPosition == 0) return;
- comunicationData.charOledLastPos = comunicationData.charOledPos;
- comunicationData.charOledLastRow = comunicationData.charOledRow;
- if(comunicationData.charOledPos < 0){
- comunicationData.charOledRow -= 9;
- comunicationData.charOledPos = (OLED_DISPLAY_WIDTH - 6);
- }
- else{
- comunicationData.msg[comunicationData.cursorPosition] = '\0';
- erase_char_on_oled();
- comunicationData.cursorPosition--;
- comunicationData.lastChar = comunicationData.msg[comunicationData.cursorPosition];
- }
- comunicationData.charOledPos -= 6;
- printf("%s\n",comunicationData.msg);
- }
- void check_joystick(int joyState){
- if(comunicationData.sending == 1) return;
- if ((joyState & JOYSTICK_CENTER) != 0) {
- comunicationData.sending = 1;
- send_data(comunicationData.msg);
- comunicationData.sending = 0;
- return;
- }
- if ((joyState & JOYSTICK_RIGHT) != 0) {
- move_cursor_pos_forward();
- draw_cursor_on_oled();
- return;
- }
- if((joyState & JOYSTICK_LEFT) != 0){
- move_cursor_pos_backward();
- draw_cursor_on_oled();
- return;
- }
- if ((joyState & JOYSTICK_UP) != 0 ) {
- increse_char();
- erase_char_on_oled();
- put_char_to_oled();
- }
- if ((joyState & JOYSTICK_DOWN) != 0 ) {
- decrese_char();
- erase_char_on_oled();
- put_char_to_oled();
- }
- }
- void blink_diode(int timeMs, int port, int pin){
- GPIO_ClearValue(port, 1<<pin);
- GPIO_SetValue(port, 1<<pin);
- Timer0_Wait(timeMs);
- GPIO_ClearValue(port, 1<<pin);
- }
- static void init_diode(int portNum)
- {
- //blink_diode(1000, 2, 6);
- }
- uint8_t crcFast(uint8_t message[],int nBytes)
- {
- uint8_t data;
- uint8_t remainder = 0;
- int byte;
- /*
- * Divide the message by the polynomial, a byte at a time.
- */
- for (byte = 0; byte < nBytes; ++byte)
- {
- data = message[byte] ^ (remainder >> (WIDTH - 8));
- remainder = crcTable[data] ^ (remainder << 8);
- }
- /*
- * The final remainder is the CRC.
- */
- return (remainder);
- }
- int byteArray[8];
- void charToByte(char c){
- int i,j;
- for(i = 7, j = 0; i >= 0; --i, j++)
- if (c & 1 << i){
- byteArray[j] = 1;
- printf("1");
- }
- else {
- byteArray[j] = 0;
- printf("0");
- }
- printf("\n");
- }
- void send_data(char * str){
- int i,j;
- set_status_msg(2);
- uint8_t tmp = crcFast((uint8_t*)str, strlen(str));
- //printf(" crc %c\n",crc((uint8_t*)str, strlen(str)));
- for(i = 0; i<strlen(str) + 1; i++){
- charToByte(str[i]);
- blink_diode(appCfg.startStatusTimeMs,appCfg.port,appCfg.pin);
- Timer0_Wait(appCfg.waitTimeMs);
- for(j = 7; j >= 0; --j){
- if(byteArray[j] == 1) {
- blink_diode(appCfg.oneTimeMs, appCfg.port, appCfg.pin);
- } else {
- blink_diode(appCfg.zeroTimeMs, appCfg.port, appCfg.pin);
- }
- Timer0_Wait(appCfg.waitTimeMs);
- }
- blink_diode(appCfg.startStatusTimeMs,appCfg.port,appCfg.pin);
- Timer0_Wait(appCfg.waitTimeMs);
- }
- //blink_diode(appCfg.startStatusTimeMs,appCfg.port,appCfg.pin);
- oled_clearScreen(OLED_COLOR_WHITE);
- init_com_data();
- draw_cursor_on_oled();
- set_status_msg(3);
- Timer0_Wait(1000);
- set_status_msg(1);
- }
- static void init_ssp(void)
- {
- SSP_CFG_Type SSP_ConfigStruct;
- PINSEL_CFG_Type PinCfg;
- /*
- * Initialize SPI pin connect
- * P0.7 - SCK;
- * P0.8 - MISO
- * P0.9 - MOSI
- * P2.2 - SSEL - used as GPIO
- */
- PinCfg.Funcnum = 2;
- PinCfg.OpenDrain = 0;
- PinCfg.Pinmode = 0;
- PinCfg.Portnum = 0;
- PinCfg.Pinnum = 7;
- PINSEL_ConfigPin(&PinCfg);
- PinCfg.Pinnum = 8;
- PINSEL_ConfigPin(&PinCfg);
- PinCfg.Pinnum = 9;
- PINSEL_ConfigPin(&PinCfg);
- PinCfg.Funcnum = 0;
- PinCfg.Portnum = 2;
- PinCfg.Pinnum = 2;
- PINSEL_ConfigPin(&PinCfg);
- SSP_ConfigStructInit(&SSP_ConfigStruct);
- // Initialize SSP peripheral with parameter given in structure above
- SSP_Init(LPC_SSP1, &SSP_ConfigStruct);
- // Enable SSP peripheral
- SSP_Cmd(LPC_SSP1, ENABLE);
- }
- int main (void) {
- int state;
- init_app();
- init_diode(28);
- init_com_data();
- init_status_msg();
- init_ssp();
- crcInit();
- oled_init();
- oled_clearScreen(OLED_COLOR_WHITE);
- joystick_init();
- // put_char_to_oled();
- //
- // increse_char();
- //
- // Timer0_Wait(2000);
- //
- // erase_char_on_oled();
- //
- // put_char_to_oled();
- draw_cursor_on_oled();
- set_status_msg(1);
- while(1)
- {
- state = joystick_read();
- if (state != 0)
- check_joystick(state);
- Timer0_Wait(100);
- }
- //Timer0_Wait(3000);
- //send_data("Hello");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement