{"id":16205,"date":"2016-08-25T10:00:03","date_gmt":"2016-08-25T17:00:03","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=16205"},"modified":"2021-06-16T13:56:44","modified_gmt":"2021-06-16T20:56:44","slug":"fundementals-of-writing-a-library","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/fundementals-of-writing-a-library\/","title":{"rendered":"FUNdamentals of Writing a Library"},"content":{"rendered":"<p>Since working at Digilent, I have learned several lessons. One of these is that whenever I am done programming a library, I should have some fun by creating a small side project using it. This way, I can understand my library on a new depth, and find any limitations or errors in it. Also I get to have fun, which is well, always fun!<\/p>\n<figure id=\"attachment_16215\" aria-describedby=\"caption-attachment-16215\" style=\"width: 600px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-16215 size-medium\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/20160816_150920-1-600x338.jpg\" alt=\"20160816_150920\" width=\"600\" height=\"338\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/20160816_150920-1-600x338.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/20160816_150920-1-768x432.jpg 768w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/20160816_150920-1-1024x576.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/20160816_150920-1-800x450.jpg 800w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/20160816_150920-1-1200x675-cropped.jpg 1200w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-16215\" class=\"wp-caption-text\">Having so much fun!<\/figcaption><\/figure>\n<p>A good example of this occurred\u00a0last week. I just finished a library for the <a href=\"https:\/\/digilent.com\/shop\/pmodssd-seven-segment-display\/\">PmodSSD<\/a>, which is a 2 digit seven segment display. This Pmod\u00a0utilizes 12 pins. Pins 1 through 7 each light up a single segment on the display, while pin 8 switches between the left and right digit. This display can only light up one digit at a time, so in order to simulate a 2 digit number it needs to flash the numbers on both sides fast enough so that the human eye can see\u00a0both numbers at once.<\/p>\n<p>Here is what I came up with:<\/p>\n<blockquote><p><code>void SnakePattern(){<br \/>\n\/\/ start timer<br \/>\nlong startTimer = counter;<br \/>\nwhile(startTimer + displayTime &gt; counter){<br \/>\n\/\/ select left digit<br \/>\ndigitalWrite(pinArray[7], HIGH);<br \/>\n\/\/ print snake at segment A of left digit<br \/>\ndigitalWrite(pinArray[0], HIGH);<br \/>\ndigitalWrite(pinArray[1], LOW);<br \/>\ndigitalWrite(pinArray[2], LOW);<br \/>\ndigitalWrite(pinArray[3], LOW);<br \/>\ndigitalWrite(pinArray[4], LOW);<br \/>\ndigitalWrite(pinArray[5], LOW);<br \/>\ndigitalWrite(pinArray[6], LOW);<br \/>\ndelay(1);<\/code><\/p>\n<p>\/\/ select right digit<br \/>\ndigitalWrite(pinArray[7], LOW);<br \/>\n\/\/ print snake at segment A of right digit<br \/>\ndigitalWrite(pinArray[0], HIGH);<br \/>\ndigitalWrite(pinArray[1], LOW);<br \/>\ndigitalWrite(pinArray[2], LOW);<br \/>\ndigitalWrite(pinArray[3], LOW);<br \/>\ndigitalWrite(pinArray[4], LOW);<br \/>\ndigitalWrite(pinArray[5], LOW);<br \/>\ndigitalWrite(pinArray[6], LOW);<br \/>\ndelay(1);<br \/>\n}<br \/>\nstartTimer = counter;<br \/>\nwhile(startTimer + displayTime &gt; counter){<br \/>\n\/\/ select left digit<br \/>\ndigitalWrite(pinArray[7], LOW);<br \/>\n\/\/ print snake at segment B right digit<br \/>\ndigitalWrite(pinArray[0], LOW);<br \/>\ndigitalWrite(pinArray[1], HIGH);<br \/>\ndigitalWrite(pinArray[2], LOW);<br \/>\ndigitalWrite(pinArray[3], LOW);<br \/>\ndigitalWrite(pinArray[4], LOW);<br \/>\ndigitalWrite(pinArray[5], LOW);<br \/>\ndigitalWrite(pinArray[6], LOW);<br \/>\ndelay(1);<\/p>\n<p>\/\/ This continues on and on until pattern is done. It is a lot of lines.<\/p><\/blockquote>\n<p>My library was very simple, as you can see above. Basically, it takes a number, matches it to a case in a switch statement, and that case performs 7 digitalWrites() to display that number on the <a href=\"https:\/\/digilent.com\/shop\/pmodssd-seven-segment-display\/\">PmodSSD<\/a>. This function worked, and I was about to publish it.<\/p>\n<p>However,\u00a0I followed my lesson and decided to have some fun! Numbers are boring, I wanted to make some extravagant patterns on the display. So I set off to simulate a pattern of a snake circling around the display.<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" width=\"735\" height=\"551\" src=\"https:\/\/www.youtube.com\/embed\/SmOW49avxeU?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/div>\n<p>I coded this pattern the same way I coded the numbers being displayed, by individually telling pins 1 to 7 to be HIGH or LOW. As you can imagine, this requires A LOT of lines of code, and therefore a lot of screen scrolling. Without wanting to make this pattern, I wouldn&#8217;t have known just how wasteful my code was. To make it more efficient, I fit all\u00a07 lines of digitalWrite()&#8217;s into a single binary array.<\/p>\n<blockquote><p><code><br \/>\nvoid DisplayPattern(int binArray1[7], int binArray1[7]){<br \/>\n\/\/ grab current time from timer to start countdown<br \/>\nlong startTimer = counter;<br \/>\nwhile(startTimer + displayTime &gt; counter){<br \/>\n\/\/ start backwards so that the digit selection is written to first<br \/>\nfor(int i = 7; i &gt;= 0; i--){<br \/>\nif(binArray1[i]== 1){<br \/>\ndigitalWrite(pinArray[i], HIGH);<br \/>\n} else {<br \/>\ndigitalWrite(pinArray[i], LOW);<br \/>\n}<br \/>\n}<br \/>\nfor(int i = 7; i &gt;= 0; i--){<br \/>\nif(binArray2[i]== 1){<br \/>\ndigitalWrite(pinArray[i], HIGH);<br \/>\n} else {<br \/>\ndigitalWrite(pinArray[i], LOW);<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>void SnakePattern(){<br \/>\n\/\/ print snake at segment A of left digit<br \/>\nint snakeArray1[] = {1, 0, 0, 0, 0, 0, 0, 1};<br \/>\nint snakeArray2[] = {0, 0, 0, 0, 0, 0, 0, 0};<br \/>\nDisplayBinnaryArray(snakeArray1, snakeArray1);<br \/>\ndelay(1);<\/p>\n<p>\/\/ print snake at segment A of right digit<br \/>\nint snakeArray1[] = {0, 0, 0, 0, 0, 0, 0, 1};<br \/>\nint snakeArray2[] = {1, 0, 0, 0, 0, 0, 0, 0};<br \/>\nDisplayBinnaryArray(snakeArray1, snakeArray1);<\/p>\n<p>\/\/ print snake at segment B of right digit<br \/>\nint snakeArray1[] = {0, 0, 0, 0, 0, 0, 0, 1};<br \/>\nint snakeArray2[] = {0, 1, 0, 0, 0, 0, 0, 0};<br \/>\nDisplayBinnaryArray(snakeArray1, snakeArray1);<\/p>\n<p>\/\/ print snake at segment C of right digit<br \/>\nint snakeArray1[] = {0, 0, 0, 0, 0, 0, 0, 1};<br \/>\nint snakeArray2[] = {0, 0, 1, 0, 0, 0, 0, 0};<br \/>\nDisplayBinnaryArray(snakeArray1, snakeArray1);<\/p>\n<p>\/\/ print snake at segment D of right digit<br \/>\nint snakeArray1[] = {0, 0, 0, 0, 0, 0, 0, 1};<br \/>\nint snakeArray2[] = {0, 0, 0, 1, 0, 0, 0, 0};<br \/>\nDisplayBinnaryArray(snakeArray1, snakeArray1);<\/p>\n<p>\/\/ continues<\/p><\/blockquote>\n<p>This solution trimmed down a lot of lines of code. However, it could be reduced even more. Instead of passing a binary array, I wrote a function that takes\u00a0a number between 0 and 255, interprets it as a binary array, and displays that pattern on the <a href=\"https:\/\/digilent.com\/shop\/pmodssd-seven-segment-display\/\">PmodSSD<\/a>. This took my previous solution, and pushed it into one simple integer. Now I could easily write whole patterns in just one array of bytes.<\/p>\n<blockquote><p><code>void DisplayByte(int byte){<br \/>\nif(byte &lt; 0 || byte &gt; 255){<br \/>\nreturn;<br \/>\n}<br \/>\nint mask = 1 &lt;&lt; (7); for(int x = 7; x &gt;= 0; x--){<br \/>\nif( (byte &amp; mask) == 0 ){<br \/>\ndigitalWrite(pinArray[x], LOW);<br \/>\n}else{<br \/>\ndigitalWrite(pinArray[x], HIGH);<br \/>\n}<br \/>\nmask &gt;&gt;= 1;<br \/>\n}<br \/>\ndelay(1);<br \/>\n}<\/code><\/p>\n<p>void DisplayArrayOfBytes(int ByteArray[], long displayTime){<br \/>\n\/\/ grab current time from timer to start countdown<br \/>\nlong startTimer;<br \/>\nint i = 0;<br \/>\nwhile(ByteArray[i]!= -1){<br \/>\nstartTimer = counter;<br \/>\nwhile(startTimer + displayTime &gt; counter){<br \/>\nDisplayByte(ByteArray[i]);<br \/>\n\/\/ wait a bit<\/p>\n<p>DisplayByte(ByteArray[i+1]);<br \/>\n\/\/ wait a bit<br \/>\n}<br \/>\ni += 2;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Send DisplayByte an array of bytes containing a snake pattern<br \/>\nvoid SnakePattern(){<br \/>\nint circleAnimation[15] = {129, 1, 128, 3, 128, 6, 128, 12, 136, 8, 152, 0, 176, 0, -1};<br \/>\nDisplayBinnaryArray(circleAnimation);<br \/>\n}<\/p><\/blockquote>\n<p>What originally took 26 lines of code could now fit into just 2 lines of code! My library now seemed to be at its most optimal. Without creating a fun side project with my library, I wouldn&#8217;t know how much more improved it could be.<\/p>\n<p>Of course, most\u00a0anything\u00a0could always be more optimal. If you have any ideas improving my code, leave a comment below! Keep in mind that this is not the actual code in the library, which you can find <a href=\"https:\/\/digilent.com\/reference\/pmod\/pmodssd\/start?redirect=1id=pmod\/pmod\/ssd\">here<\/a>, but a representation of the idea behind its design. Now that my library is published, I went a bit crazy with the patterns&#8230;<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" width=\"735\" height=\"413\" src=\"https:\/\/www.youtube.com\/embed\/FkAk9LGnCLM?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/div>\n<p>I hope this provided some insight on the creation of libraries, as well as the importance of doing projects to test out what you come up with!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-16205 jlk' data-task='like' data-post_id='16205' data-nonce='a7290e5e40' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-16205 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-16205 jlk' data-task='unlike' data-post_id='16205' data-nonce='a7290e5e40' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-16205 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-16205 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Check out a fun Pmod project using the PmodSSD display, and learn a bit about the basics of writing libraries!<\/p>\n","protected":false},"author":45,"featured_media":16233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38,1561],"tags":[104],"ppma_author":[4495],"class_list":["post-16205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-expansion-modules","category-applications","tag-project-2"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/08\/boom-zaam-zoom.png","jetpack_sharing_enabled":true,"authors":[{"term_id":4495,"user_id":45,"is_guest":0,"slug":"emarsh","display_name":"Eric Marsh","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/d65ca3ee4750fc49bf338630709f4b7073fb08e524f159a6c4ee6f0b900b96e9?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/16205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=16205"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/16205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/16233"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=16205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=16205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=16205"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=16205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}