st7735.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* vim: set ai et ts=4 sw=4: */
  2. #include "stm32f1xx_hal.h"
  3. #include "st7735.h"
  4. #include "../spi/ad0x0_spi1.h"
  5. #define DELAY 0x80
  6. uint16_t ad0x0_charbuf[16*26];//
  7. // based on Adafruit ST7735 library for Arduino
  8. static const uint8_t
  9. Rcmd1[] = { // 7735R init, part 1 (red or green tab)
  10. 15, // 15 commands in list:
  11. ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
  12. 150, // 150 ms delay
  13. ST7735_SLPOUT, DELAY, // 2: Out of sleep mode, 0 args, w/delay
  14. 255, // 500 ms delay
  15. ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg:
  16. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  17. ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args:
  18. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  19. ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args:
  20. 0x01, 0x2C, 0x2D, // Dot inversion mode
  21. 0x01, 0x2C, 0x2D, // Line inversion mode
  22. ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg:
  23. 0x0, // No inversion
  24. ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
  25. 0xA2,
  26. 0x02, // -4.6V
  27. 0x84, // AUTO mode
  28. ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
  29. 0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD
  30. ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
  31. 0x0A, // Opamp current small
  32. 0x00, // Boost frequency
  33. ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
  34. 0x8A, // BCLK/2,
  35. 0x2A, // opamp current small & medium low
  36. ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
  37. 0x8A, 0xEE,
  38. ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
  39. 0x0E,
  40. ST7735_INVOFF, 0, // 13: Don't invert display, no args
  41. ST7735_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg:
  42. 0x68, // row/col addr, bottom-top refresh
  43. ST7735_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
  44. 0x05 }, // 16-bit color
  45. #if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128))
  46. init_cmds2[] = { // Init for 7735R, part 2 (1.44" display)
  47. 2, // 2 commands in list:
  48. ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
  49. 0x00, 0x00, // XSTART = 0
  50. 0x00, 0x7F, // XEND = 127
  51. ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
  52. 0x00, 0x00, // XSTART = 0
  53. 0x00, 0x7F }, // XEND = 127
  54. #endif // ST7735_IS_128X128
  55. #ifdef ST7735_IS_160X80
  56. init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
  57. 3, // 3 commands in list:
  58. ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
  59. 0x00, 0x00, // XSTART = 0
  60. 0x00, 0x4F, // XEND = 79
  61. ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
  62. 0x00, 0x00, // XSTART = 0
  63. 0x00, 0x9F , // XEND = 159
  64. ST7735_INVOFF, 0 }, // 3: Invert colors
  65. #endif
  66. init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
  67. 4, // 4 commands in list:
  68. ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
  69. 0x02, 0x1c, 0x07, 0x12,
  70. 0x37, 0x32, 0x29, 0x2d,
  71. 0x29, 0x25, 0x2B, 0x39,
  72. 0x00, 0x01, 0x03, 0x10,
  73. ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
  74. 0x03, 0x1d, 0x07, 0x06,
  75. 0x2E, 0x2C, 0x29, 0x2D,
  76. 0x2E, 0x2E, 0x37, 0x3F,
  77. 0x00, 0x00, 0x02, 0x10,
  78. ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
  79. 10, // 10 ms delay
  80. ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
  81. 100 }; // 100 ms delay
  82. static void ST7735_WriteCommand(uint8_t cmd) {
  83. //HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_RESET);
  84. //HAL_SPI_Transmit(&ST7735_SPI_PORT, &cmd, sizeof(cmd), HAL_MAX_DELAY);
  85. ad0x0_spi1_push(ADQS_ST7735_SET_DC,0);
  86. ad0x0_spi1_push(ADQS_TX,cmd);
  87. }
  88. static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
  89. //HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
  90. //HAL_SPI_Transmit(&ST7735_SPI_PORT, buff, buff_size, HAL_MAX_DELAY);
  91. ad0x0_spi1_push(ADQS_ST7735_SET_DC,1);
  92. //ad0x0_spi1_push_tx_pbuf(buff);
  93. ad0x0_spi1_push_tx_pbuf_dma_ext((uint8_t*)buff,NULL, buff_size);
  94. }
  95. static void ST7735_WriteByte(uint8_t _byte) {
  96. ad0x0_spi1_push(ADQS_ST7735_SET_DC,1);
  97. ad0x0_spi1_push(ADQS_TX,_byte);
  98. }
  99. static void ST7735_ExecuteCommandList(const uint8_t *addr) {
  100. uint8_t numCommands, numArgs;
  101. uint16_t ms;
  102. numCommands = *addr++;
  103. while(numCommands--) {
  104. uint8_t cmd = *addr++;
  105. ST7735_WriteCommand(cmd);
  106. numArgs = *addr++;
  107. // If high bit set, delay follows args
  108. ms = numArgs & DELAY;
  109. numArgs &= ~DELAY;
  110. if(numArgs) {
  111. ST7735_WriteData((uint8_t*)addr, numArgs);
  112. addr += numArgs;
  113. }
  114. if(ms) {
  115. ms = *addr++;
  116. if(ms == 255) ms = 500;
  117. //HAL_Delay(ms);
  118. ad0x0_spi1_push(ADQS_DELAY_MS,ms);
  119. }
  120. }
  121. ad0x0_spi1_wait();
  122. }
  123. static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
  124. // column address set
  125. ad0x0_spi1_wait();
  126. ST7735_WriteCommand(ST7735_CASET);//2a
  127. uint8_t data1[] = { 0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART };
  128. ST7735_WriteData(data1, sizeof(data1));
  129. // row address set
  130. ST7735_WriteCommand(ST7735_RASET);//2b
  131. uint8_t data2[] = { 0x00, y0 + ST7735_YSTART, 0x00, y1 + ST7735_YSTART };
  132. ST7735_WriteData(data2, sizeof(data2));
  133. // write to RAM
  134. ST7735_WriteCommand(ST7735_RAMWR);//2c
  135. ad0x0_spi1_wait();
  136. }
  137. void ST7735_Init() {
  138. //cs=0;res=0;delay(5ms);res=1;cs=1;
  139. ad0x0_spi1_push(ADQS_ST7735_SET_CS,0);
  140. ad0x0_spi1_push(ADQS_ST7735_SET_RESET,0);
  141. ad0x0_spi1_push(ADQS_DELAY_MS,5);
  142. ad0x0_spi1_push(ADQS_ST7735_SET_RESET,1);
  143. ad0x0_spi1_push(ADQS_ST7735_SET_CS,1);
  144. // ST7735_Reset();
  145. ST7735_ExecuteCommandList(Rcmd1);
  146. ST7735_ExecuteCommandList(init_cmds2);
  147. ST7735_WriteCommand(ST7735_MADCTL); //0x36
  148. ST7735_WriteByte(ST7735_ROTATION);
  149. ST7735_ExecuteCommandList(init_cmds3);
  150. //ST7735_WriteCommand(ST7735_INVOFF);//0x20
  151. //ST7735_Unselect();
  152. }
  153. void ST7735_Init2() {
  154. //cs=0;res=0;delay(5ms);res=1;cs=1;
  155. ad0x0_spi1_push(ADQS_ST7735_SET_CS,0);
  156. ad0x0_spi1_push(ADQS_ST7735_SET_RESET,0);
  157. ad0x0_spi1_push(ADQS_DELAY_MS,30);
  158. ad0x0_spi1_push(ADQS_ST7735_SET_RESET,1);
  159. ad0x0_spi1_push(ADQS_DELAY_MS,15);
  160. ad0x0_spi1_push(ADQS_ST7735_SET_CS,1);
  161. ST7735_WriteCommand(ST7735_COLMOD);//0x3A
  162. ST7735_WriteByte(0x5);
  163. ad0x0_spi1_push(ADQS_DELAY_MS,12);
  164. // Send_CMD(0x01);
  165. ST7735_WriteCommand(0x1);
  166. ad0x0_spi1_push(ADQS_DELAY_MS,12);
  167. // _delay_ms(12);
  168. ST7735_WriteCommand(ST7735_SLPOUT);//0x11
  169. ad0x0_spi1_push(ADQS_DELAY_MS,30);
  170. //Send_CMD(0x36);
  171. //Send_DAT(0xA0);
  172. ST7735_WriteCommand(ST7735_MADCTL); //0x36
  173. ST7735_WriteByte(ST7735_ROTATION);
  174. ST7735_WriteCommand(ST7735_INVCTR); //0xB4
  175. ST7735_WriteByte(0x0);
  176. //ST7735_WriteCommand(ST7735_INVOFF);//0x20
  177. //Send_CMD(0x29);
  178. ST7735_WriteCommand(0x29);
  179. }
  180. void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  181. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
  182. return;
  183. // ST7735_Select();
  184. ST7735_SetAddressWindow(x, y, x+1, y+1);
  185. uint8_t data[] = { color >> 8, color & 0xFF };
  186. ST7735_WriteData(data, sizeof(data));
  187. // ST7735_Unselect();
  188. }
  189. /*static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
  190. uint32_t i, b, j;
  191. ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
  192. for(i = 0; i < font.height; i++) {
  193. b = font.data[(ch - 32) * font.height + i];
  194. for(j = 0; j < font.width; j++) {
  195. if((b << j) & 0x8000) {
  196. uint8_t data[] = { color >> 8, color & 0xFF };
  197. ST7735_WriteData(data, sizeof(data));
  198. } else {
  199. uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF };
  200. ST7735_WriteData(data, sizeof(data));
  201. }
  202. }
  203. }
  204. }*/
  205. void ST7735_WriteChar2(uint16_t x, uint16_t y, char ch, FontDef *font, uint16_t color, uint16_t bgcolor) {
  206. uint32_t i, b, j;
  207. AD0X0_LE_SWAP(color);
  208. AD0X0_LE_SWAP(bgcolor);
  209. ST7735_SetAddressWindow(x, y, x+font->width-1, y+font->height-1);
  210. //почему я ее засунул сюда? потому что буфер символа еще будет передаваться
  211. //после выхода из функции... при рендере нового мы его будем перезаписывать.. по этому ждем нахрен(
  212. //сука 20кб у стм32ф103с8т6 это блядство чистое... ну че б не сделать 128кб?! тогда бы экран в память влезал!!
  213. ad0x0_spi1_wait();
  214. uint16_t *p,*ptxspi;
  215. p=ad0x0_charbuf;
  216. ad0x0_spi1_push(ADQS_ST7735_SET_DC,1);//!!!!!!!!!!!!!!! сука бля 2 часа просидел!!!!! тупейшая идея с DC
  217. for(i = 0; i < font->height; i++) {
  218. b = font->data[(ch - 32) * font->height + i];
  219. //ad0x0_spi1_push_ext_setbuf((uint8_t*)p,NULL, font->width<<1);//текущая строка символа
  220. ptxspi=p;
  221. for(j = 0; j < font->width; j++) {
  222. if((b << j) & 0x8000) {
  223. //uint8_t data[] = { color >> 8, color & 0xFF };
  224. //ST7735_WriteData(data, sizeof(data));
  225. *p++=(uint16_t)color;
  226. } else {
  227. //uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF };
  228. //ST7735_WriteData(data, sizeof(data));
  229. *p++=(uint16_t)bgcolor;
  230. //*p++=(uint16_t)0x0000;
  231. }
  232. }
  233. ad0x0_spi1_push_tx_pbuf_dma_ext((uint8_t*)ptxspi,NULL,font->width<<1);
  234. }
  235. ad0x0_spi1_wait();
  236. }
  237. /*
  238. Simpler (and probably slower) implementation:
  239. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color) {
  240. uint32_t i, b, j;
  241. for(i = 0; i < font.height; i++) {
  242. b = font.data[(ch - 32) * font.height + i];
  243. for(j = 0; j < font.width; j++) {
  244. if((b << j) & 0x8000) {
  245. ST7735_DrawPixel(x + j, y + i, color);
  246. }
  247. }
  248. }
  249. }
  250. */
  251. void ST7735_WriteString(uint16_t x, uint16_t y, unsigned char* str, FontDef *font, uint16_t color, uint16_t bgcolor) {
  252. //ST7735_Select();
  253. while(*str) {
  254. if(x + font->width >= ST7735_WIDTH) {
  255. x = 0;
  256. y += font->height;
  257. if(y + font->height >= ST7735_HEIGHT) {
  258. break;
  259. }
  260. if(*str == ' ') {
  261. // skip spaces in the beginning of the new line
  262. str++;
  263. continue;
  264. }
  265. }
  266. // ST7735_WriteChar(x, y, *str, font, color, bgcolor);
  267. ST7735_WriteChar2(x, y, *str, font, color, bgcolor);
  268. x += font->width;
  269. str++;
  270. }
  271. ad0x0_spi1_wait();
  272. //ST7735_Unselect();
  273. }
  274. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  275. AD0X0_LE_SWAP(color);
  276. // clipping
  277. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
  278. if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x;
  279. if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y;
  280. //ST7735_Select();
  281. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  282. // uint8_t data[] = { color >> 8, color & 0xFF };
  283. //HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
  284. uint16_t ad0x0_d[160],*p=ad0x0_d;
  285. for(x = w; x > 0; x--) *p++=color;
  286. ad0x0_spi1_push(ADQS_ST7735_SET_DC,1);
  287. //ad0x0_spi1_push_tx_pbuf((uint8_t*)(ad0x0_d));
  288. for(y = h; y > 0; y--) {
  289. ad0x0_spi1_push_tx_pbuf_ext((uint8_t*)(ad0x0_d),NULL, w);
  290. //буфер у очереди останется на той позиции где тормознулся, по этому не переприсваиваем
  291. //ad0x0_spi1_push_ext_setbuf((uint8_t*)(ad0x0_d),NULL, w);
  292. ad0x0_spi1_push(ADQS_TX_PBUF_DMA,0);
  293. /*for(x = w; x > 0; x--) {
  294. //HAL_SPI_Transmit(&ST7735_SPI_PORT, data, sizeof(data), HAL_MAX_DELAY);
  295. ad0x0_spi1_push_tx_pbuf(data);
  296. ad0x0_spi1_push(ADQS_TX_PBUF,sizeof(data));
  297. }*/
  298. }
  299. //while(!ad0x0_spi1_is_queue_empty()){}//!!!!!!!!!!ubrat' etot pizdets
  300. ad0x0_spi1_wait();
  301. //ST7735_Unselect();
  302. }
  303. void ST7735_FillScreen(uint16_t color) {
  304. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  305. }
  306. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  307. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
  308. if((x + w - 1) >= ST7735_WIDTH) return;
  309. if((y + h - 1) >= ST7735_HEIGHT) return;
  310. //ST7735_Select();
  311. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  312. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  313. //ST7735_Unselect();
  314. }
  315. void ST7735_InvertColors(bool invert) {
  316. //ST7735_Select();
  317. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  318. //ST7735_Unselect();
  319. }
  320. void ST7735_Test(void){
  321. ad0x0_err();
  322. /*ST7735_FillScreen(ST7735_BLACK);
  323. for(int x = 0; x < ST7735_WIDTH; x++) {
  324. ST7735_DrawPixel(x, 0, ST7735_RED);
  325. ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED);
  326. }
  327. for(int y = 0; y < ST7735_HEIGHT; y++) {
  328. ST7735_DrawPixel(0, y, ST7735_RED);
  329. ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED);
  330. }
  331. HAL_Delay(1000);
  332. // Check fonts
  333. ST7735_FillScreen(ST7735_BLACK);
  334. ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_RED, ST7735_BLACK);
  335. ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", &Font_11x18, ST7735_GREEN, ST7735_BLACK);
  336. //ST7735_WriteString(0, 3*10+3*18, "Font_16x26", &Font_16x26, ST7735_BLUE, ST7735_BLACK);
  337. HAL_Delay(1000);
  338. // Check colors
  339. ST7735_FillScreen(ST7735_BLACK);
  340. ST7735_WriteString(0, 0, "BLACK", &Font_11x18, ST7735_WHITE, ST7735_BLACK);
  341. ST7735_WriteString(0, 30, "тест русского ШРИФТА. Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  342. HAL_Delay(500);
  343. ST7735_FillScreen(ST7735_RED);
  344. ST7735_WriteString(0, 0, "RED", &Font_11x18, 0x0000, ST7735_RED);
  345. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  346. HAL_Delay(500);
  347. ST7735_FillScreen(ST7735_GREEN);
  348. ST7735_WriteString(0, 0, "GREEN", &Font_11x18, ST7735_BLACK, ST7735_GREEN);
  349. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  350. HAL_Delay(500);
  351. ST7735_FillScreen(ST7735_BLUE);
  352. ST7735_WriteString(0, 0, "BLUE", &Font_11x18, ST7735_BLACK, ST7735_BLUE);
  353. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  354. HAL_Delay(500);
  355. ST7735_FillScreen(ST7735_CYAN);
  356. ST7735_WriteString(0, 0, "CYAN", &Font_11x18, ST7735_BLACK, ST7735_CYAN);
  357. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  358. HAL_Delay(500);
  359. ST7735_FillScreen(ST7735_MAGENTA);
  360. ST7735_WriteString(0, 0, "MAGENTA", &Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
  361. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  362. HAL_Delay(500);
  363. ST7735_FillScreen(ST7735_YELLOW);
  364. ST7735_WriteString(0, 0, "YELLOW", &Font_11x18, ST7735_BLACK, ST7735_YELLOW);
  365. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  366. HAL_Delay(500);
  367. ST7735_FillScreen(ST7735_WHITE);
  368. ST7735_WriteString(0, 0, "WHITE", &Font_11x18, ST7735_BLACK, ST7735_WHITE);
  369. ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK);
  370. HAL_Delay(500);*/
  371. }
  372. static unsigned char * i16_2s(int16_t value, unsigned char *buffer)
  373. {
  374. memset(buffer,' ',8);
  375. buffer += 7;
  376. char m=(value<0)?'-':' ';
  377. if(value<0)value=-value;
  378. // 11 áàéò äîñòàòî÷íî äëÿ äåñÿòè÷íîãî ïðåäñòàâëåíèÿ 32-õ áàéòíîãî ÷èñëà
  379. // è çàâåðøàþùåãî íóëÿ
  380. *--buffer = 0;
  381. do
  382. {
  383. *--buffer = value % 10 + '0';
  384. value /= 10;
  385. }
  386. while (value != 0);
  387. *--buffer=m;
  388. return buffer;
  389. }
  390. static unsigned char * i32_2s(int32_t value, unsigned char *buffer)
  391. {
  392. memset(buffer,' ',8);
  393. buffer += 7;
  394. char m=(value<0)?'-':' ';
  395. if(value<0)value=-value;
  396. // 11 áàéò äîñòàòî÷íî äëÿ äåñÿòè÷íîãî ïðåäñòàâëåíèÿ 32-õ áàéòíîãî ÷èñëà
  397. // è çàâåðøàþùåãî íóëÿ
  398. *--buffer = 0;
  399. do
  400. {
  401. *--buffer = value % 10 + '0';
  402. value /= 10;
  403. }
  404. while (value != 0);
  405. *--buffer=m;
  406. return buffer;
  407. }