Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.68 KB | None | 0 0
  1. /*****************************************************************************
  2.  *   Value read from BNC is written to the OLED display (nothing graphical
  3.  
  4.  *   yet only value).
  5.  *
  6.  *   Copyright(C) 2010, Embedded Artists AB
  7.  *   All rights reserved.
  8.  *
  9.  ******************************************************************************/
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include "lpc17xx_pinsel.h"
  14. #include "lpc17xx_gpio.h"
  15. #include "lpc17xx_i2c.h"
  16. #include "lpc17xx_ssp.h"
  17. #include "lpc17xx_adc.h"
  18. #include "lpc17xx_timer.h"
  19.  
  20. #include "oled.h"
  21. #include "LPC17xx.h"
  22. #include "lpc_types.h"
  23. #include "joystick.h"
  24.  
  25. #define WIDTH  (8 * sizeof(crc))
  26. #define TOPBIT (1 << (WIDTH - 1))
  27. #define POLYNOMIAL 0xD8  /* 11011 followed by 0's */
  28. uint8_t crcTable[256];
  29. typedef uint8_t crc;
  30.  
  31.  
  32. uint8_t crcNaive(uint8_t const message)
  33. {
  34.     uint8_t  remainder;
  35.  
  36.  
  37.     /*
  38.      * Initially, the dividend is the remainder.
  39.      */
  40.     remainder = message;
  41.     uint8_t bit;
  42.     /*
  43.      * For each bit position in the message....
  44.      */
  45.     for (bit = 8; bit > 0; --bit)
  46.     {
  47.         /*
  48.          * If the uppermost bit is a 1...
  49.          */
  50.         if (remainder & 0x80)
  51.         {
  52.             /*
  53.              * XOR the previous remainder with the divisor.
  54.              */
  55.             remainder ^= POLYNOMIAL;
  56.         }
  57.  
  58.         /*
  59.          * Shift the next bit of the message into the remainder.
  60.          */
  61.         remainder = (remainder << 1);
  62.     }
  63.  
  64.     /*
  65.      * Return only the relevant bits of the remainder as CRC.
  66.      */
  67.     return (remainder >> 4);
  68.  
  69. }   /* crcNaive() */
  70.  
  71. crc  crcTable[256];
  72.  
  73. void
  74. crcInit(void)
  75. {
  76.     crc  remainder;
  77.     int dividend;
  78.  
  79.     /*
  80.      * Compute the remainder of each possible dividend.
  81.      */
  82.     for (dividend = 0; dividend < 256; ++dividend)
  83.     {
  84.         /*
  85.          * Start with the dividend followed by zeros.
  86.          */
  87.         remainder = dividend << (WIDTH - 8);
  88.         uint8_t bit;
  89.         /*
  90.          * Perform modulo-2 division, a bit at a time.
  91.          */
  92.         for (bit = 8; bit > 0; --bit)
  93.         {
  94.             /*
  95.              * Try to divide the current data bit.
  96.              */
  97.             if (remainder & TOPBIT)
  98.             {
  99.                 remainder = (remainder << 1) ^ POLYNOMIAL;
  100.             }
  101.             else
  102.             {
  103.                 remainder = (remainder << 1);
  104.             }
  105.         }
  106.  
  107.         /*
  108.          * Store the result into the table.
  109.          */
  110.         crcTable[dividend] = remainder;
  111.     }
  112.  
  113. }   /* crcInit() */
  114.  
  115. typedef struct {
  116.     int zeroTimeMs;
  117.     int oneTimeMs;
  118.     int waitTimeMs;
  119.     int startStatusTimeMs;
  120.     int pin;
  121.     int port;
  122. } App_config;
  123.  
  124. App_config appCfg;
  125.  
  126. typedef struct {
  127.     char msg[100];
  128.     char currentChar;
  129.     char lastChar;
  130.     char cursorChar;
  131.     int charOledPos;
  132.     int charOledRow;
  133.     int charOledLastPos;
  134.     int charOledLastRow;
  135.     int cursorPosition;
  136.     int memAlocated;
  137.     int sending;
  138. } Comunication_data;
  139.  
  140. Comunication_data comunicationData;
  141.  
  142. typedef struct {
  143.     char enterMsg[15];
  144.     char sendingMsg[15];
  145.     char finishedSendingMsg[15];
  146. } Status_messages;
  147.  
  148. Status_messages statusMsg;
  149.  
  150. static void init_app()
  151. {
  152.     appCfg.zeroTimeMs = 200;
  153.     appCfg.oneTimeMs = 400;
  154.     appCfg.waitTimeMs = 200;
  155.     appCfg.startStatusTimeMs = 1000;
  156.     appCfg.pin = 6;
  157.     appCfg.port = 2;
  158.     GPIO_SetDir(2, 1 << 6, 1);
  159. }
  160.  
  161. void init_com_data(){
  162.     comunicationData.currentChar = 'a';
  163.     comunicationData.lastChar = comunicationData.currentChar;
  164.     comunicationData.cursorChar = '\0';
  165.     comunicationData.charOledPos = 1;
  166.     comunicationData.charOledRow = 1;
  167.     comunicationData.cursorPosition = 0;
  168.     comunicationData.memAlocated = 0;
  169.     comunicationData.sending = 0;
  170. }
  171.  
  172. void init_status_msg(){
  173.     strcpy(statusMsg.enterMsg, "Vnesi poraka");
  174.     strcpy(statusMsg.sendingMsg, "Se ispraka");
  175.     strcpy(statusMsg.finishedSendingMsg, "Ispratno");
  176. }
  177.  
  178. void set_status_msg(int msgType){
  179.     oled_fillRect(0, 9*6, 6*15, 9*6,  OLED_COLOR_WHITE);
  180.     printf("%s\n",statusMsg.enterMsg);
  181.     if(msgType == 1){
  182.         oled_putString(1, 9*6, (uint8_t*)statusMsg.enterMsg, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  183.     } else if(msgType == 2) {
  184.         oled_putString(1, 9*6, (uint8_t*)statusMsg.sendingMsg, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  185.     } else if(msgType == 3) {
  186.         oled_putString(1, 9*6, (uint8_t*)statusMsg.finishedSendingMsg, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  187.     }
  188. }
  189.  
  190. void increse_char(){
  191.     comunicationData.currentChar +=1;
  192.     comunicationData.cursorChar = comunicationData.currentChar;
  193. }
  194.  
  195. void decrese_char(){
  196.     comunicationData.currentChar -=1;
  197.     comunicationData.cursorChar = comunicationData.currentChar;
  198. }
  199.  
  200. void put_char_to_oled(){
  201.     oled_putChar(comunicationData.charOledPos, comunicationData.charOledRow,
  202.             (uint8_t*)comunicationData.currentChar, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  203. }
  204.  
  205. void erase_char_on_oled(){
  206.     oled_fillRect(comunicationData.charOledPos, comunicationData.charOledRow,
  207.             comunicationData.charOledPos + 6, comunicationData.charOledRow +7,  OLED_COLOR_WHITE);
  208. }
  209.  
  210. void draw_cursor_on_oled(){
  211.     oled_line(comunicationData.charOledLastPos, comunicationData.charOledLastRow + 9,
  212.                 comunicationData.charOledLastPos + 6, comunicationData.charOledLastRow + 9, OLED_COLOR_WHITE);
  213.     oled_line(comunicationData.charOledPos, comunicationData.charOledRow + 9,
  214.             comunicationData.charOledPos+6, comunicationData.charOledRow + 9, OLED_COLOR_BLACK);
  215. }
  216.  
  217. void move_cursor_pos_forward(){
  218.  
  219.     comunicationData.charOledLastPos = comunicationData.charOledPos;
  220.     comunicationData.charOledLastRow = comunicationData.charOledRow;
  221.  
  222.     comunicationData.charOledPos += 6;
  223.  
  224.     comunicationData.charOledRow += ((comunicationData.charOledPos + 6) / OLED_DISPLAY_WIDTH) * 9;
  225.     comunicationData.charOledPos %= (OLED_DISPLAY_WIDTH - 6);
  226.  
  227.  
  228.     if(comunicationData.cursorChar == '\0') {
  229.         comunicationData.msg[comunicationData.cursorPosition] = ' ';
  230.     }
  231.     else
  232.         comunicationData.msg[comunicationData.cursorPosition] = comunicationData.currentChar;
  233.  
  234.     if(comunicationData.cursorPosition<100){
  235.         comunicationData.cursorPosition++;
  236.         comunicationData.msg[comunicationData.cursorPosition] = '\0';
  237.         comunicationData.cursorChar = '\0';
  238.     }
  239.  
  240.  
  241.  
  242.  
  243.     printf("%s\n",comunicationData.msg);
  244.  
  245. }
  246.  
  247. void move_cursor_pos_backward(){
  248.     if(comunicationData.cursorPosition == 0) return;
  249.     comunicationData.charOledLastPos = comunicationData.charOledPos;
  250.     comunicationData.charOledLastRow = comunicationData.charOledRow;
  251.  
  252.     if(comunicationData.charOledPos < 0){
  253.         comunicationData.charOledRow -= 9;
  254.         comunicationData.charOledPos = (OLED_DISPLAY_WIDTH - 6);
  255.     }
  256.     else{
  257.  
  258.         comunicationData.msg[comunicationData.cursorPosition] = '\0';
  259.         erase_char_on_oled();
  260.         comunicationData.cursorPosition--;
  261.         comunicationData.lastChar = comunicationData.msg[comunicationData.cursorPosition];
  262.     }
  263.     comunicationData.charOledPos -= 6;
  264.  
  265.     printf("%s\n",comunicationData.msg);
  266. }
  267.  
  268. void check_joystick(int joyState){
  269.     if(comunicationData.sending == 1) return;
  270.     if ((joyState & JOYSTICK_CENTER) != 0) {
  271.         comunicationData.sending = 1;
  272.         send_data(comunicationData.msg);
  273.         comunicationData.sending = 0;
  274.         return;
  275.     }
  276.     if ((joyState & JOYSTICK_RIGHT) != 0) {
  277.         move_cursor_pos_forward();
  278.         draw_cursor_on_oled();
  279.         return;
  280.     }
  281.  
  282.     if((joyState & JOYSTICK_LEFT) != 0){
  283.         move_cursor_pos_backward();
  284.         draw_cursor_on_oled();
  285.         return;
  286.     }
  287.  
  288.     if ((joyState & JOYSTICK_UP) != 0 ) {
  289.         increse_char();
  290.         erase_char_on_oled();
  291.         put_char_to_oled();
  292.     }
  293.  
  294.     if ((joyState & JOYSTICK_DOWN) != 0 ) {
  295.         decrese_char();
  296.         erase_char_on_oled();
  297.         put_char_to_oled();
  298.     }
  299.  
  300.  
  301. }
  302.  
  303. void blink_diode(int timeMs, int port, int pin){
  304.     GPIO_ClearValue(port, 1<<pin);
  305.     GPIO_SetValue(port, 1<<pin);
  306.     Timer0_Wait(timeMs);
  307.     GPIO_ClearValue(port, 1<<pin);
  308. }
  309.  
  310. static void init_diode(int portNum)
  311. {
  312.     //blink_diode(1000, 2, 6);
  313. }
  314.  
  315. uint8_t crcFast(uint8_t message[],int nBytes)
  316. {
  317.     uint8_t data;
  318.     uint8_t remainder = 0;
  319.     int byte;
  320.  
  321.     /*
  322.      * Divide the message by the polynomial, a byte at a time.
  323.      */
  324.     for (byte = 0; byte < nBytes; ++byte)
  325.     {
  326.         data = message[byte] ^ (remainder >> (WIDTH - 8));
  327.         remainder = crcTable[data] ^ (remainder << 8);
  328.     }
  329.  
  330.     /*
  331.      * The final remainder is the CRC.
  332.      */
  333.     return (remainder);
  334.  
  335. }
  336.  
  337. int byteArray[8];
  338.  
  339. void charToByte(char c){
  340.  
  341.     int i,j;
  342.     for(i = 7, j = 0; i >= 0; --i, j++)
  343.                if (c & 1 << i){
  344.                    byteArray[j] = 1;
  345.                    printf("1");
  346.                }
  347.  
  348.                else {
  349.                    byteArray[j] = 0;
  350.                    printf("0");
  351.                }
  352.  
  353.     printf("\n");
  354. }
  355. void send_data(char * str){
  356.     int i,j;
  357.     set_status_msg(2);
  358.  
  359.     uint8_t tmp = crcFast((uint8_t*)str, strlen(str));
  360.     //printf(" crc %c\n",crc((uint8_t*)str, strlen(str)));
  361.     for(i = 0; i<strlen(str) + 1; i++){
  362.  
  363.         charToByte(str[i]);
  364.         blink_diode(appCfg.startStatusTimeMs,appCfg.port,appCfg.pin);
  365.         Timer0_Wait(appCfg.waitTimeMs);
  366.         for(j = 7; j >= 0; --j){
  367.             if(byteArray[j] == 1) {
  368.                 blink_diode(appCfg.oneTimeMs, appCfg.port, appCfg.pin);
  369.             } else {
  370.                 blink_diode(appCfg.zeroTimeMs, appCfg.port, appCfg.pin);
  371.             }
  372.             Timer0_Wait(appCfg.waitTimeMs);
  373.         }
  374.         blink_diode(appCfg.startStatusTimeMs,appCfg.port,appCfg.pin);
  375.         Timer0_Wait(appCfg.waitTimeMs);
  376.     }
  377.     //blink_diode(appCfg.startStatusTimeMs,appCfg.port,appCfg.pin);
  378.     oled_clearScreen(OLED_COLOR_WHITE);
  379.     init_com_data();
  380.     draw_cursor_on_oled();
  381.     set_status_msg(3);
  382.     Timer0_Wait(1000);
  383.     set_status_msg(1);
  384. }
  385.  
  386. static void init_ssp(void)
  387. {
  388.     SSP_CFG_Type SSP_ConfigStruct;
  389.     PINSEL_CFG_Type PinCfg;
  390.  
  391.     /*
  392.      * Initialize SPI pin connect
  393.      * P0.7 - SCK;
  394.      * P0.8 - MISO
  395.      * P0.9 - MOSI
  396.      * P2.2 - SSEL - used as GPIO
  397.      */
  398.     PinCfg.Funcnum = 2;
  399.     PinCfg.OpenDrain = 0;
  400.     PinCfg.Pinmode = 0;
  401.     PinCfg.Portnum = 0;
  402.     PinCfg.Pinnum = 7;
  403.     PINSEL_ConfigPin(&PinCfg);
  404.     PinCfg.Pinnum = 8;
  405.     PINSEL_ConfigPin(&PinCfg);
  406.     PinCfg.Pinnum = 9;
  407.     PINSEL_ConfigPin(&PinCfg);
  408.     PinCfg.Funcnum = 0;
  409.     PinCfg.Portnum = 2;
  410.     PinCfg.Pinnum = 2;
  411.     PINSEL_ConfigPin(&PinCfg);
  412.  
  413.     SSP_ConfigStructInit(&SSP_ConfigStruct);
  414.  
  415.     // Initialize SSP peripheral with parameter given in structure above
  416.     SSP_Init(LPC_SSP1, &SSP_ConfigStruct);
  417.  
  418.     // Enable SSP peripheral
  419.     SSP_Cmd(LPC_SSP1, ENABLE);
  420.  
  421. }
  422.  
  423.  
  424. int main (void) {
  425.  
  426.     int state;
  427.     init_app();
  428.     init_diode(28);
  429.     init_com_data();
  430.     init_status_msg();
  431.     init_ssp();
  432.     crcInit();
  433.  
  434.     oled_init();
  435.     oled_clearScreen(OLED_COLOR_WHITE);
  436.     joystick_init();
  437.  
  438.  
  439. //  put_char_to_oled();
  440. //
  441. //  increse_char();
  442. //
  443. //  Timer0_Wait(2000);
  444. //
  445. //  erase_char_on_oled();
  446. //
  447. //  put_char_to_oled();
  448.     draw_cursor_on_oled();
  449.     set_status_msg(1);
  450.     while(1)
  451.     {
  452.         state = joystick_read();
  453.                 if (state != 0)
  454.                     check_joystick(state);
  455.         Timer0_Wait(100);
  456.     }
  457.  
  458.     //Timer0_Wait(3000);
  459.  
  460.     //send_data("Hello");
  461.  
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement