5 * @brief Library for communication with uOLED display using serial interface.
16 #include <robottype.h>
17 #include <robottype.h>
22 static struct sercom_data sercom;
24 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
26 struct sercom_data* uoled_sercom_init(char * tty, void(*sighandler)(int))
29 strcpy((char *)&sercom.devname, tty);
30 sercom.baudrate = 115200;
31 sercom.parity = SERCOM_PARNONE;
35 sercom.sighandler = NULL;
36 ret = sercom_open(&sercom);
43 void uoled_sercom_close()
45 if(sercom.mode != 0 && sercom.fd >=0 )
46 sercom_close(&sercom);
49 int uoled_write_cmd(uint8_t *buff, int size)
53 pthread_mutex_lock(&mutex);
54 printf("sending command (%2d bytes): ", size);
56 /* specialni vypis pro stav komponent */
57 printf(" 0x%02x ", buff[0]);
59 for(i=1; i<size; i++){
60 for(j=sizeof(buff[i])*8; j >=0 ; j--)
61 printf("%i",(buff[i] >> j) & 1 );
62 printf(" (component: %i, status: %i)",buff[i] & COMPONENTS_MASK, (buff[i] & STATUS_MASK) >> STATUS_SHIFT);
66 if (buff[i] < ' ' || buff[i] > '~' || buff[0] == 0x81)
67 printf(" 0x%02x ", buff[i]);
69 printf("%c", buff[i]);
73 ret = write(sercom.fd, buff, size);
74 pthread_mutex_unlock(&mutex);
76 printf("uoled: WRITE FAILED!\n");
83 int uoled_read_cmd(uint8_t *buff, int size)
86 pthread_mutex_lock(&mutex);
87 ret = read(sercom.fd, buff, size);
88 pthread_mutex_unlock(&mutex);
90 printf("uoled: READ FAILED!\n");
97 int uoled_recieve_cmd(UDE_recieve_cmd_t *cmd)
99 return uoled_read_cmd(&cmd, 1);
102 int uoled_display_status(UDE_component_t c, UDE_hw_status_t s)
105 uint8_t msg[MGS_LENGTH_DISPLAY_STATUS];
107 msg[0]=ID_DISPLAY_STATUS;
111 ret = uoled_write_cmd(msg, MGS_LENGTH_DISPLAY_STATUS);
118 int uoled_display_fsm(UDE_fsm_t fsm, char * state)
121 uint8_t msg[MGS_LENGTH_DISPLAY_SM];
123 msg[0]=ID_DISPLAY_FSM + fsm;
124 for( i = 0; i < MGS_LENGTH_DISPLAY_SM - 2; i++){
129 msg[MGS_LENGTH_DISPLAY_SM - 1] = '\0';
131 ret = uoled_write_cmd(msg, i+2);
138 int uoled_display_alive()
141 uint8_t msg[MGS_LENGTH_DISPLAY_ALIVE];
142 msg[0]=ID_DISPLAY_ALIVE;
144 ret = uoled_write_cmd(msg, MGS_LENGTH_DISPLAY_ALIVE);
151 int uoled_display_color(int color)
154 uint8_t msg[MGS_LENGTH_DISPLAY_COLOR];
155 msg[0]=ID_DISPLAY_COLOR;
158 ret = uoled_write_cmd(msg, MGS_LENGTH_DISPLAY_COLOR);
165 int uoled_display_voltage(double voltage33, double voltage50,
166 double voltage80, double voltageBAT)
169 uint8_t msg[MGS_LENGTH_DISPLAY_VOLTAGE];
170 unsigned char status_indicator = 0;
172 if( voltageBAT < WARNING_VOLTAGEBAT)
173 status_indicator += 1 << 4;
174 if( voltage33 < TRESHOLDS_VOLTAGE33 )
175 status_indicator += 1 << 3;
176 if( voltage50 < TRESHOLDS_VOLTAGE50 )
177 status_indicator += 1 << 2;
178 if( voltage80 < TRESHOLDS_VOLTAGE80 )
179 status_indicator += 1 << 1;
180 if( voltageBAT < TRESHOLDS_VOLTAGEBAT )
181 status_indicator += 1 << 0;
183 msg[0]=ID_DISPLAY_VOLTAGE;
184 msg[1]=(uint8_t)status_indicator;
186 sprintf((char*)(msg+2), "%2i%i%i", (int)voltage33 % 100,
187 (int)(voltage33 * 10) % 10, (int)(voltage33 * 100) % 10);
188 sprintf((char*)(msg+6), "%2i%i%i", (int)voltage50 % 100,
189 (int)(voltage50 * 10) % 10, (int)(voltage50 * 100) % 10);
190 sprintf((char*)(msg+10), "%2i%i%i", (int)voltage80 % 100,
191 (int)(voltage80 * 10) % 10, (int)(voltage80 * 100) % 10);
192 sprintf((char*)(msg+14), "%2i%i%i", (int)voltageBAT % 100,
193 (int)(voltageBAT * 10) % 10, (int)(voltageBAT * 100) % 10);
195 ret = uoled_write_cmd(msg, MGS_LENGTH_DISPLAY_VOLTAGE);
202 int uoled_display_position(double x, double y, double phi)
205 uint8_t msg[MGS_LENGTH_DISPLAY_POSITION];
207 //printf(" x: %f \n y: %f \n phi: %f\n",x,y,phi);
209 msg[0]=ID_DISPLAY_POSITION;
211 sprintf((char*)(msg+1),"%3i", (int)x % 1000);
214 sprintf((char*)(msg+1),"%2i%i", (int)x % 100,
217 sprintf((char*)(msg+1),"%i%i%i", (int)x % 10,
218 (int)(x*10) % 10, (int)(x*100) % 10);
220 sprintf((char*)(msg+4),"%3i", (int)y % 1000);
223 sprintf((char*)(msg+4),"%2i%i", (int)y % 100,
226 sprintf((char*)(msg+4),"%i%i%i", (int)y % 10,
227 (int)(y*10) % 10, (int)(y*100) % 10);
230 phi = fmod(phi, 360);
234 sprintf((char*)(msg+7),"%3i", (int)phi);
236 ret = uoled_write_cmd(msg, MGS_LENGTH_DISPLAY_POSITION);